Skip to content

Commit aafd172

Browse files
author
root
committed
test
1 parent c5ad116 commit aafd172

File tree

15 files changed

+329
-0
lines changed

15 files changed

+329
-0
lines changed

2017-04-28/doctest/doc.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Dict(dict):
2+
'''
3+
Simple dict but also support access as x.y style.
4+
5+
>>> d1 = Dict()
6+
>>> d1['x'] = 100
7+
>>> d1.x
8+
100
9+
>>> d1.y = 200
10+
>>> d1['y']
11+
200
12+
>>> d2 = Dict(a=1, b=2, c='3')
13+
>>> d2.c
14+
'3'
15+
>>> d2['empty']
16+
Traceback (most recent call last):
17+
...
18+
KeyError: 'empty'
19+
>>> d2.empty
20+
Traceback (most recent call last):
21+
...
22+
AttributeError: 'Dict' object has no attribute 'empty'
23+
'''
24+
def __init__(self, **kw):
25+
super(Dict, self).__init__(**kw)
26+
27+
def __getattr__(self, key):
28+
try:
29+
return self[key]
30+
except KeyError:
31+
raise AttributeError(r"'Dict' object has no attribute '%s'" % key)
32+
33+
def __setattr__(self, key, value):
34+
self[key] = value
35+
36+
if __name__=='__main__':
37+
import doctest
38+
doctest.testmod()

2017-04-28/error_debug/debugging.txt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
n的调试器pdb,让程序以单步方式运行,可以随时查看运行状态。我们先准备好程序:
3+
4+
# err.py
5+
s = '0'
6+
n = int(s)
7+
print 10 / n
8+
然后启动:
9+
10+
$ python -m pdb err.py
11+
> /Users/michael/Github/sicp/err.py(2)<module>()
12+
-> s = '0'
13+
以参数-m pdb启动后,pdb定位到下一步要执行的代码-> s = '0'。输入命令l来查看代码:
14+
15+
(Pdb) l
16+
1 # err.py
17+
2 -> s = '0'
18+
3 n = int(s)
19+
4 print 10 / n
20+
[EOF]
21+
输入命令n可以单步执行代码:
22+
23+
(Pdb) n
24+
> /Users/michael/Github/sicp/err.py(3)<module>()
25+
-> n = int(s)
26+
(Pdb) n
27+
> /Users/michael/Github/sicp/err.py(4)<module>()
28+
-> print 10 / n
29+
任何时候都可以输入命令p 变量名来查看变量:
30+
31+
(Pdb) p s
32+
'0'
33+
(Pdb) p n
34+
0
35+
输入命令q结束调试,退出程序:
36+
37+
(Pdb) n
38+
ZeroDivisionError: 'integer division or modulo by zero'
39+
> /Users/michael/Github/sicp/err.py(4)<module>()
40+
-> print 10 / n
41+
(Pdb) q
42+
这种通过pdb在命令行调试的方法理论上是万能的,但实在是太麻烦了,如果有一千行代码,要运行到第999行得敲多少命令啊。还好,我们还有另一种调试方法。
43+
44+
pdb.set_trace()
45+
46+
这个方法也是用pdb,但是不需要单步执行,我们只需要import pdb,然后,在可能出错的地方放一个pdb.set_trace(),就可以设置一个断点:
47+
48+
# err.py
49+
import pdb
50+
51+
s = '0'
52+
n = int(s)
53+
pdb.set_trace() # 运行到这里会自动暂停
54+
print 10 / n
55+
运行代码,程序会自动在pdb.set_trace()暂停并进入pdb调试环境,可以用命令p查看变量,或者用命令c继续运行:
56+
57+
$ python err.py
58+
> /Users/michael/Github/sicp/err.py(7)<module>()
59+
-> print 10 / n
60+
(Pdb) p n
61+
0
62+
(Pdb) c
63+
Traceback (most recent call last):
64+
File "err.py", line 7, in <module>
65+
print 10 / n
66+
ZeroDivisionError: integer division or modulo by zero
67+
这个方式比直接启动pdb单步调试效率要高很多,但也高不到哪去。

2017-04-28/error_debug/error.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# err.py
2+
import logging
3+
logging.basicConfig(level=logging.INFO)
4+
s = '0'
5+
n = int(s)
6+
logging.info('n = %d' % n)
7+
print 10 / n

