Skip to content

Commit f4db559

Browse files
authored
Merge pull request #10 from gjbex/development
Development
2 parents 0bd6142 + 8818f0a commit f4db559

File tree

15 files changed

+155
-12
lines changed

15 files changed

+155
-12
lines changed

source-code/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ was used to develop it.
2525
1. `testing`: illustrates writing unit tests with `unittest` and
2626
`pytest`.
2727
1. `oo_vs_functional.ipynb`: comparing object-oriented approach to
28-
functional approach, including coroutines.
28+
functional approach, including coroutines.
2929
1. `metaclasses`: illustration of the use of metaclasses in Python.
3030
1. `data-structures`: illustration of Python and standard library data
3131
structures.
32+
1. `code-organization`: illustration of how to organize code in packages
33+
and modules.
34+
1. `error-handling`: simple illustration of error handling.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Code organization
2+
3+
Simple example of code organization in packages, modules.
4+
5+
## What is it?
6+
7+
1. `my_pkg`: Python package.
8+
1. `__init__.py`: package-level documentation and version
9+
information.
10+
1. `my_module.py`: simple module in the `my_pkg` package.
11+
1. `my_script.py`: Python script that uses the `my_module`
12+
module in the `my_pkg` package.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'''
2+
This is the my_pkg documentation
3+
'''
4+
5+
__version__ = '1.0'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def hello(name):
2+
return f'hello {name}'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python
2+
3+
import my_pkg.my_module
4+
5+
print(my_pkg.my_module.hello('world'))

source-code/error-handling/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Error handling
2+
3+
Illustrations of error handling.
4+
5+
## What is it?
6+
7+
1. `simple.py`: Python scripts that illustrates some
8+
ways ot handle errors.
9+
1. `data.txt`: data file to use as input.
10+
1. `data_not_ok.txt`: incorrect data file to use as input.

source-code/error-handling/data.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3.7
2+
3.3
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
3.7
2+
5.1
3+
some_string
4+
5.2

source-code/error-handling/simple.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
5+
ARG_ERROR = 1
6+
FILE_ERROR = 2
7+
DATA_ERROR = 3
8+
9+
def sum_numbers(file):
10+
total = 0
11+
for line in file:
12+
total += float(line)
13+
return total
14+
15+
16+
if __name__ == '__main__':
17+
if len(sys.argv) < 2:
18+
print('### error: no argument given', file=sys.stderr)
19+
sys.exit(ARG_ERROR)
20+
file_name = sys.argv[1]
21+
try:
22+
file = open(file_name, 'r')
23+
print(sum_numbers(file))
24+
except IOError as e:
25+
print(f'### IO Error on {e.filename}: {e.strerror}')
26+
sys.exit(FILE_ERROR)
27+
except ValueError:
28+
print(f'### Data Error on {file_name}: should contain only numbers')
29+
sys.exit(DATA_ERROR)

source-code/oo_vs_functional.ipynb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@
285285
" '''create a coroutine that keeps track of descriptive\n",
286286
" statistics of a data stream\n",
287287
" \n",
288-
" Params\n",
289-
" ------\n",
288+
" Parameters\n",
289+
" ----------\n",
290290
" stats : dict\n",
291291
" dictrionary that will hold the statistics, i.e., number of values,\n",
292292
" mean and standard deviation. The dictionary will contain None for\n",
@@ -396,8 +396,8 @@
396396
" '''create a coroutine that keeps track of descriptive\n",
397397
" statistics of a data stream\n",
398398
" \n",
399-
" Params\n",
400-
" ------\n",
399+
" Parameters\n",
400+
" ----------\n",
401401
" stats : dict\n",
402402
" dictrionary that will hold the statistics, i.e., number of values,\n",
403403
" mean and standard deviation. The dictionary will contain None for\n",
@@ -710,8 +710,8 @@
710710
" def __init__(self, sum_value: float=0.0, sum2: float=0.0, n: int=0):\n",
711711
" '''constructor for Stats objects\n",
712712
" \n",
713-
" Params\n",
714-
" ------\n",
713+
" Parameters\n",
714+
" ----------\n",
715715
" Either none, which initializes an object that has seen no data yet, or\n",
716716
" \n",
717717
" sum_value: float\n",
@@ -777,8 +777,8 @@
777777
" '''functional operator, creates a new Stats object when a value is added\n",
778778
" called as stats + value\n",
779779
" \n",
780-
" Params\n",
781-
" ------\n",
780+
" Parameters\n",
781+
" ----------\n",
782782
" value: float\n",
783783
" new data value to be added to the statistics\n",
784784
" \n",
@@ -792,8 +792,8 @@
792792
" def __iadd__(self, value: float):\n",
793793
" '''non-functional operator, updates the Stats object when a value is added\n",
794794
" \n",
795-
" Params\n",
796-
" ------\n",
795+
" Parameters\n",
796+
" ----------\n",
797797
" value: float\n",
798798
" new data value to be added to the statistics\n",
799799
" \n",
@@ -1568,7 +1568,7 @@
15681568
"name": "python",
15691569
"nbconvert_exporter": "python",
15701570
"pygments_lexer": "ipython3",
1571-
"version": "3.10.9"
1571+
"version": "3.11.2"
15721572
}
15731573
},
15741574
"nbformat": 4,

0 commit comments

Comments
 (0)