Skip to content

Commit 4ad695a

Browse files
committed
更新工具类
1 parent bb3947d commit 4ad695a

6 files changed

Lines changed: 136 additions & 19 deletions

File tree

test/test_except.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,41 @@
1111
import time
1212
import traceback
1313

14-
try:
15-
# raise Exception('error_message')
16-
raise Exception('error_message', 'error_code')
17-
except Exception as e:
18-
# 打印异常
19-
print type(e), e
2014

21-
time.sleep(0.1)
15+
def test_01():
16+
try:
17+
# raise Exception('error_message')
18+
raise Exception('error_message', 'error_code')
19+
except Exception as e:
20+
# 打印异常
21+
print type(e), e
2222

23-
# 打印异常消息
24-
print type(e.message), e.message
23+
time.sleep(0.1)
2524

26-
time.sleep(0.1)
25+
# 打印异常消息
26+
print type(e.message), e.message
2727

28-
# 打印异常参数
29-
print e.args, type(e.args[0]), e.args[0]
28+
time.sleep(0.1)
3029

31-
time.sleep(0.1)
30+
# 打印异常参数
31+
print e.args, type(e.args[0]), e.args[0]
3232

33-
# 打印异常堆栈跟踪信息 stack traceback
34-
traceback.print_exc()
33+
time.sleep(0.1)
34+
35+
# 打印异常堆栈跟踪信息 stack traceback
36+
traceback.print_exc()
37+
38+
39+
def test_02():
40+
try:
41+
print u'try'
42+
raise Exception(u'try')
43+
except Exception as e:
44+
print u'except'
45+
raise Exception(u'except')
46+
finally:
47+
print u'finally'
3548

3649

3750
if __name__ == '__main__':
38-
pass
51+
test_02()

test/test_pandas.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,43 @@ def test_read_sem_excel():
6969
return sem_map
7070

7171

72+
def test_read_area_code_excel():
73+
file_path = '全球区号.xlsx'
74+
df = pd.read_excel(file_path, sheetname='code') # sheet_name=str(0)
75+
print list(df.keys())
76+
# print df.to_dict()
77+
# sem_map = {}
78+
for i in df.values:
79+
code_dict = {
80+
'id': i[0],
81+
'name_c': i[1],
82+
'area_code': i[2],
83+
'phone_pre': '+00%s' % i[2],
84+
'country_area': i[3],
85+
'short_code': i[4],
86+
'name_e': i[5]
87+
}
88+
print json.dumps(code_dict, indent=4, ensure_ascii=False)+','
89+
# print sem_map
90+
91+
92+
def test_read_area_code_map_excel():
93+
file_path = '全球区号.xlsx'
94+
df = pd.read_excel(file_path, sheetname='code') # sheet_name=str(0)
95+
print list(df.keys())
96+
# print df.to_dict()
97+
# sem_map = {}
98+
for i in df.values:
99+
print '%s: \'%s\', # [%s]%s(%s) %s' % (i[0], i[2], i[4], i[1], i[5], i[3])
100+
101+
72102
if __name__ == '__main__':
73103
# test_csv()
74104
# test_excel()
75105
# test_csv_encode()
76-
test_read_sem_excel()
106+
# test_read_sem_excel()
107+
# test_read_area_code_excel()
108+
test_read_area_code_map_excel()
77109

78110

79111
"""

test/全球区号.xlsx

44.3 KB
Binary file not shown.

tools/convert.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def hex_to_bytes(string):
2424
def test_unescape():
2525
# unicode编码后的汉字的解析
2626
str_xx = '加入到"我的书目选单"中'
27+
str_xx = '\xe5\xbd\x93\xe5\x89\x8d\xe5\xb7\xb2\xe8\xbe\xbe\xe5\x88\xb0\xe6\x8a\x93\xe5\x8f\x96\xe9\x85\x8d\xe7\xbd\xae\xe7\x9a\x84\xe6\x9c\x80\xe5\xa4\xa7\xe9\xa1\xb5\xe7\xa0\x81'
2728
from HTMLParser import HTMLParser
2829
h = HTMLParser()
2930
print h.unescape(str_xx)
@@ -44,6 +45,7 @@ def test_unicode():
4445
# unicode_string.encode('target_encoding')
4546
print repr(u'\u6211\u6765\u4e86'.encode('utf-8')), u'\u6211\u6765\u4e86'.encode('utf-8') # 每个汉子占3个字节
4647
print repr(u'\u6211\u6765\u4e86'.encode('GBK')), u'\u6211\u6765\u4e86'.encode('GBK') # 每个汉子占2个字节
48+
print repr('没有新数据,停止翻页')
4749

4850

4951
def test():

tools/redis_pub_sub.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ def sub(self, k):
4747
# yield item
4848
yield item.get('data')
4949

50+
def p_sub(self, k):
51+
"""
52+
PSub
53+
订阅一个或多个符合给定模式的频道
54+
每个模式以 * 作为匹配符
55+
注意 psubscribe 与 subscribe 区别
56+
:param k:
57+
:return:
58+
"""
59+
ps = self.__db.pubsub()
60+
ch = '%s:%s' % (self.key, k)
61+
ps.psubscribe(ch)
62+
for item in ps.listen():
63+
# {'pattern': None, 'type': 'subscribe', 'channel': 'queue:test:hh', 'data': 1L}
64+
# yield item
65+
yield item.get('data')
66+
5067
def sub_not_loop(self, k):
5168
"""
5269
Sub 非无限循环,取到结果即退出
@@ -62,17 +79,24 @@ def sub_not_loop(self, k):
6279

6380

6481
def test_pub():
65-
q = RedisPubSub('test')
82+
q = RedisPubSub('test:aa')
6683
q.pub('hh', '123')
6784

6885

6986
def test_sub():
70-
q = RedisPubSub('test')
87+
q = RedisPubSub('test:aa')
7188
r = q.sub('hh')
7289
for i in r:
7390
print i
7491

7592

93+
def test_p_sub():
94+
q = RedisPubSub('test:*')
95+
r = q.p_sub('hh')
96+
for i in r:
97+
print i
98+
99+
76100
def test_sub_not_loop():
77101
q = RedisPubSub('test')
78102
r = q.sub_not_loop('hh')
@@ -104,6 +128,14 @@ def run():
104128
✗ python redis_pub_sub.py test_pub
105129
106130
131+
测试模式订阅
132+
终端一:
133+
✗ python redis_pub_sub.py test_p_sub
134+
终端二:
135+
✗ python redis_pub_sub.py test_pub
136+
137+
138+
107139
测试非无限循环模式
108140
终端一:
109141
✗ python redis_pub_sub.py test_sub_not_loop

tools/token.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
@author: zhanghe
6+
@software: PyCharm
7+
@file: token.py
8+
@time: 2016/11/29 下午7:13
9+
"""
10+
11+
import base64
12+
import json
13+
14+
15+
class Token(object):
16+
def __init__(self):
17+
self._key = '123qweasdzxc'
18+
19+
def sign(self, data):
20+
data += '.%s' % self._key
21+
print data
22+
return base64.b64encode(data)
23+
24+
def unsign(self, data):
25+
return base64.b64decode(data)
26+
27+
28+
if __name__ == '__main__':
29+
token = Token()
30+
# 测试字符串
31+
a = token.sign('1234')
32+
b = token.unsign(a)
33+
print a, b
34+
# 测试字典
35+
test_dict_str = json.dumps({'z': 56, 'm': 12, 'p': 34}, sort_keys=True)
36+
a = token.sign(test_dict_str)
37+
b = token.unsign(a)
38+
print a, b

0 commit comments

Comments
 (0)