Skip to content

Commit f58e9ea

Browse files
committed
add struct, collections and hashlib sample
1 parent 38624c3 commit f58e9ea

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

py3/commonlib/check_bmp.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import struct
5+
6+
bmp_header = b'\x42\x4d\x38\x8c\x0a\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00\x00\x00\x80\x02\x00\x00\x68\x01\x00\x00\x01\x00\x18\x00'
7+
8+
print(struct.unpack('<ccIIIIIIHH', bmp_header))

py3/commonlib/use_collections.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
from collections import namedtuple
5+
6+
Point = namedtuple('Point', ['x', 'y'])
7+
p = Point(1, 2)
8+
print('Point:', p.x, p.y)
9+
10+
from collections import deque
11+
12+
q = deque(['a', 'b', 'c'])
13+
q.append('x')
14+
q.appendleft('y')
15+
print(q)
16+
17+
from collections import defaultdict
18+
19+
dd = defaultdict(lambda: 'N/A')
20+
dd['key1'] = 'abc'
21+
print('dd[\'key1\'] =', dd['key1'])
22+
print('dd[\'key2\'] =', dd['key2'])
23+
24+
from collections import Counter
25+
c = Counter()
26+
for ch in 'programming':
27+
c[ch] = c[ch] + 1
28+
print(c)

py3/commonlib/use_hashlib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import hashlib
5+
6+
md5 = hashlib.md5()
7+
md5.update('how to use md5 in python hashlib?'.encode('utf-8'))
8+
print(md5.hexdigest())
9+
10+
sha1 = hashlib.sha1()
11+
sha1.update('how to use sha1 in '.encode('utf-8'))
12+
sha1.update('python hashlib?'.encode('utf-8'))
13+
print(sha1.hexdigest())

0 commit comments

Comments
 (0)