-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonstudy.py
More file actions
295 lines (226 loc) · 7.34 KB
/
pythonstudy.py
File metadata and controls
295 lines (226 loc) · 7.34 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
import os, time, re
# get the path
print(os.getcwd())
# get file metainfo
metadata = os.stat("pythonstudy.py")
print(time.localtime(metadata.st_mtime))
a_list = [x**2 for x in range(10) if x % 2==0]
print(a_list)
phonePattern = re.compile(r'^(\d{4})-(\d{8})-(\D*)')
find = phonePattern.search("0398-44556677-").groups()
print(re.sub('[ove]', 'i', "Lofe"))
print(re.sub('([^aeiou])y$', r'\1ies', 'funny'))
temp = (a, b, c) = range(3)
linecount = 0
with open("pythonstudy.py") as demofile:
for line in demofile:
linecount+=1
def fib(max):
"""function's docstring"""
x, y = 0, 1
while x < max:
yield x
x, y = y, x+y
# for x in fib(10):
# print(x, end=" ")
'''
class FibIterator():
"""docstring for FibIterator"""
ins_count = 0;
def __init__(self, max):
super(FibIterator, self).__init__()
self.max = max
def __iter__(self):
self.a = 0
self.b = 1
return self
def __next__(self):
fib = self.a
if fib > self.max:
raise StopIteration
self.a, self.b = self.b, self.a + self.b
return fib
fibIter = FibIterator(10)
fibIt = FibIterator(100)
fibIt.ins_count = 3
print(fibIter.__class__.ins_count)
fibIt.__class__.ins_count = 6
print(fibIter.__class__.ins_count)
print(fibIter.ins_count)
print(fibIt.ins_count)
print(fibIt.__class__.ins_count)
'''
mystring = "How time flies!"
print(set(mystring))
gen_exp = (a for a in range(15) if a % 3 == 0)
print(tuple(gen_exp))
mylist = [12, 2, 13, 4]
print(sorted(["12", "2", "132", "4"], key=len))
print(eval('"A" + "B"'))
mydict = {'name':'GoT', 'producer':'HBO', 'lang':'eng'}
mydict.setdefault('date',2016) # makesure that key 'date' is initialized
mydict['date'] = 2010 # we dont need to check the 'date' is in the dict now
for k,v in sorted(mydict.items()):
print(k+' is: '+str(v))
import pprint
got = {'name':'GoT', 'producer':'HBO', 'lang':'eng'}
hoc = {'name':'House of Card', 'producer':'Netflix', 'lang':'eng'}
tvs = {}
tvs['got'] = got
tvs['hoc'] = hoc
pprint.pprint(tvs)
def double(arg):
arg = arg * 2
print(arg) # [1, 2, 1, 2]
def change(arg):
arg.append('data')
val = [1,2]
double(val)
print(val) #[1, 2]
change(val)
print(val) # [1, 2, 'data']
import sqlite3
def database_work():
# connect to the database, if file is not exist, this will create it
conn = sqlite3.connect('log.db')
# create a Cursor
cursor = conn.cursor()
showtables = """select name from sqlite_master where type='table' order by name"""
cursor.execute(showtables)
result = cursor.fetchall()
if len(result) == 0:
# execute a sql statement
cursor.execute('create table log (id INTEGER PRIMARY KEY AUTOINCREMENT, request varchar(50))')
else:
print(result)
# insert a data
cursor.execute("insert into log (request) values ('christmas')")
# close the Cursor
cursor.close()
# commit the transaction
conn.commit()
# close the connect
conn.close()
class CountFromBy(object):
"""docstring for CountFromBy"""
def __init__(self, v: int = 0, s: int=1) -> None:
super(CountFromBy, self).__init__()
self.val = v
self.step = s
def increase(self) -> None:
self.val += self.step
def __repr__(self) -> str:
return str(self.val)
def outter_fun():
def inner_print():
print('xxx')
print('return inner fun:')
return inner_print()
def show_fun(*args):
for arg in args:
print(arg)
def show_dict(**kwargs):
for k, v in kwargs.items():
print(k, v, sep='->', end=' ')
def show_any_args(*args, **kwargs):
if args:
for arg in args:
print(arg)
print()
if kwargs:
for k, v in kwargs.items():
print(k, v, sep='->', end=' ')
from functools import wraps
def decorator_name(func):
@wraps(func)
def wrapper(*args, **kwargs):
# 1. Code to execute before calling the decorated function.
# 2. Call the decorated function as required, returning its results if needed.
return func(*args, **kwargs)
# 3. Code to execute Instead of calling the decorated function.
return wrapper
class WorldNotWork(Exception):
pass
def test_world_not_work():
try:
raise WorldNotWork('The world has been corrupt....')
except WorldNotWork as e:
print("Some error", str(e)) #Some error The world has been corrupt....
def read_file():
try:
with open('xxx.txt') as fd:
file_data = fd.read()
print(file_data)
except FileNotFoundError:
print('file is missing.')
except PermissionError:
print('not allowed.')
except Exception as err:
print('some error occurred:', str(err))
def test_return_list():
# text = 'key,value,keys' # ValueError: too many values to unpack
text = 'key,value'
k, v = text.split(',')
print(k,v)
from datetime import datetime
def convert2ampm(time24: str) -> str:
return datetime.strptime(time24, '%H:%M').strftime('%I:%M %p')
def gen_dest(time_table:dict):
for k, v in time_table.items():
yield v
print(k) # this will be execute next time
import requests
def gen_from_urls(urls:tuple)->tuple:
# use generator expression to request each url
for resp in (requests.get('http://'+url) for url in urls):
yield len(resp.content), resp.status_code, resp.url
def test_generator():
fts = {'10:00':'Hongkong', '08:00':'NewYork', '16:00':'Hongkong', '12:00':'Taipei',}
for v in gen_dest(fts):
print(v)
demo = [print(v) for v in gen_dest(fts)]
when = {}
for dest in set(fts.values()):
templist = []
for k, v in fts.items():
if v == dest:
templist.append(k)
when[dest] = templist
print(when)
when2 = { dest : [k for k, v in fts.items() if v == dest] for dest in set(fts.values()) }
print(when2)
#list comprehension
for i in [x*2 for x in [1, 2, 3, 4]]:
print(i)
print('------------------------')
#generator
for i in (x*2 for x in [1, 2, 3, 4]):
print(i)
urls = ('www.baidu.com', 'www.douban.com', 'www.qq.com')
# use dict comprehension to iterator the generator function
# the `_`underscore is Python's defualt variable name, tells the code to ignore the second value
urls_res = { url : size for size, _, url in gen_from_urls(urls) }
print(urls_res)
import threading
import asyncio
@asyncio.coroutine
def hello():
print('Hello world! (%s)' % threading.currentThread())
yield from asyncio.sleep(3)
print('Hello again! (%s)' % threading.currentThread())
def asynDemo():
loop = asyncio.get_event_loop()
tasks = [hello(), hello()]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
if __name__ == '__main__':
value = 3.14
tag = 'is for circle'
msg = 'Value %2.2f %s' % (value, tag)
print(msg)
msg = 'Value {} which {}'.format(value, tag)
print(msg)
fts = {'10:00':'Hongkong', '08:00':'NewYork', '16:00':'Hongkong', '12:00':'Taipei',}
for k in sorted(fts, key=fts.get, reverse=True):
print(k, '->', fts[k])
asynDemo()