Skip to content

Commit 1d6a18b

Browse files
committed
code and pdg
1 parent 437c9e8 commit 1d6a18b

19 files changed

+470
-22
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
259259

260260
## [six](content/six.md)
261261

262+
## [traceback](content/traceback.md)
263+
264+
## [code](content/code.md)
265+
262266
## [tools](content/tools.md)
263267

264268
## [Other_thing](content/other_thing.md)

code/bunch.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ if __name__ == '__main__':
4747
4848
```
4949

50+
快速创建一个对象,当你有一个字典,而需要一个对象的时候,使用 DotDict 是最简单的方式,特别是当你的字典键的数量比较多的时候。
51+
5052
而 bunch 的实现是这样的
5153

5254
```

code/card.jpeg

5.34 KB
Loading

code/code_terminal.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# doding=utf-8
2+
3+
import code
4+
5+
name = "windard"
6+
7+
# console = code.InteractiveConsole(locals())
8+
# console.interact()
9+
10+
code.interact(local=locals())

code/image_background.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
# coding=utf-8
3+
4+
from PIL import Image
5+
6+
im = Image.open('card.jpeg')
7+
r, g, b = im.split()
8+
h,w = im.size
9+
print h,w
10+
11+
# 创建一个新的 r 通道分量, 注意 mode 值为 'L'
12+
# 255 是白
13+
# 0 是黑
14+
n = Image.new('L', (h, w), color=0)
15+
16+
for i in range(h):
17+
for j in range(w):
18+
pixel = im.getpixel((i, j))
19+
if all(map(lambda x:x>210, pixel)):
20+
im.putpixel((i, j), (0, 0, 255))
21+
# im = Image.merge('RGB', (n, n, b))
22+
im.show()
23+

code/image_gaussian.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from PIL import Image, ImageFilter
4+
5+
6+
img = Image.open("card.jpeg")
7+
im = img.filter(ImageFilter.GaussianBlur(25))
8+
9+
im.show()

code/socket_udp_bind.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ def main(host, port):
2020

2121
udpsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
2222
udpsock.bind((host, port))
23-
threading.Thread(target=server, args=(udpsock,)).start()
23+
thread = threading.Thread(target=server, args=(udpsock,))
24+
thread.setDaemon(True)
25+
thread.start()
2426

2527
import pdb
2628
pdb.set_trace()
2729

28-
# while 1:
29-
# data = raw_input(">")
30-
# if not data:
31-
# break
32-
# udpsock.sendto(data, (host, port))
33-
# data, addr = udpsock.recvfrom(bufsize)
34-
# if not data:
35-
# break
36-
# print data
30+
while 1:
31+
data = raw_input(">")
32+
if not data:
33+
break
34+
udpsock.sendto(data, (host, port))
3735

3836
udpsock.close()
3937

code/terminal_color.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# coding=utf-8
2+
3+
class bcolors:
4+
BLUE = '\033[95m'
5+
WHITE = '\033[94m'
6+
BLACK = '\033[92m'
7+
GRAY = '\033[93m'
8+
RED = '\033[91m'
9+
ENDC = '\033[0m'
10+
11+
12+
print bcolors.BLUE + "First color is blue " + bcolors.ENDC
13+
print bcolors.WHITE + "Second is white" + bcolors.ENDC
14+
print bcolors.BLACK + "Second is black" + bcolors.ENDC
15+
print bcolors.GRAY + "Second is gray" + bcolors.ENDC
16+
print bcolors.RED + "Second is red" + bcolors.ENDC

code/traceback_demo.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# coding=utf-8
2+
import traceback
3+
from flask import Flask
4+
5+
6+
app = Flask(__name__)
7+
8+
9+
@app.route('/')
10+
def index():
11+
traceback.print_stack()
12+
return 'hello world'
13+
14+
15+
def main():
16+
app.run()
17+
18+
19+
if __name__ == '__main__':
20+
main()

code/tracestack_flask.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
import signal
3+
import pdb
4+
from flask import Flask
5+
6+
app = Flask(__name__)
7+
signal.signal(signal.SIGUSR2, lambda num,stack: pdb.set_trace())
8+
9+
10+
@app.route('/')
11+
def index():
12+
return json.dumps({"name":"windard"})
13+
14+
15+
def main():
16+
app.run()
17+
18+
19+
if __name__ == '__main__':
20+
data = json.dumps({"name":"windard"})
21+
main()

code/tracestack_signal.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
import traceback
3+
import signal
4+
import socket
5+
import time
6+
7+
8+
host = "127.0.0.1"
9+
port = 8081
10+
11+
# s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
12+
# s.bind((host,port))
13+
# s.listen(5)
14+
15+
print "Server is running on port %s Press Ctrl-C to stop"%port
16+
signal.signal(signal.SIGUSR2, lambda num,stack:traceback.print_stack(stack))
17+
18+
19+
while 1:
20+
time.sleep(10)
21+
print '10 seconds sheep'
22+
# clientsock, clientaddr = s.accept()
23+
# print "Welcome from %s:%s"%(clientaddr[0],clientaddr[1])
24+
# while 1:
25+
# request = clientsock.recv(1024)
26+
# print "Received From client : %r" % request
27+
# if not request:
28+
# break
29+
# clientsock.send("Hello client:%s" % request)

content/Image.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
这个是专门用来处理图片的,只要你在上面了安装了pil图像处理库,就会自带这个库的。
44

5+
在开始图片之前,我们先来认识一下三原色和三基色。
6+
- 三原色一般是指颜料三原色红黄蓝,组合颜色是黑色
7+
- 三基色一般是指光学三原色红绿蓝,组合颜色是白色, RGB
8+
9+
![source_color](images/source_color.png)
10+
511
#### 基本操作
612

