Skip to content

Commit 38c7836

Browse files
committed
Merge remote-tracking branch 'origin/development' into development
2 parents 23bf62d + bee9797 commit 38c7836

3 files changed

Lines changed: 96 additions & 5 deletions

File tree

source-code/decorators/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ What is it?
1414
large. The decorated function in this example is the factorial.
1515
Also it illustrates `functools` wrap decorator to retain the wrapped
1616
function's name, docstring, etc.
17+
1. `decorator_arguments.py`: an example of a decorator that takes arguments.
18+
The `check_range` decorator takes a minimum and maximum value as arguments
19+
and checks if the wrapped function's argument falls within that range,
20+
throwing an exception if it does not.
1721
1. `memoize.py`: an example of adding a cache to a function using a simple
1822
custom decorator, as well as `functools`'s `lru_cache` decorator.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
3+
import functools
4+
5+
6+
def check_bounds(min_value, max_value):
7+
'''Check bounds of a function argument
8+
9+
Parameters
10+
----------
11+
min_value: float
12+
Smallest value allowed for the function's parameter
13+
max_value: float
14+
Largest value allowed for the function's parameter
15+
16+
Raises
17+
------
18+
ValueError:
19+
If the argument is outside the specified bounds
20+
'''
21+
def check_bounds_wrapper(_func):
22+
@functools.wraps(_func)
23+
def wrapper(x):
24+
if min_value > x or x > max_value:
25+
raise ValueError(f'argument {x} not in [{min_value}, {max_value}]')
26+
return _func(x)
27+
return wrapper
28+
return check_bounds_wrapper
29+
30+
31+
@check_bounds(min_value=-1.0, max_value=1.0)
32+
def silly(x):
33+
'''Compute a rather uninteresting function
34+
35+
Parameters
36+
----------
37+
x: float
38+
Argument for the function
39+
40+
Returns
41+
-------
42+
float:
43+
Value computed by the function
44+
'''
45+
return x**2 - 2.0
46+
47+
48+
if __name__ == '__main__':
49+
print(silly(0.5))
50+
try:
51+
print(silly(2.5))
52+
except ValueError as e:
53+
print(f'Excepton raised as expected: {e.message}')

source-code/object-orientation/dynamic_classes.ipynb

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,15 @@
119119
"metadata": {},
120120
"outputs": [],
121121
"source": [
122-
"Cat = make_dataclass('Cat', [('species', str, 'cat'), ('nr_legs', int, 4), ('sound', str, 'meow')], bases=(Animal, ))"
122+
"Cat = make_dataclass(\n",
123+
" 'Cat',\n",
124+
" [\n",
125+
" ('species', str, 'cat'),\n",
126+
" ('nr_legs', int, 4),\n",
127+
" ('sound', str, 'meow')\n",
128+
" ],\n",
129+
" bases=(Animal, )\n",
130+
")"
123131
]
124132
},
125133
{
@@ -202,7 +210,15 @@
202210
"metadata": {},
203211
"outputs": [],
204212
"source": [
205-
"Dog = make_dataclass('Dog', [('nr_legs', int, 4), ('species', str, 'dog'), ('sound', str, 'woof')], bases=(Animal, ))"
213+
"Dog = make_dataclass(\n",
214+
" 'Dog',\n",
215+
" [\n",
216+
" ('nr_legs', int, 4),\n",
217+
" ('species', str, 'dog'),\n",
218+
" ('sound', str, 'woof')\n",
219+
" ],\n",
220+
" bases=(Animal, )\n",
221+
")"
206222
]
207223
},
208224
{
@@ -423,7 +439,16 @@
423439
"metadata": {},
424440
"outputs": [],
425441
"source": [
426-
"Animal = type('Animal', tuple(), {'name': None, 'species': None, 'nr_legs': None, '__init__': animal_init})"
442+
"Animal = type(\n",
443+
" 'Animal',\n",
444+
" tuple(),\n",
445+
" {\n",
446+
" 'name': None,\n",
447+
" 'species': None,\n",
448+
" 'nr_legs': None,\n",
449+
" '__init__': animal_init,\n",
450+
" }\n",
451+
")"
427452
]
428453
},
429454
{
@@ -579,7 +604,16 @@
579604
"metadata": {},
580605
"outputs": [],
581606
"source": [
582-
"Cat = type('Cat', (Animal, ), {'name': None, 'species': 'cat', 'nr_legs': 4, 'sound': 'meow'})"
607+
"Cat = type(\n",
608+
" 'Cat',\n",
609+
" (Animal, ),\n",
610+
" {\n",
611+
" 'name': None,\n",
612+
" 'species': 'cat',\n",
613+
" 'nr_legs': 4,\n",
614+
" 'sound': 'meow',\n",
615+
" }\n",
616+
")"
583617
]
584618
},
585619
{
@@ -970,7 +1004,7 @@
9701004
"name": "python",
9711005
"nbconvert_exporter": "python",
9721006
"pygments_lexer": "ipython3",
973-
"version": "3.9.7"
1007+
"version": "3.12.3"
9741008
}
9751009
},
9761010
"nbformat": 4,

0 commit comments

Comments
 (0)