-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPythonSyntax.py
More file actions
373 lines (257 loc) · 7.46 KB
/
Copy pathPythonSyntax.py
File metadata and controls
373 lines (257 loc) · 7.46 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from datetime import datetime
import math
# Datetime
now = datetime.now()
print('%s/%s/%s %s:%s:%s' %
(now.month, now.day, now.year, now.month, now.day, now.year))
# String
print(ord('A'), chr(66), '\u4e2d\u6587') # 65, B, '中文'
x = b'ABC' # each char occupies only 1 byte instead of unicode
print('ABC'.encode('ascii')) # b'ABC'
print('中文'.encode('utf-8')) # b'\xe4\xb8\xad\xe6\x96\x87'
b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
print(len('中文'), len('中文'.encode('utf-8'))) # 2, 6
print('Hi, %s, you have $%d.%2d-%02d %.2f %%' %
('Michael', 1000000, 3, 1, 3.1415926))
a = 'abc'
b = a.replace('a', 'A')
print(a, b)
import re
re.split(r'[\s\,\;]+', 'a,b;; c d') # ['a', 'b', 'c', 'd']
m = re.match(r'^(\d{3})-(\d{3,8})$', '010-12345')
m.group(0) # '010-12345'
m.group(1) # '010'
m.group(2) # '12345'
re_telephone = re.compile(r'^(\d{3})-(\d{3,8})$')
re_telephone.match('010-12345').groups()
# List
classmates = ['Michael', 'Bob', 'Tracy']
print(len(classmates)) # 3
print(classmates[-1]) # 'Tracy'
classmates.append('Adam') # ['Michael', 'Bob', 'Tracy', 'Adam']
classmates.insert(1, 'Jack') # ['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
classmates.pop() # 'Adam'
classmates.sort() # ['Bob', 'Jack', 'Michael', 'Tracy']
s = ['Apple', 123, True, 'python', 'java', ['asp', 'php'], 'scheme']
print(classmates)
# Advanced - Split
L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
print(L[:3]) # L[0:3] = ['Michael', 'Sarah', 'Tracy']
print(L[-2:]) # ['Bob', 'Jack']
print(L[-2:-1]) # ['Bob']
# Advanced - Generation
print([x * x for x in range(1, 11) if x % 2 == 0]) # [4, 16, 36, 64, 100]
print([m + n for m in 'ABC' for n in 'XYZ'])
# ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
print([s.lower() for s in L]) # ['hello', 'world', 'ibm', 'apple']
L2 = [s.lower() for s in L if isinstance(s, str)]
# lower case ['Hello', 'World', 18, 'Apple', None]
# Tuple: Tuple is fixed and const list.
classmates = ('Michael', 'Bob', 'Tracy')
t = (1,) # a tuple with only one element
# Generator: Generator is a list with pre-computed space
g = (x * x for x in range(10)) # <generator object>
# Generator can be defined using yield and function:
def fib(max):
n, a, b = 0, 0, 1
while n < max:
yield b
a, b = b, a + b
n = n + 1
return 'done'
for n in fib(6):
print(n)
# Using exception to catch the return value of generator
g = fib(6)
while True:
try:
x = next(g)
print('g:', x)
except StopIteration as e:
print('Generator return value:', e.value)
break
# Dict: dict is the (hash) map in C++, which finds and insert in O(1),
# but occupies lots of memory
d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
print(d['Michael']) # 95
print('Thomas' in d) # False
d.get('Thomas', -1) # -1, None if no parameter
d.pop('Bob')
keys = ['a', 'b', 'c']
vals = [1, 2, 3]
zipped = dict(zip(keys, vals))
# Set: Set is a set of keys without values:
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s1.add(4)
s1.remove(4)
print(s1 & s2) # {2, 3}
print(s1 | s2) # {1, 2, 3, 4}
# collections
from collections import namedtuple
## namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
Circle = namedtuple('Circle', ['x', 'y', 'r'])
# double linked list
from collections import deque
q = deque()
q.append('x')
q.appendleft('y')
from collections import defaultdict
# defaultdict
dd = defaultdict(lambda: 'N/A')
from collections import OrderedDict
od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
from collections import Counter
c = Counter() # subclass of dict
for ch in 'programming':
c[ch] = c[ch] + 1
c # Counter({'g': 2, 'm': 2, 'r': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'p': 1})
# Functional Programming
## High-order function
def add(x, y, f):
return f(x) + f(y)
add(-5, 6, abs)
## map/reduce
# https://ai.google/research/pubs/pub62
def sqr(x):
return x * x
r = map(sqr, [1, -2, 3, 4, -5, 6, -7, 8, 9]) # Iterator, a lazy list
list(r)
# reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
from functools import reduce
def add(x, y):
return x + y
reduce(add, [1, 3, 5, 7, 9]) # 13579
def str2int(s):
def fn(x, y):
return x * 10 + y
def char2num(s):
return {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9
}[s]
return reduce(fn, map(char2num, s))
# lambda function
def str2int2(s):
def char2num(s):
return {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9
}[s]
return reduce(lambda x, y: x * 10 + y, map(char2num, s))
# filter
# Filter acts like map, but removes the element depending on the return value, True or False.
def is_odd(n):
return n % 2 == 1
print(list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15]))) # [1, 5, 9, 15]
def not_empty(s):
return s and s.strip()
print(list(filter(not_empty, ['A', 'B', None, 'C', ' ']))) # ['A', 'B', 'C']
# Anonymous function
list(map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
def build(x, y):
return lambda: x * x + y * y
# Partial function
def int2(x, base=2):
return int(x, base)
import functools
int2 = functools.partial(int, base=2)
def log(text):
def decorator(func):
def wrapper(*args, **kw):
print('%s %s():' % (text, func.__name__))
return func(*args, **kw)
return wrapper
return decorator
# Object Oriented Programming
class Student(object):
__slots__ = ('name', 'age', 'score'
) # use tuple to defined the allowed properties
def __init__(self, name, score):
self.name = name
self.score = score # _Student__score
def print_score(self):
print('%s: %s' % (self.name, self.score))
bart = Student('Bart Simpson', 59)
bart.print_score()
isinstance(bart, Student)
type(123) # int
isinstance([1, 2, 3], (list, tuple))
dir('ABC') # list all the methods and properties of a variable
# Inherit
class Animal(object):
pass
# first level:
class Mammal(Animal):
pass
class Bird(Animal):
pass
# next level:
class Dog(Mammal):
pass
class Bat(Mammal):
pass
class Parrot(Bird):
pass
class Ostrich(Bird):
pass
# Enum
from enum import Enum
Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec'))
for name, member in Month.__members__.items():
print(name, '=>', member, ',', member.value)
from enum import Enum, unique
@unique
class Weekday(Enum):
Sun = 0
Mon = 1
Tue = 2
Wed = 3
Thu = 4
Fri = 5
Sat = 6
for name, member in Weekday.__members__.items():
print(name, '=>', member)
# Debug
try:
print('try...')
r = 10 / int('2')
print('result:', r)
except ValueError as e:
print('ValueError:', e)
except ZeroDivisionError as e:
print('ZeroDivisionError:', e)
else:
print('no error!')
finally:
print('finally...')
print('END')
assert x != 0, 'n is zero!'
import unittest
# if __name__ == '__main__':
# unittest.main()
class TestDict(unittest.TestCase):
def test_init(self):
d = dict(a=1, b='test')
self.assertEqual(d.a, 1)
self.assertEqual(d.b, 'test')
self.assertTrue(isinstance(d, dict))