-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiary5.txt
510 lines (441 loc) · 12.3 KB
/
Diary5.txt
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
【2018-07-20】Python with
// https://www.cnblogs.com/DswCnblog/p/6126588.html
// https://www.ibm.com/developerworks/cn/opensource/os-cn-pythonwith/index.html
紧跟with后面的语句被求值后 返回对象的__enter__()方法被调用 这个方法的返回值将被赋值给as后面的变量
当with后面的代码块全部被执行完之后 将调用前面返回对象的__exit__()方法
例:
class MyClass(object):
def __enter__(self):
print "__enter__"
return "enter"
def __exit__(self, type, value, trace):
print "__exit__"
with MyClass() as message:
print message
输出:
__enter__
enter
__exit__
【2018-08-23】【1】Python map与reduce
map()是 Python 内置的高阶函数 它接收一个函数 f 和一个 list
并通过把函数 f 依次作用在 list 的每个元素上 得到一个新的 list 并返回
例:
myList = [1, 2, 3, 4, 5, 6, 7]
myList2 = [7, 6, 5, 4, 3, 2, 1]
# myList myList2 的元素分别作为lambda的输入
mul_list = map(lambda x, y: x * y, myList, myList2)
print mul_list
def sqrt(x):
import math
return math.sqrt(x)
# myList 的元素 作为 sqrt的输入
sqrt_list = map(sqrt, myList)
print sqrt_list
输出:
[7, 12, 15, 16, 15, 12, 7]
[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907]
reduce() 函数会对参数序列中元素进行累积
函数将一个数据集合(链表 元组等)中的所有数据进行下列操作
用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作
得到的结果再与第三个数据用 function 函数运算 最后得到一个结果
例:
myList = [1, 2, 3, 4, 5, 6, 7]
def add(x, y):
return x + y
list_sum = reduce(add, myList)
print list_sum
输出:
28
【2018-08-23】【2】Python *与**
例:
加了星号*的变量名会存放所有未命名的可变参数
def func(x, *args):
print "x:", x
# args 为一个 tuple
print "args:", args
func(10, 20, 30)
输出:
x: 10
args: (20, 30)
例:
加了星号**的变量名会存放所有未命名的关键字参数
def func(x, **kwagrs):
print "x:", x
# kwagrs 为一个 dict
print "kwagrs:", kwagrs
func(10, y = 20, z = 30)
输出:
x: 10
args: {'y': 20, 'z': 30}
【2018-08-23】【3】list 另类赋值
list = [1, 2, 3, 4, 5]
# 遍历 list 对每一个元素 x 用 x * x 作用生成一个新的 sqr_list
sqr_list = [x * x for x in list]
print sqr_list
输出:
[1, 4, 9, 16, 25]
【2018-08-23】【4】sum 遍历一个可迭代对象并求和
例:
list = [1, 2, 3, 4, 5]
sum_res = sum(list)
print sum_res
输出:
15
【2018-10-08】Android Studio
AndroidManifest.xml位置
Android
项目
manifests
AndroidManifest.xml
.gradle位置
Project
项目
build.gradle
build.gradle
settings.gradle
【2018-10-12】Python 字典值修改
dic = {'a':0, 'b':0}
for key, val in dic.items():
# 并没有修改字典中的值
# val = 1
# 这样才真正修改了
dic[key] = 1
print dic
输出:
{'a': 1, 'b': 1}
【2018-10-31】【1】Python变量修改
idx = 0
# 遍历过程中实际上修改了上面定义的idx的值
for idx in xrange(10):
pass
print idx
输出:
9
【2018-10-31】【2】Python列表判空
def check(item):
if item:
print 1
else:
print 0
item = [1, 2]
check(item)
item = []
check(item)
输出:
1
0
【2018-11-15】【1】Python import
1.函数内import module结束函数调用后module不会释放
例:
-->main.py(顶层脚本)
def test():
import test
test.val = 1
test()
import test
print test.val
-->test.py
val = 0
输出:
1
2.脚本不会被import多于1次 对于顶层脚本被import后__name__将不再是__main__(与顶层脚本不是同一个脚本)
例:
1.
-->main.py(顶层脚本)
print "module:", __name__
import test
-->test.py
print "module:", __name__
import main
-->test2.py
print "module:", __name__
import test
输出:
module: __main__
module: test
module: test2
2.
-->main.py(顶层脚本)
print "module:", __name__
import test
-->test.py
print "module:", __name__
import main
输出:
module: __main__
module: test
module: main
【2018-11-15】【2】Python判空
Python对象判空 Python类有__nonzero__方法用于判断对象BOOL值 通常用于if语句中
对于特殊类型 list dict tuple 可以直接用if语句判断是否为空
例:
def judge(obj):
if obj:
print "obj is True"
else:
print "obj is False"
class Test(object):
def __init__(self, val):
self.val = val
def __nonzero__(self):
return self.val
t = Test(False)
judge(t)
t = Test(True)
judge(t)
dict = {}
list = []
tuple = ()
if not dict:
print "empty dict"
if not list:
print "empty list"
if not tuple:
print "empty tuple"
输出:
obj is False
obj is True
empty dict
empty list
empty tuple
【2018-11-21】【1】Python filter关键字
listA = [1,2,3,4,5,6,7,8,9,10]
listB = filter(lambda x: x % 2 == 0, listA)
print listB
输出:
[2, 4, 6, 8, 10]
【2018-11-21】【2】Python list comprehension
# https://www.cnblogs.com/dadadechengzi/p/6292156.html
list comprehension
[expr for iter_var in iterable ] or [expr for iter_ in iterable if cond_expr]
例:
listA = [1,2,3,4,5,6,7,8,9,10]
listB = [e**2 for e in listA]
print listB
listB = [e for e in listA if e % 2 == 0]
print listB
listB = [3 for e in listA]
print listB
输出:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[2, 4, 6, 8, 10]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
【2018-11-22】Python generator expression
# https://www.cnblogs.com/dadadechengzi/p/6292156.html
注意 generator 只能遍历一次
generator expression
(expr for iter_var in iterable) or (expr for iter_var in interable if cond_expr)
例:
listA = [1,2,3,4,5,6,7,8,9,10]
genA = (e**2 for e in listA)
print genA
for e in genA:
print e
【2018-12-12】Python数据类型与字节流互转
# https://www.cnblogs.com/volcao/p/8807507.html
struct.pack将Python数据转化为字节流
struct.unpack将字节流转化为Python数据
例:
import struct
with open('test.bin', 'wb') as f:
data = struct.pack('ccii', '0', '1', 200, 300)
f.write(data)
with open('test.bin', 'rb') as f:
data = f.read()
data = struct.unpack('ccii', data)
print data
输出:
('0', '1', 200, 300)
【2019-04-13】【1】Python Pack Unpack
# https://www.cnblogs.com/summer-cool/p/3885384.html
python中还允许包裹方式的参数传递 这未不确定参数个数和参数类型的函数调用提供了基础
def f(*a,**b)
Pack:
包裹参数传递的实现是在定义函数时在形参前面加上*或**
*所对应的形参(如上面的a)会被解释为一个元组(tuple而**所对应的形参(如上面的b))会被解释为一个字典
中规定非关键字传递的变量必须写在关键字传递变量的前面 所以混合使用*和**时肯定时*形参在**形参的前面
常规形参要在非常规形参* **之前
Unpack:
把元组(列表)或字典作为参数传入时如果要适配包裹形式的形参定义
按照元组(列表)用* 字典用**的方式"解包裹"传递即可
例:
def test_pack(*tuple, **dircetory):
print "------>"
print tuple
print dircetory
print "<------"
def test_pack_swallower(swallower, *tuple, **dircetory):
print "------>"
print swallower
print tuple
print dircetory
print "<------"
print "test_pack"
test_pack(1,2,3,a=4,b=5)
test_pack_swallower(1,2,3,a=4,b=5)
print "test unpack"
list = [1,2,3]
tuple = (2,3,4)
dircetory = {'a':4, 'b':5}
test_pack(*list, **dircetory)
test_pack_swallower(1, *tuple, **dircetory)
输出:
test_pack
------>
(1, 2, 3)
{'a': 4, 'b': 5}
<------
------>
1
(2, 3)
{'a': 4, 'b': 5}
<------
test unpack
------>
(1, 2, 3)
{'a': 4, 'b': 5}
<------
------>
1
(2, 3, 4)
{'a': 4, 'b': 5}
<------
【2019-04-13】【2】Python 装饰器
不带参数的装饰器要求接受一个callable对象 并返回一个callable对象
带参数的装饰起要求接受装饰器参数 返回一个callable对象
该对象接受一个callable对象 并返回一个callable对象
例:
def test_org(message):
print "message:", message
test = test_org
test("without wapper")
print "[wrapper function]"
def inwapper(func):
def ret(*args, **kwargs):
print inwapper, args, kwargs
return func(*args, **kwargs)
return ret
# test = inwapper(test_org)
@inwapper
def test(message):
print "message:", message
test("with wapper")
def outwapper(*args, **kwargs):
print outwapper, args, kwargs
def inwapper(func):
def ret(*args, **kwargs):
print inwapper, args, kwargs
return func(*args, **kwargs)
return ret
return inwapper
# test = outwapper("outwapper")(test_org)
@outwapper("outwapper")
def test(message):
print "message:", message
test("with outter wapper")
class innerwraper_cls(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print innerwraper_cls, args, kwargs
return self.func(*args, **kwargs)
print "[wrapper cls]"
# test = innerwraper_cls(test_org)
@innerwraper_cls
def test(message):
print "message:", message
test("with wraper_cls")
class outterwraper_cls(object):
def __init__(self, *args, **kwargs):
print outterwraper_cls, args, kwargs
def __call__(self, func):
return innerwraper_cls(func)
# test = outterwraper_cls("outwapper")(test_org)
@outterwraper_cls("outwapper cls")
def test(message):
print "message:", message
test("with outterwraper_cls")
输出:
message: without wapper
[wrapper function]
<function inwapper at 0x02533530> ('with wapper',) {}
message: with wapper
<function outwapper at 0x025335F0> ('outwapper',) {}
<function inwapper at 0x025337F0> ('with outter wapper',) {}
message: with outter wapper
[wrapper cls]
<class '__main__.innerwraper_cls'> ('with wraper_cls',) {}
message: with wraper_cls
<class '__main__.outterwraper_cls'> ('outwapper cls',) {}
<class '__main__.innerwraper_cls'> ('with outterwraper_cls',) {}
message: with outterwraper_cls
【2019-05-02】vim
1.[Setup vim]
sudo apt install vim
2.[Setup User Configure]
cd to userhome first
touch .vimrc
vim .vimrc
set up your user configure in .vimrc like:
set number
syntax on
set tabstop=4
set autoindent
after that you can check your configure in vim by inputting :echo $MYVIMRC
3.[Some hotkey]
3.1 i(Get into insert mode)
3.2 w(Go to next word)
3.3 b(Go back to previous word)
3.4 0(Go to the head of the line)
3.5 $(Go to the end of the line)
3.6 G(Go to the last line)
3.7 :w(Save the file)
3.8 :q!(Quit without save the file)
3.9 :wq(Save and quit)
3.10 a(Get into insert mode to append data after your current select letter)
3.11 A(Get into insert mode to append data in the end of you current select row)
3.12 v(Get into character visual mode)
3.13 V(Get into row visual mode)
3.14 Control v(Get into blockwise visual mode)
3.15 y after getting into visual mode(Copy the current selection)
3.16 d after getting into visual mode(Delete the current selection)
3.17 p(Paste the current selection)
3.18 u(Undo)
3.19 Ctrl + r(Redo)
3.20 ngg or nG or :n(Gogo to certain row, n for row number)
3.20 dd(delete[or cut] one row) d2d(delete[or cut] two row) and so on
3.21 yy(copy one row) y2y(copy two row) and so on
【2019-05-03】bash
[Navigation]
1.cd(change directory)
2.pushd(change directory and push directory into the stack)
3.popd(change directory to the diectory on the top of the stack and pop it from stack)
4.ls(list all the file and directory on current directory)
5.pwd(print the current directory)
6.which(locate the path of the command)
7.history(show bash command in your history)
8.locate(locate the file in your file system)
9.sudo updatedb(update the datebase of locate command)
10.file(show the file type)
[Get help]
1.whatis(show a short discription of what a command do)
2.apropos(search discription of the command)
3.man(show the manual of a command)
[Work with file]
1.mkdir(create a directory)
2.touch(create a file which not exists not or update the time stamp of the file which exists)
3.cp(copy file or directory)
4.mv(rename a file or move a file)
5.rm(remove file or directory)
6.rmdir(remove empty directory)
7.wc(show the rows,words,btyes count of file)
[Text]
1.cat(concatenate file and print on standard output)
2.more/less(file perusal filter)
3.nano(text editor)
4.tr(replace,compress or delete string from standard input)
[Redirection and piping]
1.>(indicate the program output into a file to REPLACE previous data [2> to indicate stderr output,> or 1> indicate the stdout output.https://ryanstutorials.net/linuxtutorial/piping.php ])
2.>>(indicate the program output into a file to APPEND previous data)
3.<(indicate the file content output into a program)
4.|(indicate the program output into another program)