Skip to content

Commit 966f76c

Browse files
updates
1 parent 6e1b4a4 commit 966f76c

8 files changed

Lines changed: 200 additions & 0 deletions

File tree

Advanced/clousre/multiply_time.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
def multiply(x):
3+
def inner(n):
4+
return x * n
5+
return inner
6+
7+
8+
times_2 = multiply(2)
9+
val = times_2(2)
10+
print(val)

Advanced/currying/1_temperature.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# source : https://python-course.eu/advanced-python/currying-in-python.php
2+
3+
# h(x) = g(f(x))
4+
5+
def compose(g, f):
6+
def h(x):
7+
return g(f(x))
8+
return h
9+
10+
def celsius_fahrenheit(t):
11+
return 1.8 * t + 32
12+
13+
def correct_error(t):
14+
return 0.9 * t - 0.5
15+
16+
17+
convert = compose(correct_error, celsius_fahrenheit)
18+
print(convert(10), celsius_fahrenheit(10))

Advanced/currying/2_bmi.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def compose(g, f):
2+
def h(*args, **kwargs):
3+
return g(f(*args, **kwargs))
4+
return h
5+
6+
def calucate_bmi(weight, height):
7+
return weight / height**2
8+
9+
def evaluate_bmi(bmi):
10+
msg = 'Obese Class III (Very severely obese)'
11+
12+
if bmi < 15:
13+
msg = 'Very severely underweight'
14+
elif bmi < 16:
15+
msg = 'Severely underweight'
16+
elif bmi < 18.5:
17+
msg = 'Underweight'
18+
elif bmi < 25:
19+
msg = 'Normal (healthy weight)'
20+
elif bmi < 30:
21+
msg = 'Overweight'
22+
elif bmi < 35:
23+
msg = 'Obese Class I (Moderately obese)'
24+
elif bmi < 40:
25+
msg = 'Obese Class II (Severely obese)'
26+
27+
return msg
28+
29+
bmi_calculator = compose(evaluate_bmi, calucate_bmi)
30+
weight = 69 # in kg
31+
height = 1.8 # in m
32+
33+
print(bmi_calculator(weight, height))

Advanced/currying/3_sum.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def summation(*args):
2+
return sum(args)
3+
4+
def curry(func):
5+
# to keep the name of the curried function:
6+
curry.__curried_func_name__ = func.__name__
7+
f_args, f_kwargs = [], {}
8+
def f(*args, **kwargs):
9+
nonlocal f_args, f_kwargs
10+
if args or kwargs:
11+
print("Calling curried function with:")
12+
print("args: ", args, "kwargs: ", kwargs)
13+
f_args += args
14+
f_kwargs.update(kwargs)
15+
print("Currying the values:")
16+
print("f_args: ", f_args)
17+
print("f_kwargs:", f_kwargs)
18+
return f
19+
else:
20+
print("Calling " + curry.__curried_func_name__ + " with:")
21+
print(f_args, f_kwargs)
22+
result = func(*f_args, *f_kwargs)
23+
f_args, f_kwargs = [], {}
24+
return result
25+
return f
26+
27+
28+
29+
curried_summation = curry(summation)
30+
curried_summation(1)(2)(3)(4, 5)
31+
32+
# it will keep on currying:
33+
# curried_arimean(6, 7)
34+
print(curried_summation())
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def add_world(wrapped_func):
2+
3+
def inner_func():
4+
wrapped_func()
5+
print('World')
6+
7+
return inner_func
8+
9+
@add_world
10+
def hello():
11+
print('Hello', end=' ')
12+
13+
14+
hello()

Advanced/decorators/2_nested.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Here's the example of nested function aka decorators.
3+
if you can guess the ouput you can guess the order of execution.
4+
"""
5+
6+
def a(wrapped_func):
7+
8+
def inner(*args, **kwargs):
9+
print('A')
10+
wrapped_func()
11+
return inner
12+
13+
def b(wrapped_func):
14+
15+
def inner(*args, **kwargs):
16+
print('B')
17+
wrapped_func('C')
18+
return inner
19+
20+
21+
@a
22+
@b
23+
def c(c=None):
24+
print(c)
25+
26+
27+
c()

Advanced/decorators/3_maths.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
def add(wrapped_func):
4+
5+
def inner(*args, **kwargs):
6+
c = args[0] + args[1]
7+
print('Adding value : {}, {}'.format(args[0], args[1]))
8+
print('Result {}'.format(c))
9+
return wrapped_func(c)
10+
return inner
11+
12+
def sqaure(wrapped_func):
13+
14+
def inner(*args, **kwargs):
15+
c = args[0] ** 2
16+
print('Squaring : {}'.format(args[0]))
17+
print('Result {}'.format(c))
18+
return c
19+
return inner
20+
21+
22+
@add
23+
@sqaure
24+
def maths_on_numners(a= None, b = None):
25+
pass
26+
27+
a, b = 6, 9
28+
value = maths_on_numners(a, b)
29+
out_msg = 'Value returned = {value}'
30+
print(out_msg.format(value = value))

Advanced/decorators/4_memo.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import time
2+
3+
cached = {}
4+
5+
def memo(wrapped_func):
6+
def inner(n1, n2):
7+
params = '{}{}'.format(n1, n2)
8+
if params not in cached:
9+
value = wrapped_func(n1, n2)
10+
cached[params] = value
11+
return value
12+
return cached[params]
13+
return inner
14+
15+
@memo
16+
def calucate_sum(n1, n2):
17+
_sum = 0
18+
for i in range(n1, n2):
19+
_sum += i
20+
return _sum
21+
22+
start = time.time()
23+
value = calucate_sum(1, 100000000)
24+
end = time.time()
25+
total_time = end - start
26+
print('Before memo')
27+
print('Value = {}, total time = {}'.format(value, total_time))
28+
29+
start = time.time()
30+
value = calucate_sum(1, 100000000)
31+
end = time.time()
32+
total_time = end - start
33+
print('\nAfter memo')
34+
print('Value = {}, total time = {}'.format(value, total_time))

0 commit comments

Comments
 (0)