Repository navigation
Expand file tree
/
Copy pathmodule_built_in.py
More file actions
180 lines (115 loc) · 3.68 KB
/
Copy pathmodule_built_in.py
File metadata and controls
180 lines (115 loc) · 3.68 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-01-21 14:58:15
# @Author : MetalSeed (mc.gaofei@gmail.com)
# @Link : ------------------
# @Version : $Id$
############################### xxxxxxxx ###############################
#----------------------------------------------------------------------#
############################### datetime ###############################
from datetime import datetime
now = datetime.now()
# print(now)
dt = datetime(2020, 11, 20, 14, 30)
# print(dt)
ts = dt.timestamp()
# print(ts)
dt = datetime.fromtimestamp(ts)
# print(dt)
dt = datetime.strptime('2020-1-1 18:00:30', '%Y-%m-%d %H:%M:%S') #str to datetime
# print(dt)
#----------------------------------------------------------------------#
############################## collections ##############################
from collections import namedtuple
Circle = namedtuple('Circle', ['x', 'y', 'r'])
circle = Circle(1, 1, 4)
# print(circle.r)
from collections import deque
dq = deque([1, 2, 3])
dq.append(4) #de.appendleft()
dq.popleft() #de.pop()
# print(dq)
from collections import defaultdict
dd = defaultdict(lambda: 'N/A')
# print(dd['key1'])
from collections import OrderedDict
#order is the order of insertion
od = OrderedDict()
od[1] = 'a'
od[3] = 'c'
od[2] = 'b'
# print(od.keys())
from collections import Counter
c = Counter()
for x in 'this is a short string':
c[x] = c[x] + 1
print(c)
#----------------------------------------------------------------------#
############################### base64 ###############################
import base64
s = base64.b64encode(b'binary\x00string')
# print(s)
s = base64.b64decode(s)
# print(s)
s = base64.b64encode(b'i\xb7\x1d\xfb\xef\xff')
# print(s)
s = base64.urlsafe_b64encode(b'i\xb7\x1d\xfb\xef\xff')
# print(s)
s = base64.urlsafe_b64decode(s)
# print(s)
#----------------------------------------------------------------------#
############################### struct ###############################
# bytes to string/int
n = 10240099
b1 = (n & 0xff000000) >> 24
b2 = (n & 0xff0000) >> 16
b3 = (n & 0xff00) >> 8
b4 = n & 0xff
bs = bytes([b1, b2, b3, b4])
bs
# print(bs)
import struct
s = struct.pack('>I', 10240099)
# print(s)
s = struct.unpack('>IH', b'\xf0\xf0\xf0\xf0\x80\x80')
#----------------------------------------------------------------------#
############################### hashlib ###############################
import hashlib
#md5
md5 = hashlib.md5()
md5.update('how to use md5 in '.encode('utf-8'))
md5.update('python hashlib?'.encode('utf-8'))
# print(md5.hexdigest())
#sha1
sha1 = hashlib.sha1()
sha1.update('how to use sha1 in '.encode('utf-8'))
sha1.update('python hashlib?'.encode('utf-8'))
# print(sha1.hexdigest())
#----------------------------------------------------------------------#
############################### hmac ###############################
import hmac
message = b'this x'
key = b'secret'
h = hmac.new(key, message, digestmod='MD5')
# print(h.hexdigest())
#----------------------------------------------------------------------#
############################### itertools ###############################
import itertools
#just caculate next object when been called
cs = itertools.cycle('abc')
# for c in cs:
# print(c)
ns = itertools.repeat('abc', 3)
# for n in ns:
# print(n)
natuals = itertools.count(1)
ns = itertools.takewhile(lambda x: x <= 10, natuals)
# print(list(ns))
# chain groupby
# for c in itertools.chain('abc', '123'):
# print(c)
# for key, group in itertools.groupby('AAABBBBCCCDD'):
# print(key, list(group))
# for key, group in itertools.groupby('AaabBbcCaa', lambda c: c.upper()):
# print(key, list(group))
#----------------------------------------------------------------------#