diff --git a/hands-on/shell_interaction.ipynb b/hands-on/shell_interaction.ipynb index 98a28c1..b394d12 100644 --- a/hands-on/shell_interaction.ipynb +++ b/hands-on/shell_interaction.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Intereacting with the shell" + "# Interacting with the shell" ] }, { @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The [`sh` module](https://amoffat.github.io/sh/) is very convenient to interact with the shell. Note that `sh` is not part of Python's standard library, if you prefer not to use extra modules, use the `subprocess` module in the standard library." + "The [`sh` module](https://amoffat.github.io/sh/) is very convenient to interact with the shell. Note that `sh` is not part of Python's standard library, if you prefer not to use extra modules, use the `subprocess` module in the standard library. The statements below will install `sh` using `pip` is it isn't already installed." ] }, { @@ -27,16 +27,12 @@ "metadata": {}, "outputs": [], "source": [ - "!pip install sh" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import sh" + "try:\n", + " import sh\n", + "except ModuleNotFoundError:\n", + " print('installing sh using pip')\n", + " !pip install sh\n", + " import sh" ] }, { @@ -314,7 +310,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "try:\n", @@ -323,6 +321,29 @@ " print(error)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Clean up" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Remove the `tmp` directory." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sh.rm('-rf', 'tmp')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -605,11 +626,43 @@ " env=environ, shell=True)\n", "print(process.stdout.rstrip())" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Clean up" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Remove the `tmp` directory." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "process = subprocess.run(['rm', '-rf', 'tmp'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "process.returncode" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -623,9 +676,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.5" + "version": "3.11.3" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/hands-on/system_information.ipynb b/hands-on/system_information.ipynb index 6590791..1abd216 100644 --- a/hands-on/system_information.ipynb +++ b/hands-on/system_information.ipynb @@ -12,16 +12,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Install `sh` since it doesn't seem to be available." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "!pip install sh" + "Install `sh` if it doesn't seem to be available." ] }, { @@ -34,7 +25,12 @@ "import os\n", "import platform\n", "import psutil\n", - "import sh\n", + "try:\n", + " import sh\n", + "except ModuleNotFoundError:\n", + " print('installing sh using pip')\n", + " !pip install sh\n", + " import sh\n", "import sys" ] }, @@ -152,15 +148,6 @@ "platform.version()" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "platform.linux_distribution()" - ] - }, { "cell_type": "code", "execution_count": null, @@ -233,13 +220,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ "for process in psutil.process_iter():\n", - " if 'chrome' in process.name():\n", + " if 'python' in process.name():\n", " cpu_times = process.cpu_times()\n", " thread_str = f'threads: {process.num_threads()}'\n", " cpu_str = f'user: {cpu_times.user}, sys: {cpu_times.system}'\n", @@ -328,7 +313,8 @@ "outputs": [], "source": [ "for user in psutil.users():\n", - " started = datetime.strftime(datetime.fromtimestamp(user.started), '%Y-%m-%d %H:%M:%S')\n", + " started = datetime.strftime(datetime.fromtimestamp(user.started),\n", + " '%Y-%m-%d %H:%M:%S')\n", " print(f'{user.name}: {started}')" ] }, @@ -432,7 +418,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -446,9 +432,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.5" + "version": "3.11.3" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/python_for_systems_programming.pptx b/python_for_systems_programming.pptx index b3d90f5..1c8cb47 100644 Binary files a/python_for_systems_programming.pptx and b/python_for_systems_programming.pptx differ diff --git a/source-code/command-line-arguments/Fire/sayer.py b/source-code/command-line-arguments/Fire/sayer.py index 10bebdc..b583433 100755 --- a/source-code/command-line-arguments/Fire/sayer.py +++ b/source-code/command-line-arguments/Fire/sayer.py @@ -55,7 +55,7 @@ def __init__(self, hello_name=None, bye_name=None): self.bye = Bye(bye_name) def to(self): - return 'Do you want to say hello or bye' + return 'Do you want to say hello or bye?' def info(self): return 'This is version 0.1beta' diff --git a/source-code/data-formats/read_xml.py b/source-code/data-formats/read_xml.py index 1254761..b3ba195 100755 --- a/source-code/data-formats/read_xml.py +++ b/source-code/data-formats/read_xml.py @@ -24,8 +24,7 @@ def finish(self): self._data.sort() def __str__(self): - return '\n'.join(['{0}: {1}'.format(self.name, x) - for x in self._data]) + return '\n'.join([f'{self.name}: {value}' for value in self._data]) class BlocksHandler(ContentHandler): @@ -54,10 +53,8 @@ def startDocument(self): def startElement(self, name, attrs): if name == 'block': - logging.info('start of {0}'.format(attrs.getValue('name'))) - parent_name = '' - if self._stack: - parent_name = self._stack[-1].name + '/' + logging.info(f'start of {attrs.getValue("name")}') + parent_name = f'{self._stack[-1].name}/' if self._stack else '' block = Block(parent_name + attrs.getValue('name')) self._stack.append(block) elif name == 'item': @@ -68,7 +65,7 @@ def characters(self, contents): contents = contents.strip() if contents: data = float(contents.strip()) - logging.info("found '{0}'".format(data)) + logging.info(f"found '{data}'") self._stack[-1].add_data(data) def endElement(self, name): @@ -76,7 +73,7 @@ def endElement(self, name): block = self._stack.pop() block.finish() self._blocks.append(block) - logging.info('end of {0}'.format(block.name)) + logging.info(f'end of {block.name}') elif name == 'item': self.in_item = False @@ -86,7 +83,7 @@ def endDocument(self): def main(): arg_parser = ArgumentParser(description='reformat XML code') - arg_parser.add_argument('-verbose', action='store_true', + arg_parser.add_argument('--verbose', action='store_true', help='print verbose output') arg_parser.add_argument('file', type=FileType('r'), help='XML file to convert') diff --git a/source-code/logging/log_it_all.py b/source-code/logging/log_it_all.py index 840b506..02ad53a 100755 --- a/source-code/logging/log_it_all.py +++ b/source-code/logging/log_it_all.py @@ -3,30 +3,30 @@ from argparse import ArgumentParser import logging import math -import os +from pathlib import Path import sys def do_stuff(n): if n < 0: - logging.error('can not do stuff for {0}'.format(n)) + logging.error(f'can not do stuff for {n}') return 1 for i in range(n): - logging.info('doing stuff {0}'.format(str(i))) - print('doing {0}: {1:.4f}'.format(i, math.sqrt(i))) - logging.info('done stuff {0}'.format(str(i))) + logging.info(f'doing stuff {i}') + print(f'doing {i}: {math.sqrt(i):.4f}') + logging.info(f'done stuff {i}') return 0 def main(): arg_parser = ArgumentParser(description='example for logging facility') - arg_parser.add_argument('-log', dest='log_file', + arg_parser.add_argument('--log', dest='log_file', help='name of log file') - arg_parser.add_argument('-info', action='store_true', + arg_parser.add_argument('--info', action='store_true', help='set log level to info') - arg_parser.add_argument('-new_log', action='store_true', + arg_parser.add_argument('--new_log', action='store_true', help='overwrite existing log file') - arg_parser.add_argument('-n', type=int, default=1, + arg_parser.add_argument('--n', type=int, default=1, help='number of times to do stuff') options = arg_parser.parse_args() format_str = '%(asctime)s:%(levelname)s:%(message)s' @@ -39,14 +39,15 @@ def main(): else: filemode = 'a' if options.log_file: - exists = os.path.exists(options.log_file) + log_file = Path(options.log_file) + exists = log_file.exists() logging.basicConfig(level=level, filename=options.log_file, filemode=filemode, format=format_str) else: exists = False logging.basicConfig(level=level, format=format_str) if exists: - logging.warn('overwriting existing log file') + logging.warning('overwriting existing log file') logging.info('application started') logging.info('logger initialized') status = do_stuff(options.n)