Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e0ade21

Browse files
committed
add some modules before
1 parent 5e3dcf9 commit e0ade21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1227
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from hashlib import *
2+
3+
mm = md5()
4+
mm.update("asdf" + '1234dsgadsfa')
5+
print mm.digest()
6+
print mm.digest_size
7+
# print mm.digest.block_size
8+
9+
mm = new("ripemd160")
10+
print mm.digest_size
11+
mm.update("asdf")
12+
print mm.digest()
13+
mm.update('1234dsgadsfa')
14+
print mm.digest()
15+
print mm.digest_size
16+
17+
# dk = pbkdf2_hmac('sha256', b'password', b'salt', 100000)

modules_study/array/array_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sametype and control bit

modules_study/base/base_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
3+
# help(os)
4+
5+
class test(object):
6+
pass
7+
8+
mm = test()
9+
setattr(mm, 'a', 1)
10+
print dir(mm)
11+
print hasattr(mm, 'a')
12+
13+
print '1+2'
14+
print eval('1+2')
15+
16+
17+
print vars(mm)
18+
print hash('a')
19+
20+
# memoryview()
21+
22+
print bytes('a')
23+
print bytes(1)
24+
print bytes("mm")
25+
26+
print bytearray('123')

modules_study/bitsect/bitsect_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import bisect
2+
3+
data=[1,2,3,4,5]
4+
bisect.bisect_left(data, 1)
5+
bisect.bisect_right(data, 1)
6+
7+
bisect.insort_left(data, 2)
8+
bisect.insort_right(data, 2)
9+
bisect.insort(data,2)
10+
11+
def index(a, x):
12+
'Locate the leftmost value exactly equal to x'
13+
i = bisect_left(a, x)
14+
if i != len(a) and a[i] == x:
15+
return i
16+
raise ValueError
17+
18+
def find_lt(a, x):
19+
'Find rightmost value less than x'
20+
i = bisect_left(a, x)
21+
if i:
22+
return a[i-1]
23+
raise ValueError
24+
25+
def find_le(a, x):
26+
'Find rightmost value less than or equal to x'
27+
i = bisect_right(a, x)
28+
if i:
29+
return a[i-1]
30+
raise ValueError
31+
32+
def find_gt(a, x):
33+
'Find leftmost value greater than x'
34+
i = bisect_right(a, x)
35+
if i != len(a):
36+
return a[i]
37+
raise ValueError
38+
39+
def find_ge(a, x):
40+
'Find leftmost item greater than or equal to x'
41+
i = bisect_left(a, x)
42+
if i != len(a):
43+
return a[i]
44+
raise ValueError
45+
46+
47+
def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
48+
i = bisect(breakpoints, score)
49+
return grades[i]
50+
51+
[grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
52+
['F', 'A', 'C', 'C', 'B', 'A', 'A']
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from collections import *
2+
3+
# nn = ChainMap({'a':1, 'b':2}, {'c':3, 'd':4})
4+
5+
count = Counter()
6+
for i in ['red', 'blue', 'red']:
7+
count[i] += 1
8+
9+
print count
10+
print count.elements()
11+
12+
import re
13+
# words = re.findall(r'\w+', open('hamlet.txt').read().lower())
14+
# Counter(words).most_common(10)
15+
16+
"""
17+
>>> c = Counter(a=2, b=-4)
18+
>>> +c
19+
Counter({'a': 2})
20+
>>> -c
21+
Counter({'b': 4})
22+
23+
>>> c = Counter(a=3, b=1)
24+
>>> d = Counter(a=1, b=2)
25+
>>> c + d # add two counters together: c[x] + d[x]
26+
Counter({'a': 4, 'b': 3})
27+
>>> c - d # subtract (keeping only positive counts)
28+
Counter({'a': 2})
29+
>>> c & d # intersection: min(c[x], d[x])
30+
Counter({'a': 1, 'b': 1})
31+
>>> c | d # union: max(c[x], d[x])
32+
Counter({'a': 3, 'b': 2})
33+
34+
"""
35+
mm = deque()
36+
mm.append('a')
37+
mm.append('b')
38+
mm.append('c')
39+
mm.append('d')
40+
print mm
41+
42+
mm.appendleft('e')
43+
print mm
44+
45+
print mm.count('a')
46+
print mm.pop()
47+
48+
print mm
49+
mm.rotate(2)
50+
print mm
51+
52+
"""
53+
def tail(filename, n=10):
54+
'Return the last n lines of a file'
55+
with open(filename) as f:
56+
return deque(f, n)
57+
58+
59+
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
60+
>>> d = defaultdict(list)
61+
>>> for k, v in s:
62+
... d[k].append(v)
63+
...
64+
>>> sorted(d.items())
65+
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
66+
67+
68+
>>> # Basic example
69+
>>> Point = namedtuple('Point', ['x', 'y'])
70+
>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
71+
>>> p[0] + p[1] # indexable like the plain tuple (11, 22)
72+
33
73+
>>> x, y = p # unpack like a regular tuple
74+
>>> x, y
75+
(11, 22)
76+
>>> p.x + p.y # fields also accessible by name
77+
33
78+
>>> p # readable __repr__ with a name=value style
79+
Point(x=11, y=22)
80+
81+
82+
>>> # regular unsorted dictionary
83+
>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
84+
85+
>>> # dictionary sorted by key
86+
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
87+
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
88+
89+
>>> # dictionary sorted by value
90+
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
91+
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])
92+
93+
>>> # dictionary sorted by length of the key string
94+
>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
95+
OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
96+
"""
97+
98+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!--coding:utf-8--
2+
from pickle import *
3+
4+
"""
5+
# 经常遇到在Python程序运行中得到了一些字符串、列表、字典等数据,想要长久的保存下来,
6+
# 方便以后使用,而不是简单的放入内存中关机断电就丢失数据。
7+
# 它是一个将任意复杂的对象转成对象的文本或二进制表示的过程。同样,必须能够将对象经过序列化后的形式恢复到原有的对象。
8+
# marshal和pickle的区别在于Marshal只能处理简单的Python对象
9+
"""
10+
11+
from copyreg import *
12+
13+
import copyreg, copy, pickle
14+
class C(object):
15+
def __init__(self, a):
16+
self.a = a
17+
18+
def pickle_c(c):
19+
print("pickling a C instance...")
20+
return C, (c.a,)
21+
22+
copyreg.pickle(C, pickle_c)
23+
c = C(1)
24+
d = copy.copy(c)
25+
p = pickle.dumps(c)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import shelve
2+
3+
d = shelve.open('./test.dat', writeback=True)
4+
d['x'] = ['a', 'b', 'c']
5+
print d
6+
d.sync()
7+
d['x'].append('d')
8+
print d
9+
d.close()
10+
11+
d = shelve.open('./test.dat')
12+
for i in d.items():
13+
print i
14+
d.close()
15+
16+
"""
17+
dbm
18+
# 在一些python小型应用程序中,不需要关系型数据库时,
19+
# 可以方便的用持久字典来存储名称/值对,它与python的字典非常类似,
20+
# 主要区别在于数据是在磁盘读取和写入的。另一个区别在于dbm的键和值必须是字符串类型。
21+
22+
python中的shelve模块,可以提供一些简单的数据操作
23+
他和python中的dbm很相似。
24+
25+
区别如下:
26+
都是以键值对的形式保存数据,不过在shelve模块中,
27+
key必须为字符串,而值可以是python所支持的数据
28+
类型。在dbm模块中,键值对都必须为字符串类型。
29+
"""
12 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import datetime
2+
3+
print datetime.datetime.now()
4+
print datetime.datetime.today()
5+
print datetime.datetime.utcnow()
6+
print datetime.MAXYEAR
7+
print datetime.MINYEAR
8+
9+
mm = datetime.timedelta(days=365)
10+
mm1 = datetime.timedelta(days=364)
11+
print mm
12+
print abs(mm)
13+
print mm-mm1
14+
print mm.total_seconds()
15+
16+
from datetime import date
17+
18+
print date(2016, 8, 19)
19+
print date.today()
20+
21+
import time
22+
23+
nn = time.time()
24+
print nn
25+
print date.fromtimestamp(nn)
26+
#date2 = date1 + timedelta
27+
28+
print date(2002,12,4).timetuple() #time.localtime()
29+
print date(2016,9,18).weekday()
30+
print date(2016,9,18).isoweekday()
31+
print date(2016,9,18).ctime()
32+
# print date.__format__(format) #date.strftime()
33+
34+
# d = date.fromordinal(730920) # 730920th day after 1. 1. 0001
35+
36+
# d.strftime("%d/%m/%y")
37+
import calendar
38+
39+
print calendar.monthrange(2016, 9)
40+
print calendar.isleap(2016)
41+
# print calendar.monthdayscalendar(2016,9)
42+
43+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from difflib import *
2+
import sys
3+
4+
s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
5+
s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
6+
sys.stdout.writelines(context_diff(s1, s2, fromfile='before.py', tofile='after.py'))
7+
8+
get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
9+
10+
print '='*80
11+
diff = ndiff('one\ntwo\nthree'.splitlines(),'ore\ntree\nemu'.splitlines())
12+
# print ''.join(diff)
13+
print ' '.join(restore(diff, 1))
14+
15+
16+
sys.stdout.writelines(unified_diff(s1, s2, fromfile='before.py', tofile='after.py'))
17+
18+
# difflib.IS_LINE_JUNK(line)
19+
# Return true for ignorable lines. The line line is ignorable if line is blank or contains a single '#'
20+
# difflib.IS_CHARACTER_JUNK(ch)
21+
# Return true for ignorable characters. The character ch is ignorable if ch is a space or tab,
22+
# difflib.diff_bytes(dfunc, a, b, fromfile=b'', tofile=b'', fromfiledate=b'', tofiledate=b'', n=3, lineterm=b'\n')
23+
# Compare a and b (lists of bytes objects) using dfunc
24+
25+
s = SequenceMatcher(None, 'abc', 'ab')
26+
print s.ratio()
27+
28+
s = SequenceMatcher(lambda x: x=='',
29+
"private private Thread currentThread;",
30+
"private volatile Thread currentThread;")
31+
32+
print
33+
34+

modules_study/enum/enum_type.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from enum import Enum
2+
3+
class color(Enum):
4+
red = 1
5+
green = 2
6+
blue = 2
7+
8+
color.red
9+
color.green
10+
11+
12+
mm = color(1)
13+
mm = color(2)
14+
15+
color['red']
16+
mm = color['green']
17+
mm.name
18+
nn.value
19+
20+
@unique
21+
class color(Enum):
22+
red = 1
23+
green = 2
24+
blue = 2
25+
26+
list(color)
27+
28+
29+
for name, member in Shape.__members__.items():
30+
name, member
31+
32+
('square', <Shape.square: 2>)
33+
('diamond', <Shape.diamond: 1>)
34+
('circle', <Shape.circle: 3>)
35+
('alias_for_square', <Shape.square: 2>)
36+
37+
Color.red is Color.red
38+
Color.red is not Color.red
39+
Color.red == Color.red
40+
41+
Note :Subclassing an enumeration is allowed only if the enumeration does not define any members
42+
43+
Animal = Enum('Animal', 'ant bee cat dog')
44+
45+
46+
class Shape(IntEnum):
47+
circle = 1
48+
square = 2
49+

0 commit comments

Comments
 (0)