2017-04-28/error_debug/logging.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
写程序最痛苦的事情莫过于调试,程序往往会以你意想不到的流程来运行,你期待执行的语句其实根本没有执行,这时候,就需要调试了。
2+
3+
虽然用IDE调试起来比较方便,但是最后你会发现,logging才是终极武器。

2017-04-28/exception_kind/err.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def foo(s):
4+
n = int(s)
5+
return 10 / n
6+
def bar(s):
7+
try:
8+
return foo(s)*2
9+
except StandardError,e:
10+
print 'Error!'
11+
raise
12+
13+
14+
def main():
15+
bar('0')
16+
17+
18+
main()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
if __name__=='__main__':
4+
5+
try:
6+
print 'try...'
7+
r = 10 /0
8+
print 'result:',r
9+
except ZeroDivisionError,e:
10+
print 'except:',e
11+
print 'hello'
12+
finally:
13+
print 'finally...'
14+
print 'END'
15+
16+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
在程序运行过程中,总会遇到各种各样的错误。
2+
有的错误是程序编写有问题造成的,比如本来应该输出整数结果
3+
4+
输出了字符串, 这种错误我们通常称之为bug, bug是必须修复的/
5+
有的错误是用户输入造成的,比如让用户输入email地址,结果得到一个空字符串,这种错误可以通过检查用户输入来做相应的处理。
6+
还有一类错误是完全无法在程序运行过程中预测的,比如写入文件的时候,磁盘满了,写不进去了,或者从网络抓取数据,网络突然断掉了。这类错误也称为异常,在程序中通常是必须处理的,否则,程序会因为各种问题终止并退出。
7+
ython内置了一套异常处理机制,来帮助我们进行错误处理。
8+
此外,我们也需要跟踪程序的执行,查看变量的值是否正确,这个过程称为调试。Python的pdb可以让我们以单步方式执行代码。
9+
最后,编写测试也很重要。有了良好的测试,就可以在程序修改后反复运行,确保程序输出符合我们编写的测试。
10+
11+
高级语言通常都内置了一套try...except...finally...
12+

2017-04-28/stmp_text/1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
smtp.qiye.163.com
2+
smtp.163.com

2017-04-28/stmp_text/cookies.dat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#LWP-Cookies-2.0

2017-04-28/stmp_text/send.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from email import encoders
4+
from email.header import Header
5+
from email.mime.text import MIMEText
6+
from email.utils import parseaddr, formataddr
7+
import smtplib
8+
9+
def _format_addr(s):
10+
name, addr = parseaddr(s)
11+
return formataddr(( \
12+
Header(name, 'utf-8').encode(), \
13+
addr.encode('utf-8') if isinstance(addr, unicode) else addr))
14+
15+
from_addr = raw_input('From: ')
16+
password = raw_input('Password: ')
17+
to_addr = raw_input('To: ')
18+
smtp_server = raw_input('SMTP server: ')
19+
20+
msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
21+
msg['From'] = _format_addr(u'Python爱好者 <%s>' % from_addr)
22+
msg['To'] = _format_addr(u'管理员 <%s>' % to_addr)
23+
msg['Subject'] = Header(u'来自SMTP的问候……', 'utf-8').encode()
24+
25+
server = smtplib.SMTP(smtp_server, 25)
26+
server.set_debuglevel(1)
27+
server.login(from_addr, password)
28+
server.sendmail(from_addr, [to_addr], msg.as_string())
29+
server.quit()

2017-04-28/stmp_text/sendM.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from email import encoders
4+
from email.header import Header
5+
from email.mime.text import MIMEText
6+
from email.mime.base import MIMEBase
7+
from email.mime.multipart import MIMEMultipart
8+
from email.utils import parseaddr, formataddr
9+
10+
import smtplib
11+
12+
def _format_addr(s):
13+
name, addr = parseaddr(s)
14+
return formataddr(( \
15+
Header(name, 'utf-8').encode(), \
16+
addr.encode('utf-8') if isinstance(addr, unicode) else addr))
17+
18+
from_addr = raw_input('From: ')
19+
password = raw_input('Password: ')
20+
to_addr = raw_input('To: ')
21+
smtp_server = raw_input('SMTP server: ')
22+
23+
msg = MIMEMultipart()
24+
msg['From'] = _format_addr(u'康晓宇 <%s>' % from_addr)
25+
msg['To'] = _format_addr(u'kehai <%s>' % to_addr)
26+
msg['Subject'] = Header(u'2017年04月第04周工作总结【康晓宇】','utf-8').encode()
27+
28+
# add MIMEText:
29+
msg.attach(MIMEText('2017年04月第04周工作总结','plain', 'utf-8'))
30+
31+
# add file:
32+
with open('/root/report/2017年04月第04周工作总结【康晓宇】.xlsx','rb') as f:
33+
mime = MIMEBase('image', 'png', filename='test.png')
34+
mime.add_header('Content-Disposition', 'attachment', filename='test.xlsx')
35+
mime.add_header('Content-ID', '<0>')
36+
mime.add_header('X-Attachment-Id', '0')
37+
mime.set_payload(f.read())
38+
encoders.encode_base64(mime)
39+
msg.attach(mime)
40+
41+
server = smtplib.SMTP(smtp_server, 25)
42+
server.set_debuglevel(1)
43+
server.login(from_addr, password)
44+
server.sendmail(from_addr, [to_addr], msg.as_string())
45+
server.quit()

