Skip to content

Commit 8fe9ab9

Browse files
authored
Merge pull request #13 from gjbex/development
Development
2 parents 179f2d7 + 4b96831 commit 8fe9ab9

37 files changed

+625
-353
lines changed

environment.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: python_for_systems_programming
22
channels:
3-
- defaults
3+
- conda-forge
44
dependencies:
5+
- click
56
- paramiko
67
- matplotlib
78
- jinja2
@@ -10,6 +11,10 @@ dependencies:
1011
- fabric
1112
- numpy
1213
- jupyterlab
13-
pip:
14-
- schedule
15-
prefix: /home/gjb/miniconda3/envs/python_for_systems_programming
14+
- uvicorn
15+
- ca-certificates
16+
- certifi
17+
- openssl
18+
- fastapi
19+
- fire
20+
prefix: /home/gjb/mambaforge/envs/python_for_systems_programming

python_for_systems_programming.pptx

2 Bytes
Binary file not shown.

python_for_systems_programming_linux64_conda_specs.txt

Lines changed: 289 additions & 132 deletions
Large diffs are not rendered by default.

source-code/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ to create it. There is some material not covered in the presentation as well.
4444
1. `subprocess`: illustrates executing a shell command from a Python script
4545
using the `subprocess` module.
4646
1. `xml-generator`: code to generate a random XML documents.
47+
1. `fastapi`: simple illustrations of using FastAPI for webservices.

source-code/code-evaluation/fac.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
def fac(n):
2-
if n < 2:
3-
return 1
4-
else:
5-
return n*fac(n-1)
2+
return 1 if n < 2 else n*fac(n-1)

source-code/code-evaluation/fib.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
def fib(n):
2-
if n == 0 or n == 1:
3-
return 1
4-
else:
5-
return fib(n-1) + fib(n-2)
2+
return 1 if n in [0, 1] else fib(n-1) + fib(n-2)

source-code/command-line-arguments/ArgParse/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ $ ./generate_gaussian.py -h
2323
./options_in_file.py --foo something @file_options.txt
2424
```
2525
1. `file_options.txt`: file containing options for `options_in_file.py`.
26+
1. `Rerun`: experiment with file options.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Rerunnable
2+
3+
Toy application to explore the possibilities created by
4+
using argparse options stored in a file.
5+
6+
7+
## What is it?
8+
1. rerunnable.py`: script to explore the possibilities.
9+
10+
11+
## How to use?
12+
13+
```bash
14+
$ ./rerunnable.py --verbose --number 5 --type bye gjb
15+
$ ./rerunnable.py @rerunnable_cla.txt
16+
```
17+
18+
This will rerun the application with all the settings specified for the
19+
previous run.
20+
21+
To override options:
22+
```bash
23+
$ ./rerunnable.py @rerunnable_cla.txt --number 3
24+
```
25+
26+
27+
## Conclusions
28+
29+
This approach works well for command line options, e.g., `--number 5`.
30+
31+
It is not flexibile for flags, e.g., `--verbose` since they can not be "unset".
32+
33+
It is not flexible either for positional arguments since they can not be modified.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
5+
6+
def dump_options(options, pos_args=None, exclude=None):
7+
options_dict = vars(options)
8+
with open('rerunnable_cla.txt', 'w') as file:
9+
for key, value in options_dict.items():
10+
if ((exclude is None or key not in exclude) and
11+
(pos_args is None or key not in pos_args)):
12+
if isinstance(value, bool):
13+
if value:
14+
print(f'--{key}', file=file)
15+
else:
16+
print(f'--{key}\n{value}', file=file)
17+
if pos_args is not None:
18+
for key in pos_args:
19+
print(options_dict[key], file=file)
20+
21+
22+
if __name__ == '__main__':
23+
arg_parser = argparse.ArgumentParser(
24+
fromfile_prefix_chars='@',
25+
description='application that saves its command line options and can be rerun'
26+
)
27+
arg_parser.add_argument('--number', type=int, default=1,
28+
help='number of messages to write')
29+
arg_parser.add_argument('--type', choices=('hello', 'bye'), default='hello',
30+
help='message type')
31+
arg_parser.add_argument('--verbose', action='store_true',
32+
help='verbose output')
33+
arg_parser.add_argument('name', help='person to message')
34+
options = arg_parser.parse_args()
35+
dump_options(options, pos_args=('name', ))
36+
if options.verbose:
37+
print(f'printing {options.number} messages')
38+
for _ in range(options.number):
39+
print(f'{options.type} {options.name}')

source-code/command-line-arguments/ArgParse/options_in_file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import argparse
44

55

6-
def amin():
6+
def main():
77
arg_parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
88
arg_parser.add_argument('--foo', help='foo option')
9-
arg_parser.add_argument('--bar', help='bar optoin')
9+
arg_parser.add_argument('--bar', help='bar option')
1010
arg_parser.add_argument('--flag', action='store_true',
1111
help='flag option')
1212
options = arg_parser.parse_args()
1313
print(options)
1414

1515
if __name__ == '__main__':
16-
amin()
16+
main()

0 commit comments

Comments
 (0)