forked from turbidsoul/wx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
60 lines (48 loc) · 1.25 KB
/
util.py
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
# -*- coding: utf8 -*-
from hashlib import sha1
def checkSignure(token, timestamp, nonce, signature):
'''
check signature
>>> checkSignure('abcd', '1371630815', 'asdfasd', 'asdf')
False
>>> checkSignure('abcd', '1371630815', 'asdfasd', '272aeed372eb37fb01b49784e48f561fb670046d')
True
'''
args = [token, timestamp, nonce]
args.sort()
return sha1("".join(args)).hexdigest() == signature
def to_unicode(value):
"""
convert string to unicode
>>> to_unicode("123")
u'123'
>>> to_unicode("abc")
u'abc'
>>> to_unicode("测试")
u'\u6d4b\u8bd5'
"""
if isinstance(value, unicode):
return value
if isinstance(value, basestring):
return value.decode('utf-8')
if isinstance(value, int):
return str(value)
if isinstance(value, bytes):
return value.decode('utf-8')
return value
def singleton(cls, *args, **kw):
"""
单例
@singleton
class ClassName(object):
pass
"""
instances = {}
def getinstance(*args, **kw):
if cls not in instances:
instances[cls] = cls(*args, **kw)
return instances[cls]
return getinstance
if __name__ == '__main__':
import doctest
doctest.testmod()