713
```python
@@ -140,4 +146,47 @@ im2 = Image.open('image2.png')
140146
im3 = ImageChops.invert(im2)
141147
Image.blend(im1,im3,0.5).show()
142148
143-
```
149+
```
150+
151+
### 高斯模糊
152+
153+
```
154+
# -*- coding: utf-8 -*-
155+
156+
from PIL import Image, ImageFilter
157+
158+
159+
img = Image.open("card.jpeg")
160+
im = img.filter(ImageFilter.GaussianBlur(25))
161+
162+
im.show()
163+
164+
```
165+
166+
### 切换证件照背景色
167+
168+
```
169+
# -*- coding: utf-8 -*-
170+
# coding=utf-8
171+
172+
from PIL import Image
173+
174+
im = Image.open('card.jpeg')
175+
r, g, b = im.split()
176+
h,w = im.size
177+
print h,w
178+
179+
# 创建一个新的 r 通道分量, 注意 mode 值为 'L'
180+
# 255 是白
181+
# 0 是黑
182+
n = Image.new('L', (h, w), color=0)
183+
184+
for i in range(h):
185+
for j in range(w):
186+
pixel = im.getpixel((i, j))
187+
if all(map(lambda x:x>210, pixel)):
188+
im.putpixel((i, j), (0, 0, 255))
189+
# im = Image.merge('RGB', (n, n, b))
190+
im.show()
191+
192+
```

content/code.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## code
2+
3+
提供 python 交互执行解释器
4+
5+
可以使用 `code.interact()` 建立一个 交互式 shell ,或者使用 `code.InteractiveConsole` 自己构建
6+
7+
```
8+
# doding=utf-8
9+
10+
import code
11+
12+
name = "windard"
13+
14+
# console = code.InteractiveConsole(locals())
15+
# console.interact()
16+
17+
code.interact(local=locals())
18+
19+
```
20+
21+
看源代码 `interact` 的实现
22+
23+
```
24+
def interact(banner=None, readfunc=None, local=None):
25+
"""Closely emulate the interactive Python interpreter.
26+
27+
This is a backwards compatible interface to the InteractiveConsole
28+
class. When readfunc is not specified, it attempts to import the
29+
readline module to enable GNU readline if it is available.
30+
31+
Arguments (all optional, all default to None):
32+
33+
banner -- passed to InteractiveConsole.interact()
34+
readfunc -- if not None, replaces InteractiveConsole.raw_input()
35+
local -- passed to InteractiveInterpreter.__init__()
36+
37+
"""
38+
console = InteractiveConsole(local)
39+
if readfunc is not None:
40+
console.raw_input = readfunc
41+
else:
42+
try:
43+
import readline
44+
except ImportError:
45+
pass
46+
console.interact(banner)
47+
48+
```
49+
50+
51+
在 code 中的交互式命令行不能改变样式,也没有自动补全,但是在默认的 python 交互式命令行中可以修改样式。
52+
53+
54+
```
55+
>>> import sys
56+
>>> sys.ps1
57+
'>>> '
58+
>>> sys.ps2
59+
'... '
60+
>>> sys.ps1 = '--------'
61+
--------sys.ps2 = '********'
62+
--------def foo():
63+
******** pass
64+
********
65+
```
66+
67+

content/colorama.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,26 @@ print_format_table()
123123

124124
![color_row](images/color_row.png)
125125

126+
127+
```
128+
# coding=utf-8
129+
130+
class bcolors:
131+
BLUE = '\033[95m'
132+
WHITE = '\033[94m'
133+
BLACK = '\033[92m'
134+
GRAY = '\033[93m'
135+
RED = '\033[91m'
136+
ENDC = '\033[0m'
137+
138+
139+
print bcolors.BLUE + "First color is blue " + bcolors.ENDC
140+
print bcolors.WHITE + "Second is white" + bcolors.ENDC
141+
print bcolors.BLACK + "Second is black" + bcolors.ENDC
142+
print bcolors.GRAY + "Second is gray" + bcolors.ENDC
143+
print bcolors.RED + "Second is red" + bcolors.ENDC
144+
```
145+
126146
## colorama
127147

128148
上面的方法只能在 linux 下有效,如果想要跨平台的话,可能就需要这个库了

content/images/source_color.png

69.5 KB
Loading

content/pdb.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,32 @@ import pdb
99
pdb.set_trace()
1010
```
1111

12-
然后就可以了。
12+
然后即可使用当前位置的变量,环境和堆栈等相关信息。
13+
14+
pdb 中的命令
15+
16+
0. help [command] 显示相关语句帮助
17+
1. n|next 下一步,不进入函数内部
18+
7. j|jump [lineno] 跳转至某一行
19+
2. c|continue 继续运行,没有断点则退出 pdb 调试
20+
3. q|exit|quit 报错退出
21+
4. l|list 列出上下文相关代码
22+
5. w|where 显示堆栈信息
23+
6. b|break [lineno] 打断点
24+
8. cl|clear 清除所有断点
25+
9. a|args 当前环境变量
26+
10. r|return 退出当前函数
27+
11. whatis [arg] 查看变量类型
28+
12. s|step 下一步,进入函数内部
29+
30+
还可以使用 `python -m pdb file.py` 使用 pdb 调试代码,默认停在第一行
1331

1432
2018-09-15
1533

1634
需注意两点
1735
1. 在 pdb 中不能创建新的变量
1836
2. 在 pdb 中不能给已有的变量赋值
37+
38+
2018-09-16
39+
40+
可以设置变量,但是 a,b,c 都被占用了,所以不能直接设置

0 commit comments

Comments
 (0)