2017-04-28/stmp_text/sendReport.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from email import encoders
4+
from email.header import Header
5+
from email.mime.text import MIMEText
6+
from email.mime.base import MIMEBase
7+
from email.mime.multipart import MIMEMultipart
8+
from email.utils import parseaddr, formataddr
9+
import smtplib
10+
11+
def _format_addr(s):
12+
name, addr = parseaddr(s)
13+
return formataddr(( \
14+
Header(name, 'utf-8').encode(), \
15+
addr.encode('utf-8') if isinstance(addr, unicode) else addr))
16+
17+
from_addr = raw_input('From: ')
18+
password = raw_input('Password: ')
19+
to_addr = raw_input('To: ')
20+
smtp_server = raw_input('SMTP server: ')
21+
22+
# 邮件对象:
23+
msg = MIMEMultipart()
24+
msg['From'] = _format_addr(u'Python爱好者 <%s>' % from_addr)
25+
msg['To'] = _format_addr(u'管理员 <%s>' % to_addr)
26+
msg['Subject'] = Header(u'来自SMTP的问候……', 'utf-8').encode()
27+
28+
# 邮件正文是MIMEText:
29+
msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
30+
31+
# 添加附件就是加上一个MIMEBase,从本地读取一个图片:
32+
with open('/root/report/test.xlsx', 'rb') as f:
33+
# 设置附件的MIME和文件名,这里是png类型:
34+
mime = MIMEBase('image', 'png', filename='test.png')
35+
# 加上必要的头信息:
36+
mime.add_header('Content-Disposition', 'attachment', filename='test.xlsx')
37+
mime.add_header('Content-ID', '<0>')
38+
mime.add_header('X-Attachment-Id', '0')
39+
# 把附件的内容读进来:
40+
mime.set_payload(f.read())
41+
# 用Base64编码:
42+
encoders.encode_base64(mime)
43+
# 添加到MIMEMultipart:
44+
msg.attach(mime)

2017-04-28/unitest/mydict.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Dict(dict):
2+
3+
def __init__(self, **kw):
4+
super(Dict, self).__init__(**kw)
5+
6+
def __getattr__(self, key):
7+
try:
8+
return self[key]
9+
except KeyError:
10+
raise AttributeError(r"'Dict' object has no attribute '%s'" % key)
11+
12+
def __setattr__(self, key, value):
13+
self[key] = value

2017-04-28/unitest/mydict.pyc

1.03 KB
Binary file not shown.

2017-04-28/unitest/test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import unittest
2+
3+
from mydict import Dict
4+
5+
class TestDict(unittest.TestCase):
6+
7+
def test_init(self):
8+
d = Dict(a=1, b='test')
9+
self.assertEquals(d.a, 1)
10+
self.assertEquals(d.b, 'test')
11+
self.assertTrue(isinstance(d, dict))
12+
13+
def test_key(self):
14+
d = Dict()
15+
d['key'] = 'value'
16+
self.assertEquals(d.key, 'value')
17+
18+
def test_attr(self):
19+
d = Dict()
20+
d.key = 'value'
21+
self.assertTrue('key' in d)
22+
self.assertEquals(d['key'], 'value')
23+
24+
def test_keyerror(self):
25+
d = Dict()
26+
with self.assertRaises(KeyError):
27+
value = d['empty']
28+
29+
def test_attrerror(self):
30+
d = Dict()
31+
with self.assertRaises(AttributeError):
32+
value = d.empty
33+
if __name__ == '__main__':
34+
unittest.main()

0 commit comments

Comments
 (0)