Skip to content

Development #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 20, 2023
Merged
85 changes: 69 additions & 16 deletions hands-on/shell_interaction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Intereacting with the shell"
"# Interacting with the shell"
]
},
{
Expand All @@ -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."
]
},
{
Expand All @@ -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"
]
},
{
Expand Down Expand Up @@ -314,7 +310,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"try:\n",
Expand All @@ -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": {},
Expand Down Expand Up @@ -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"
},
Expand All @@ -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
}
42 changes: 14 additions & 28 deletions hands-on/system_information.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
},
{
Expand All @@ -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"
]
},
Expand Down Expand Up @@ -152,15 +148,6 @@
"platform.version()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"platform.linux_distribution()"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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}')"
]
},
Expand Down Expand Up @@ -432,7 +418,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -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
}
Binary file modified python_for_systems_programming.pptx
Binary file not shown.
2 changes: 1 addition & 1 deletion source-code/command-line-arguments/Fire/sayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 6 additions & 9 deletions source-code/data-formats/read_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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':
Expand All @@ -68,15 +65,15 @@ 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):
if name == 'block':
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

Expand All @@ -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')
Expand Down
23 changes: 12 additions & 11 deletions source-code/logging/log_it_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down