-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpython_experiments.py
172 lines (134 loc) · 3.06 KB
/
python_experiments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Append list to itself
a = [1, 2, 3, 4]
a.append(a)
a.append('3')
print(a, a[4])
# Create list from the list of lists
my_list = [[1, 2, 3], [4, 5], [6], [7, 8, 9]]
res = sum(my_list, [])
print(res)
# With list generator
res = [y for x in my_list for y in x]
print(res)
# With Reduce
from functools import reduce
from operator import add
res = reduce(add, my_list)
print(res)
a = set([1, 2, 3, 4])
b = set([3, 4, 5, 6])
print(a | b) # combine sets (OR)
print(a & b) # find common elements (AND)
print(a ^ b) # find unique elements in both sets (XOR)
# Create class inline
NewType = type('NewType', (object,), {'x': 'hello'})
c = NewType()
print(c)
print(c.x)
# Unpacking
first, second, *rest = (1, 2, 3, 4, 5, 6, 7, 8, 9)
print(rest)
# How Try/Except works
try:
print()
except Exception as e:
print('Here is our exception:', e)
else:
print('Try passed, except was not called')
finally:
print('do this anyway at the end')
print(' - ' * 20)
# How it works when in it has several levels
try:
try:
DD
except FileExistsError as e:
print('Inner exception handler', e)
finally:
print('Inner finally')
except Exception as e:
print('Here is our exception:', e)
else:
print('Try passed, except was not called')
finally:
print('do this anyway at the end')
print(' - ' * 20)
# Crazy lists
a = [1, 2]
b = a
b.append(3)
print(a)
c = a[:] # the right way to copy dicts
c.append('4')
print(a)
print(' - ' * 20)
# Do not init value with list like this
def foo(x=[]):
x.append('AAA')
print(x)
foo()
foo()
print(' - ' * 20)
def jim(phrase):
return 'Jim says: "%s".' % phrase
def say_something(person, phrase):
print(person(phrase))
say_something(jim, 'hey guys')
# Round can be with negative numbers:
print(round(1234.5678, -2))
import this
print(' - ' * 20)
a = [(1, 2), (3, 4), (5, 6)]
res = zip(*a)
print(list(res))
a = [1, 2, 4, 5, 6, 7]
b = [7, 8, 9]
res = dict(zip(a, b))
print(res)
res = list(zip(a, b))
print(res)
print(a)
print(*a) # hmm....
a[::2] = a[::-2] # Create a mirror effect
print(a)
from collections import Counter
s = 'habrahabr'
res = Counter(s)
print(res)
# The same:
res = {k: s.count(k) for k in s}
print(res)
# Map
numbers = [1, 2, 3, 4, 5]
res = map(lambda x: x * x, numbers)
res2 = (x*x for x in numbers) # create generator
print(list(res), list(res2))
# Filter
res = filter(lambda x: x < 4, numbers)
res2 = (x for x in numbers if x < 4) # create generator
print(list(res), list(res2))
# Intern strings:
# https://temofeev.ru/info/articles/ukazateli-v-python-v-chyem-sut/
a = 'test'
b = 'test'
print(a is b)
a = 'test!'
b = 'test!'
print(a is b)
def my_dec_with_params(test):
def my_decorator(func):
def internal_func(*args, **kwargs):
print('Before')
print(args)
args = (10, args[1])
res = func(*args, **kwargs)
print('After')
return res
return internal_func
return my_decorator()
@my_dec_with_params(test=2)
def my_test_func(a, b):
res = a + b
print(res)
return res
my_test_func(1, 3)