Skip to content

Commit 79ac314

Browse files
committed
finished book example
1 parent fc5de79 commit 79ac314

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

chap7_caseif_dic.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def sum1(a, b):
2+
return a+b
3+
def sub(a, b):
4+
return a - b
5+
6+
def mul(a , b):
7+
return a * b
8+
9+
def divide(a , b):
10+
return a/b
11+
12+
func = [sum1, sub, mul, divide ]
13+
#method one
14+
operator = func[3]
15+
print(operator(1,2))
16+
#method two
17+
print(func[2](12,4))
18+
19+
20+
def dispatch_dict(operator, x ,y):
21+
return {
22+
'add': lambda: x + y,
23+
'sub': lambda: x - y,
24+
'mul': lambda: x * y,
25+
'div': lambda: x / y,
26+
}.get(operator, lambda : None)()
27+
28+
print(dispatch_dict('mul', 2, 8))
29+
30+
print(dispatch_dict('unknown', 2, 8))

chap7_dic_sorted.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
xs = {'a': 4, 'c': 2, 'b': 3, 'd': 1}
2+
3+
s = sorted(xs.items(), key=lambda x: x[0])
4+
print(s)

merge_dic.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
xs = {'a': 1, 'b': 2}
2+
ys = {'b': 3, 'c': 4}
3+
4+
# Merge xs and ys into zs using the dict() constructor
5+
zs = dict(xs, **ys)
6+
print(zs) # Output: {'a': 1, 'b': 3, 'c': 4}
7+
8+
xs1 = {'a': 1, 'b': 2}
9+
ys1 = {'b': 3, 'c': 4}
10+
11+
# Merge xs and ys into zs using dictionary unpacking
12+
zs1 = {**xs1, **ys1}
13+
print(zs1) # Output: {'a': 1, 'b': 3, 'c': 4}

nicer_debug.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from json import dumps as du
2+
mapping = {'a': 23, 'c': 42, 'b': 0xc0ffee}
3+
print(mapping)
4+
x = du(mapping, indent=4, sort_keys=True)#True
5+
print(x)

test1.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
4+
5+
from time import perf_counter
6+
from functools import wraps
7+
def dec(func):
8+
@wraps (func)
9+
def inner (*args, **kwargs):
10+
#time before run a func
11+
before = perf_counter()
12+
value = func(*args, **kwargs)
13+
#time after run a func
14+
after = perf_counter()
15+
pichidegi = after - before
16+
print (f"pichidegi zamani ", func.__name__, pichidegi)
17+
return None
18+
return inner
19+
l = [1, 3, 5, 6, 9]
20+
@dec
21+
def moraba_2(l):
22+
print([x**2 for x in l])
23+
24+
@dec
25+
def moraba_1(l):
26+
print([x**2 for x in l])
27+
28+
moraba_1(l)
29+
moraba_2(l)
30+
31+

0 commit comments

Comments
 (0)