update from atlas
This commit is contained in:
parent
8fb691d3c7
commit
290079ec9a
65
17-futures/crypto/arcfour-timings.txt
Normal file
65
17-futures/crypto/arcfour-timings.txt
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
workers|time
|
||||||
|
4|4.96
|
||||||
|
3|5.40
|
||||||
|
2|8.35
|
||||||
|
1|11.25
|
||||||
|
1|11.17
|
||||||
|
2|8.45
|
||||||
|
3|6.08
|
||||||
|
4|5.83
|
||||||
|
4|6.22
|
||||||
|
3|7.33
|
||||||
|
2|9.48
|
||||||
|
1|11.86
|
||||||
|
1|11.72
|
||||||
|
2|9.22
|
||||||
|
3|6.74
|
||||||
|
4|6.37
|
||||||
|
4|4.94
|
||||||
|
3|5.51
|
||||||
|
2|8.25
|
||||||
|
1|11.47
|
||||||
|
1|12.90
|
||||||
|
2|8.94
|
||||||
|
3|6.44
|
||||||
|
4|5.90
|
||||||
|
4|5.94
|
||||||
|
3|6.46
|
||||||
|
2|9.10
|
||||||
|
1|11.66
|
||||||
|
1|11.48
|
||||||
|
2|9.08
|
||||||
|
3|6.31
|
||||||
|
4|5.99
|
||||||
|
4|5.02
|
||||||
|
3|5.46
|
||||||
|
2|8.26
|
||||||
|
1|11.18
|
||||||
|
1|11.23
|
||||||
|
2|8.52
|
||||||
|
3|5.64
|
||||||
|
4|5.39
|
||||||
|
4|5.53
|
||||||
|
3|6.07
|
||||||
|
2|8.66
|
||||||
|
1|11.42
|
||||||
|
1|11.34
|
||||||
|
2|8.44
|
||||||
|
3|5.88
|
||||||
|
4|5.57
|
||||||
|
4|4.93
|
||||||
|
3|5.47
|
||||||
|
2|8.65
|
||||||
|
1|11.23
|
||||||
|
1|11.12
|
||||||
|
2|7.83
|
||||||
|
3|5.81
|
||||||
|
4|5.45
|
||||||
|
4|5.54
|
||||||
|
3|6.09
|
||||||
|
2|8.84
|
||||||
|
1|11.45
|
||||||
|
1|11.25
|
||||||
|
2|8.32
|
||||||
|
3|6.02
|
||||||
|
4|5.74
|
55
17-futures/crypto/arcfour.py
Normal file
55
17-futures/crypto/arcfour.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
"""RC4 compatible algorithm"""
|
||||||
|
|
||||||
|
def arcfour(key, in_bytes, loops=20):
|
||||||
|
|
||||||
|
kbox = bytearray(256) # create key box
|
||||||
|
for i, car in enumerate(key): # copy key and vector
|
||||||
|
kbox[i] = car
|
||||||
|
j = len(key)
|
||||||
|
for i in range(j, 256): # repeat until full
|
||||||
|
kbox[i] = kbox[i-j]
|
||||||
|
|
||||||
|
# [1] initialize sbox
|
||||||
|
sbox = bytearray(range(256))
|
||||||
|
|
||||||
|
# repeat sbox mixing loop, as recommened in CipherSaber-2
|
||||||
|
# http://ciphersaber.gurus.com/faq.html#cs2
|
||||||
|
j = 0
|
||||||
|
for k in range(loops):
|
||||||
|
for i in range(256):
|
||||||
|
j = (j + sbox[i] + kbox[i]) % 256
|
||||||
|
sbox[i], sbox[j] = sbox[j], sbox[i]
|
||||||
|
|
||||||
|
# main loop
|
||||||
|
i = 0
|
||||||
|
j = 0
|
||||||
|
out_bytes = bytearray()
|
||||||
|
|
||||||
|
for car in in_bytes:
|
||||||
|
i = (i + 1) % 256
|
||||||
|
# [2] shuffle sbox
|
||||||
|
j = (j + sbox[i]) % 256
|
||||||
|
sbox[i], sbox[j] = sbox[j], sbox[i]
|
||||||
|
# [3] compute t
|
||||||
|
t = (sbox[i] + sbox[j]) % 256
|
||||||
|
k = sbox[t]
|
||||||
|
car = car ^ k
|
||||||
|
out_bytes.append(car)
|
||||||
|
|
||||||
|
return out_bytes
|
||||||
|
|
||||||
|
|
||||||
|
def test():
|
||||||
|
from time import time
|
||||||
|
clear = bytearray(b'1234567890' * 100000)
|
||||||
|
t0 = time()
|
||||||
|
cipher = arcfour(b'key', clear)
|
||||||
|
print('elapsed time: %.2fs' % (time() - t0))
|
||||||
|
result = arcfour(b'key', cipher)
|
||||||
|
assert result == clear, '%r != %r' % (result, clear)
|
||||||
|
print('elapsed time: %.2fs' % (time() - t0))
|
||||||
|
print('OK')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test()
|
46
17-futures/crypto/arcfour_futures.py
Normal file
46
17-futures/crypto/arcfour_futures.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import sys
|
||||||
|
import time
|
||||||
|
from concurrent import futures
|
||||||
|
from random import randrange
|
||||||
|
from arcfour import arcfour
|
||||||
|
|
||||||
|
JOBS = 12
|
||||||
|
SIZE = 2**18
|
||||||
|
|
||||||
|
KEY = b"'Twas brillig, and the slithy toves\nDid gyre"
|
||||||
|
STATUS = '{} workers, elapsed time: {:.2f}s'
|
||||||
|
|
||||||
|
|
||||||
|
def arcfour_test(size, key):
|
||||||
|
in_text = bytearray(randrange(256) for i in range(size))
|
||||||
|
cypher_text = arcfour(key, in_text)
|
||||||
|
out_text = arcfour(key, cypher_text)
|
||||||
|
assert in_text == out_text, 'Failed arcfour_test'
|
||||||
|
return size
|
||||||
|
|
||||||
|
|
||||||
|
def main(workers=None):
|
||||||
|
if workers:
|
||||||
|
workers = int(workers)
|
||||||
|
t0 = time.time()
|
||||||
|
|
||||||
|
with futures.ProcessPoolExecutor(workers) as executor:
|
||||||
|
actual_workers = executor._max_workers
|
||||||
|
to_do = []
|
||||||
|
for i in range(JOBS, 0, -1):
|
||||||
|
size = SIZE + int(SIZE / JOBS * (i - JOBS/2))
|
||||||
|
job = executor.submit(arcfour_test, size, KEY)
|
||||||
|
to_do.append(job)
|
||||||
|
|
||||||
|
for future in futures.as_completed(to_do):
|
||||||
|
res = future.result()
|
||||||
|
print('{:.1f} KB'.format(res/2**10))
|
||||||
|
|
||||||
|
print(STATUS.format(actual_workers, time.time() - t0))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) == 2:
|
||||||
|
workers = int(sys.argv[1])
|
||||||
|
else:
|
||||||
|
workers = None
|
||||||
|
main(workers)
|
119
17-futures/crypto/arcfour_test.py
Normal file
119
17-futures/crypto/arcfour_test.py
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from arcfour import arcfour
|
||||||
|
|
||||||
|
'''
|
||||||
|
Source of the test vectors:
|
||||||
|
A Stream Cipher Encryption Algorithm "Arcfour"
|
||||||
|
http://tools.ietf.org/html/draft-kaukonen-cipher-arcfour-03
|
||||||
|
'''
|
||||||
|
|
||||||
|
TEST_VECTORS = [
|
||||||
|
('CRYPTLIB', {
|
||||||
|
'Plain Text' : (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
||||||
|
'Key' : (0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF),
|
||||||
|
'Cipher Text' : (0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
('COMMERCE', {
|
||||||
|
'Plain Text' : (0xdc, 0xee, 0x4c, 0xf9, 0x2c),
|
||||||
|
'Key' : (0x61, 0x8a, 0x63, 0xd2, 0xfb),
|
||||||
|
'Cipher Text' : (0xf1, 0x38, 0x29, 0xc9, 0xde),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
('SSH ARCFOUR', {
|
||||||
|
'Plain Text' : (
|
||||||
|
0x52, 0x75, 0x69, 0x73, 0x6c, 0x69, 0x6e, 0x6e,
|
||||||
|
0x75, 0x6e, 0x20, 0x6c, 0x61, 0x75, 0x6c, 0x75,
|
||||||
|
0x20, 0x6b, 0x6f, 0x72, 0x76, 0x69, 0x73, 0x73,
|
||||||
|
0x73, 0x61, 0x6e, 0x69, 0x2c, 0x20, 0x74, 0xe4,
|
||||||
|
0x68, 0x6b, 0xe4, 0x70, 0xe4, 0x69, 0x64, 0x65,
|
||||||
|
0x6e, 0x20, 0x70, 0xe4, 0xe4, 0x6c, 0x6c, 0xe4,
|
||||||
|
0x20, 0x74, 0xe4, 0x79, 0x73, 0x69, 0x6b, 0x75,
|
||||||
|
0x75, 0x2e, 0x20, 0x4b, 0x65, 0x73, 0xe4, 0x79,
|
||||||
|
0xf6, 0x6e, 0x20, 0x6f, 0x6e, 0x20, 0x6f, 0x6e,
|
||||||
|
0x6e, 0x69, 0x20, 0x6f, 0x6d, 0x61, 0x6e, 0x61,
|
||||||
|
0x6e, 0x69, 0x2c, 0x20, 0x6b, 0x61, 0x73, 0x6b,
|
||||||
|
0x69, 0x73, 0x61, 0x76, 0x75, 0x75, 0x6e, 0x20,
|
||||||
|
0x6c, 0x61, 0x61, 0x6b, 0x73, 0x6f, 0x74, 0x20,
|
||||||
|
0x76, 0x65, 0x72, 0x68, 0x6f, 0x75, 0x75, 0x2e,
|
||||||
|
0x20, 0x45, 0x6e, 0x20, 0x6d, 0x61, 0x20, 0x69,
|
||||||
|
0x6c, 0x6f, 0x69, 0x74, 0x73, 0x65, 0x2c, 0x20,
|
||||||
|
0x73, 0x75, 0x72, 0x65, 0x20, 0x68, 0x75, 0x6f,
|
||||||
|
0x6b, 0x61, 0x61, 0x2c, 0x20, 0x6d, 0x75, 0x74,
|
||||||
|
0x74, 0x61, 0x20, 0x6d, 0x65, 0x74, 0x73, 0xe4,
|
||||||
|
0x6e, 0x20, 0x74, 0x75, 0x6d, 0x6d, 0x75, 0x75,
|
||||||
|
0x73, 0x20, 0x6d, 0x75, 0x6c, 0x6c, 0x65, 0x20,
|
||||||
|
0x74, 0x75, 0x6f, 0x6b, 0x61, 0x61, 0x2e, 0x20,
|
||||||
|
0x50, 0x75, 0x75, 0x6e, 0x74, 0x6f, 0x20, 0x70,
|
||||||
|
0x69, 0x6c, 0x76, 0x65, 0x6e, 0x2c, 0x20, 0x6d,
|
||||||
|
0x69, 0x20, 0x68, 0x75, 0x6b, 0x6b, 0x75, 0x75,
|
||||||
|
0x2c, 0x20, 0x73, 0x69, 0x69, 0x6e, 0x74, 0x6f,
|
||||||
|
0x20, 0x76, 0x61, 0x72, 0x61, 0x6e, 0x20, 0x74,
|
||||||
|
0x75, 0x75, 0x6c, 0x69, 0x73, 0x65, 0x6e, 0x2c,
|
||||||
|
0x20, 0x6d, 0x69, 0x20, 0x6e, 0x75, 0x6b, 0x6b,
|
||||||
|
0x75, 0x75, 0x2e, 0x20, 0x54, 0x75, 0x6f, 0x6b,
|
||||||
|
0x73, 0x75, 0x74, 0x20, 0x76, 0x61, 0x6e, 0x61,
|
||||||
|
0x6d, 0x6f, 0x6e, 0x20, 0x6a, 0x61, 0x20, 0x76,
|
||||||
|
0x61, 0x72, 0x6a, 0x6f, 0x74, 0x20, 0x76, 0x65,
|
||||||
|
0x65, 0x6e, 0x2c, 0x20, 0x6e, 0x69, 0x69, 0x73,
|
||||||
|
0x74, 0xe4, 0x20, 0x73, 0x79, 0x64, 0xe4, 0x6d,
|
||||||
|
0x65, 0x6e, 0x69, 0x20, 0x6c, 0x61, 0x75, 0x6c,
|
||||||
|
0x75, 0x6e, 0x20, 0x74, 0x65, 0x65, 0x6e, 0x2e,
|
||||||
|
0x20, 0x2d, 0x20, 0x45, 0x69, 0x6e, 0x6f, 0x20,
|
||||||
|
0x4c, 0x65, 0x69, 0x6e, 0x6f),
|
||||||
|
'Key' : (
|
||||||
|
0x29, 0x04, 0x19, 0x72, 0xfb, 0x42, 0xba, 0x5f,
|
||||||
|
0xc7, 0x12, 0x77, 0x12, 0xf1, 0x38, 0x29, 0xc9),
|
||||||
|
'Cipher Text' : (
|
||||||
|
0x35, 0x81, 0x86, 0x99, 0x90, 0x01, 0xe6, 0xb5,
|
||||||
|
0xda, 0xf0, 0x5e, 0xce, 0xeb, 0x7e, 0xee, 0x21,
|
||||||
|
0xe0, 0x68, 0x9c, 0x1f, 0x00, 0xee, 0xa8, 0x1f,
|
||||||
|
0x7d, 0xd2, 0xca, 0xae, 0xe1, 0xd2, 0x76, 0x3e,
|
||||||
|
0x68, 0xaf, 0x0e, 0xad, 0x33, 0xd6, 0x6c, 0x26,
|
||||||
|
0x8b, 0xc9, 0x46, 0xc4, 0x84, 0xfb, 0xe9, 0x4c,
|
||||||
|
0x5f, 0x5e, 0x0b, 0x86, 0xa5, 0x92, 0x79, 0xe4,
|
||||||
|
0xf8, 0x24, 0xe7, 0xa6, 0x40, 0xbd, 0x22, 0x32,
|
||||||
|
0x10, 0xb0, 0xa6, 0x11, 0x60, 0xb7, 0xbc, 0xe9,
|
||||||
|
0x86, 0xea, 0x65, 0x68, 0x80, 0x03, 0x59, 0x6b,
|
||||||
|
0x63, 0x0a, 0x6b, 0x90, 0xf8, 0xe0, 0xca, 0xf6,
|
||||||
|
0x91, 0x2a, 0x98, 0xeb, 0x87, 0x21, 0x76, 0xe8,
|
||||||
|
0x3c, 0x20, 0x2c, 0xaa, 0x64, 0x16, 0x6d, 0x2c,
|
||||||
|
0xce, 0x57, 0xff, 0x1b, 0xca, 0x57, 0xb2, 0x13,
|
||||||
|
0xf0, 0xed, 0x1a, 0xa7, 0x2f, 0xb8, 0xea, 0x52,
|
||||||
|
0xb0, 0xbe, 0x01, 0xcd, 0x1e, 0x41, 0x28, 0x67,
|
||||||
|
0x72, 0x0b, 0x32, 0x6e, 0xb3, 0x89, 0xd0, 0x11,
|
||||||
|
0xbd, 0x70, 0xd8, 0xaf, 0x03, 0x5f, 0xb0, 0xd8,
|
||||||
|
0x58, 0x9d, 0xbc, 0xe3, 0xc6, 0x66, 0xf5, 0xea,
|
||||||
|
0x8d, 0x4c, 0x79, 0x54, 0xc5, 0x0c, 0x3f, 0x34,
|
||||||
|
0x0b, 0x04, 0x67, 0xf8, 0x1b, 0x42, 0x59, 0x61,
|
||||||
|
0xc1, 0x18, 0x43, 0x07, 0x4d, 0xf6, 0x20, 0xf2,
|
||||||
|
0x08, 0x40, 0x4b, 0x39, 0x4c, 0xf9, 0xd3, 0x7f,
|
||||||
|
0xf5, 0x4b, 0x5f, 0x1a, 0xd8, 0xf6, 0xea, 0x7d,
|
||||||
|
0xa3, 0xc5, 0x61, 0xdf, 0xa7, 0x28, 0x1f, 0x96,
|
||||||
|
0x44, 0x63, 0xd2, 0xcc, 0x35, 0xa4, 0xd1, 0xb0,
|
||||||
|
0x34, 0x90, 0xde, 0xc5, 0x1b, 0x07, 0x11, 0xfb,
|
||||||
|
0xd6, 0xf5, 0x5f, 0x79, 0x23, 0x4d, 0x5b, 0x7c,
|
||||||
|
0x76, 0x66, 0x22, 0xa6, 0x6d, 0xe9, 0x2b, 0xe9,
|
||||||
|
0x96, 0x46, 0x1d, 0x5e, 0x4d, 0xc8, 0x78, 0xef,
|
||||||
|
0x9b, 0xca, 0x03, 0x05, 0x21, 0xe8, 0x35, 0x1e,
|
||||||
|
0x4b, 0xae, 0xd2, 0xfd, 0x04, 0xf9, 0x46, 0x73,
|
||||||
|
0x68, 0xc4, 0xad, 0x6a, 0xc1, 0x86, 0xd0, 0x82,
|
||||||
|
0x45, 0xb2, 0x63, 0xa2, 0x66, 0x6d, 0x1f, 0x6c,
|
||||||
|
0x54, 0x20, 0xf1, 0x59, 0x9d, 0xfd, 0x9f, 0x43,
|
||||||
|
0x89, 0x21, 0xc2, 0xf5, 0xa4, 0x63, 0x93, 0x8c,
|
||||||
|
0xe0, 0x98, 0x22, 0x65, 0xee, 0xf7, 0x01, 0x79,
|
||||||
|
0xbc, 0x55, 0x3f, 0x33, 0x9e, 0xb1, 0xa4, 0xc1,
|
||||||
|
0xaf, 0x5f, 0x6a, 0x54, 0x7f),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
for name, vectors in TEST_VECTORS:
|
||||||
|
print(name, end='')
|
||||||
|
plain = bytearray(vectors['Plain Text'])
|
||||||
|
cipher = bytearray(vectors['Cipher Text'])
|
||||||
|
key = bytearray(vectors['Key'])
|
||||||
|
assert cipher == arcfour(key, plain, loops=1)
|
||||||
|
assert plain == arcfour(key, cipher, loops=1)
|
||||||
|
print(' --> OK')
|
50
17-futures/crypto/sha-timings.txt
Normal file
50
17-futures/crypto/sha-timings.txt
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
workers|time
|
||||||
|
4|8.88
|
||||||
|
3|11.14
|
||||||
|
2|13.66
|
||||||
|
1|22.80
|
||||||
|
1|25.42
|
||||||
|
2|16.37
|
||||||
|
3|12.09
|
||||||
|
4|11.06
|
||||||
|
4|11.40
|
||||||
|
3|11.51
|
||||||
|
2|15.20
|
||||||
|
1|24.18
|
||||||
|
1|22.09
|
||||||
|
2|12.48
|
||||||
|
3|10.78
|
||||||
|
4|10.48
|
||||||
|
4|8.48
|
||||||
|
3|10.07
|
||||||
|
2|12.42
|
||||||
|
1|20.24
|
||||||
|
1|20.31
|
||||||
|
2|11.39
|
||||||
|
3|10.88
|
||||||
|
4|10.44
|
||||||
|
4|10.43
|
||||||
|
3|11.11
|
||||||
|
2|12.39
|
||||||
|
1|20.69
|
||||||
|
1|20.53
|
||||||
|
2|11.80
|
||||||
|
3|11.01
|
||||||
|
4|10.52
|
||||||
|
4|11.50
|
||||||
|
3|14.45
|
||||||
|
2|16.95
|
||||||
|
1|24.77
|
||||||
|
1|22.71
|
||||||
|
2|18.35
|
||||||
|
3|12.66
|
||||||
|
4|12.20
|
||||||
|
4|12.37
|
||||||
|
3|13.37
|
||||||
|
2|19.30
|
||||||
|
1|24.30
|
||||||
|
1|23.93
|
||||||
|
2|18.51
|
||||||
|
3|13.88
|
||||||
|
4|12.97
|
||||||
|
|
38
17-futures/crypto/sha_futures.py
Normal file
38
17-futures/crypto/sha_futures.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import hashlib
|
||||||
|
from concurrent import futures
|
||||||
|
from random import randrange
|
||||||
|
|
||||||
|
JOBS = 12
|
||||||
|
SIZE = 2**20
|
||||||
|
STATUS = '{} workers, elapsed time: {:.2f}s'
|
||||||
|
|
||||||
|
|
||||||
|
def sha(size):
|
||||||
|
data = bytearray(randrange(256) for i in range(size))
|
||||||
|
algo = hashlib.new('sha256')
|
||||||
|
algo.update(data)
|
||||||
|
return algo.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def main(workers=None):
|
||||||
|
if workers:
|
||||||
|
workers = int(workers)
|
||||||
|
t0 = time.time()
|
||||||
|
|
||||||
|
with futures.ProcessPoolExecutor(workers) as executor:
|
||||||
|
actual_workers = executor._max_workers
|
||||||
|
to_do = (executor.submit(sha, SIZE) for i in range(JOBS))
|
||||||
|
for future in futures.as_completed(to_do):
|
||||||
|
res = future.result()
|
||||||
|
print(res)
|
||||||
|
|
||||||
|
print(STATUS.format(actual_workers, time.time() - t0))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) == 2:
|
||||||
|
workers = int(sys.argv[1])
|
||||||
|
else:
|
||||||
|
workers = None
|
||||||
|
main(workers)
|
53
18-asyncio/spinner_asyncio.py
Normal file
53
18-asyncio/spinner_asyncio.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
# spinner_asyncio.py
|
||||||
|
|
||||||
|
# credits: Example by Luciano Ramalho inspired by
|
||||||
|
# Michele Simionato's multiprocessing example in the python-list:
|
||||||
|
# https://mail.python.org/pipermail/python-list/2009-February/538048.html
|
||||||
|
|
||||||
|
# BEGIN SPINNER_ASYNCIO
|
||||||
|
import asyncio
|
||||||
|
import itertools
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def spin(msg): # <1>
|
||||||
|
write, flush = sys.stdout.write, sys.stdout.flush
|
||||||
|
for char in itertools.cycle('|/-\\'):
|
||||||
|
status = char + ' ' + msg
|
||||||
|
write(status)
|
||||||
|
flush()
|
||||||
|
write('\x08' * len(status))
|
||||||
|
try:
|
||||||
|
yield from asyncio.sleep(.1) # <2>
|
||||||
|
except asyncio.CancelledError: # <3>
|
||||||
|
break
|
||||||
|
write(' ' * len(status) + '\x08' * len(status))
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def slow_computation(): # <4>
|
||||||
|
# fake computation waiting a long time for I/O
|
||||||
|
yield from asyncio.sleep(3) # <5>
|
||||||
|
return 42
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def supervisor(): # <6>
|
||||||
|
spinner = asyncio.async(spin('thinking!')) # <7>
|
||||||
|
print('spinner object:', spinner) # <8>
|
||||||
|
result = yield from slow_computation() # <9>
|
||||||
|
spinner.cancel() # <10>
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
loop = asyncio.get_event_loop() # <11>
|
||||||
|
result = loop.run_until_complete(supervisor()) # <12>
|
||||||
|
loop.close()
|
||||||
|
print('Answer:', result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
# END SPINNER_ASYNCIO
|
56
18-asyncio/spinner_thread.py
Normal file
56
18-asyncio/spinner_thread.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
# spinner_thread.py
|
||||||
|
|
||||||
|
# credits: Adapted from Michele Simionato's
|
||||||
|
# multiprocessing example in the python-list:
|
||||||
|
# https://mail.python.org/pipermail/python-list/2009-February/538048.html
|
||||||
|
|
||||||
|
# BEGIN SPINNER_THREAD
|
||||||
|
import threading
|
||||||
|
import itertools
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
class Signal: # <1>
|
||||||
|
go = True
|
||||||
|
|
||||||
|
|
||||||
|
def spin(msg, signal): # <2>
|
||||||
|
write, flush = sys.stdout.write, sys.stdout.flush
|
||||||
|
for char in itertools.cycle('|/-\\'): # <3>
|
||||||
|
status = char + ' ' + msg
|
||||||
|
write(status)
|
||||||
|
flush()
|
||||||
|
write('\x08' * len(status)) # <4>
|
||||||
|
time.sleep(.1)
|
||||||
|
if not signal.go: # <5>
|
||||||
|
break
|
||||||
|
write(' ' * len(status) + '\x08' * len(status)) # <6>
|
||||||
|
|
||||||
|
|
||||||
|
def slow_computation(): # <7>
|
||||||
|
# fake computation waiting a long time for I/O
|
||||||
|
time.sleep(3) # <8>
|
||||||
|
return 42
|
||||||
|
|
||||||
|
|
||||||
|
def supervisor(): # <9>
|
||||||
|
signal = Signal()
|
||||||
|
spinner = threading.Thread(None, spin,
|
||||||
|
args=('thinking!', signal))
|
||||||
|
print('spinner object:', spinner) # <10>
|
||||||
|
spinner.start() # <11>
|
||||||
|
result = slow_computation() # <12>
|
||||||
|
signal.go = False # <13>
|
||||||
|
spinner.join() # <14>
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
result = supervisor() # <15>
|
||||||
|
print('Answer:', result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
# END SPINNER_THREAD
|
22
21-class-metaprog/evaldecos.py
Normal file
22
21-class-metaprog/evaldecos.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
def deco_alpha(func):
|
||||||
|
print('<<100>> deco_alpha')
|
||||||
|
|
||||||
|
def inner_alpha():
|
||||||
|
print('<<200>> deco_alpha')
|
||||||
|
func()
|
||||||
|
|
||||||
|
return inner_alpha
|
||||||
|
|
||||||
|
|
||||||
|
def deco_beta(cls):
|
||||||
|
print('<<300>> deco_beta')
|
||||||
|
|
||||||
|
def inner_beta(self):
|
||||||
|
print('<<400>> inner_beta')
|
||||||
|
print("result from 'deco_beta::inner_beta'")
|
||||||
|
|
||||||
|
cls.method3 = inner_beta
|
||||||
|
return cls
|
||||||
|
|
||||||
|
|
||||||
|
print('<<500>> evaldecos mudule body')
|
92
21-class-metaprog/evaltime.py
Normal file
92
21-class-metaprog/evaltime.py
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import atexit
|
||||||
|
|
||||||
|
from evaldecos import deco_alpha
|
||||||
|
from evaldecos import deco_beta
|
||||||
|
|
||||||
|
|
||||||
|
print('<<1>> start')
|
||||||
|
|
||||||
|
|
||||||
|
func_A = lambda: print('<<2>> func_A')
|
||||||
|
|
||||||
|
|
||||||
|
def func_B():
|
||||||
|
print('<<3>> func_B')
|
||||||
|
|
||||||
|
def func_C():
|
||||||
|
print('<<4>> func_C')
|
||||||
|
|
||||||
|
return func_C
|
||||||
|
|
||||||
|
|
||||||
|
@deco_alpha
|
||||||
|
def func_D():
|
||||||
|
print('<<6>> func_D')
|
||||||
|
|
||||||
|
|
||||||
|
def func_E():
|
||||||
|
print('<<7>> func_E')
|
||||||
|
|
||||||
|
|
||||||
|
class ClassOne(object):
|
||||||
|
print('<<7>> ClassOne body')
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
print('<<8>> ClassOne.__init__')
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
print('<<9>> ClassOne.__del__')
|
||||||
|
|
||||||
|
def method1(self):
|
||||||
|
print('<<10>> ClassOne.method')
|
||||||
|
return "result from 'ClassOne.method1'"
|
||||||
|
|
||||||
|
class ClassTwo(object):
|
||||||
|
print('<<11>> ClassTwo body')
|
||||||
|
|
||||||
|
|
||||||
|
@deco_beta
|
||||||
|
class ClassThree(ClassOne):
|
||||||
|
print('<<12>> ClassThree body')
|
||||||
|
|
||||||
|
def method3(self):
|
||||||
|
print('<<13>> ClassOne.method')
|
||||||
|
return "result from 'ClassThree.method3'"
|
||||||
|
|
||||||
|
|
||||||
|
if True:
|
||||||
|
print("<<14>> 'if True'")
|
||||||
|
|
||||||
|
|
||||||
|
if False:
|
||||||
|
print("<<15>> 'if False'")
|
||||||
|
|
||||||
|
|
||||||
|
atexit.register(func_E)
|
||||||
|
|
||||||
|
print("<<16>> right before 'if ... __main__'")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('<<17>> start __main__ block')
|
||||||
|
print(func_A)
|
||||||
|
print(func_A())
|
||||||
|
print('<<18>> continue __main__ block')
|
||||||
|
print(func_B)
|
||||||
|
b_result = func_B()
|
||||||
|
print(b_result)
|
||||||
|
one = ClassOne()
|
||||||
|
one.method1()
|
||||||
|
b_result()
|
||||||
|
print(func_D)
|
||||||
|
func_D()
|
||||||
|
three = ClassThree()
|
||||||
|
three.method3()
|
||||||
|
|
||||||
|
class ClassFour(object):
|
||||||
|
print('<<19>> ClasFour body')
|
||||||
|
|
||||||
|
print('<<20>> end __main__ block')
|
||||||
|
|
||||||
|
|
||||||
|
print('<<21>> The End')
|
50
21-class-metaprog/factories.py
Normal file
50
21-class-metaprog/factories.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
"""
|
||||||
|
record_factory: create simple classes just for holding data fields
|
||||||
|
|
||||||
|
# BEGIN RECORD_FACTORY_DEMO
|
||||||
|
>>> Dog = record_factory('Dog', 'name weight owner') # <1>
|
||||||
|
>>> rex = Dog('Rex', 30, 'Bob')
|
||||||
|
>>> rex # <2>
|
||||||
|
Dog(name='Rex', weight=30, owner='Bob')
|
||||||
|
>>> name, weight, _ = rex # <3>
|
||||||
|
>>> name, weight
|
||||||
|
('Rex', 30)
|
||||||
|
>>> "{2}'s dog weights {1}kg".format(*rex)
|
||||||
|
"Bob's dog weights 30kg"
|
||||||
|
>>> rex.weight = 32 # <4>
|
||||||
|
>>> rex
|
||||||
|
Dog(name='Rex', weight=32, owner='Bob')
|
||||||
|
>>> Dog.__mro__ # <5>
|
||||||
|
(<class 'factories.Dog'>, <class 'object'>)
|
||||||
|
|
||||||
|
# END RECORD_FACTORY_DEMO
|
||||||
|
"""
|
||||||
|
|
||||||
|
# BEGIN RECORD_FACTORY
|
||||||
|
def record_factory(cls_name, field_names):
|
||||||
|
if isinstance(field_names, str): # <1>
|
||||||
|
field_names = field_names.replace(',', ' ').split()
|
||||||
|
__slots__ = tuple(field_names)
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
attrs = dict(zip(self.__slots__, args))
|
||||||
|
attrs.update(kwargs)
|
||||||
|
for name, value in attrs.items():
|
||||||
|
setattr(self, name, value)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for name in self.__slots__:
|
||||||
|
yield getattr(self, name)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
values = ', '.join('{}={!r}'.format(*i) for i
|
||||||
|
in zip(self.__slots__, self))
|
||||||
|
return '{}({})'.format(self.__class__.__name__, values)
|
||||||
|
|
||||||
|
cls_attrs = dict(__slots__ = __slots__,
|
||||||
|
__init__ = __init__,
|
||||||
|
__iter__ = __iter__,
|
||||||
|
__repr__ = __repr__)
|
||||||
|
|
||||||
|
return type(cls_name, (object,), cls_attrs)
|
||||||
|
# END RECORD_FACTORY
|
42
concurrency/spinner_asyncio.py
Normal file
42
concurrency/spinner_asyncio.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# spinner_asyncio.py
|
||||||
|
|
||||||
|
# credits: Example by Luciano Ramalho inspired by
|
||||||
|
# Michele Simionato's multiprocessing example
|
||||||
|
# source:
|
||||||
|
# http://python-3-patterns-idioms-test.readthedocs.org/en/latest/CoroutinesAndConcurrency.html
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
DELAY = 0.1
|
||||||
|
DISPLAY = '|/-\\'
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def spinner_func(before='', after=''):
|
||||||
|
write, flush = sys.stdout.write, sys.stdout.flush
|
||||||
|
while True:
|
||||||
|
for char in DISPLAY:
|
||||||
|
msg = '{} {} {}'.format(before, char, after)
|
||||||
|
write(msg)
|
||||||
|
flush()
|
||||||
|
write('\x08' * len(msg))
|
||||||
|
try:
|
||||||
|
yield from asyncio.sleep(DELAY)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def long_computation(delay):
|
||||||
|
# emulate a long computation
|
||||||
|
yield from asyncio.sleep(delay)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
spinner = loop.create_task(spinner_func('Please wait...', 'thinking!'))
|
||||||
|
long_task = loop.create_task(long_computation(3))
|
||||||
|
long_task.add_done_callback(lambda f: spinner.cancel())
|
||||||
|
loop.run_until_complete(spinner)
|
||||||
|
loop.close()
|
||||||
|
|
46
concurrency/spinner_asyncio2.py
Normal file
46
concurrency/spinner_asyncio2.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
# spinner_asyncio2.py
|
||||||
|
|
||||||
|
# credits: Example by Luciano Ramalho inspired by
|
||||||
|
# Michele Simionato's multiprocessing example
|
||||||
|
# source:
|
||||||
|
# http://python-3-patterns-idioms-test.readthedocs.org/en/latest/CoroutinesAndConcurrency.html
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
DELAY = 0.1
|
||||||
|
DISPLAY = '|/-\\'
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def spinner_func(before='', after=''):
|
||||||
|
write, flush = sys.stdout.write, sys.stdout.flush
|
||||||
|
while True:
|
||||||
|
for char in DISPLAY:
|
||||||
|
msg = '{} {} {}'.format(before, char, after)
|
||||||
|
write(msg)
|
||||||
|
flush()
|
||||||
|
write('\x08' * len(msg))
|
||||||
|
try:
|
||||||
|
yield from asyncio.sleep(DELAY)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def long_computation(delay):
|
||||||
|
# emulate a long computation
|
||||||
|
yield from asyncio.sleep(delay)
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def supervisor(delay):
|
||||||
|
spinner = loop.create_task(spinner_func('Please wait...', 'thinking!'))
|
||||||
|
yield from long_computation(delay)
|
||||||
|
spinner.cancel()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
loop.run_until_complete(supervisor(3))
|
||||||
|
loop.close()
|
||||||
|
|
@ -39,8 +39,8 @@ def home(request): # <1>
|
|||||||
|
|
||||||
html = template.format(query=query, result=res, # <5>
|
html = template.format(query=query, result=res, # <5>
|
||||||
message=msg)
|
message=msg)
|
||||||
print('Sending {} results'.format(len(descriptions)))
|
print('Sending {} results'.format(len(descriptions))) # <6>
|
||||||
return web.Response(content_type=CONTENT_TYPE, text=html)
|
return web.Response(content_type=CONTENT_TYPE, text=html) # <7>
|
||||||
# END HTTP_CHARFINDER_HOME
|
# END HTTP_CHARFINDER_HOME
|
||||||
|
|
||||||
|
|
||||||
|
42
futures/countries/flags_processpool.py
Normal file
42
futures/countries/flags_processpool.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
"""Download flags of top 20 countries by population
|
||||||
|
|
||||||
|
ProcessPoolExecutor version
|
||||||
|
|
||||||
|
Sample run::
|
||||||
|
|
||||||
|
$ python3 flags_threadpool.py
|
||||||
|
BD retrieved.
|
||||||
|
EG retrieved.
|
||||||
|
CN retrieved.
|
||||||
|
...
|
||||||
|
PH retrieved.
|
||||||
|
US retrieved.
|
||||||
|
IR retrieved.
|
||||||
|
20 flags downloaded in 0.93s
|
||||||
|
|
||||||
|
"""
|
||||||
|
# BEGIN FLAGS_PROCESSPOOL
|
||||||
|
from concurrent import futures
|
||||||
|
|
||||||
|
from flags import save_flag, get_flag, show, main
|
||||||
|
|
||||||
|
MAX_WORKERS = 20
|
||||||
|
|
||||||
|
|
||||||
|
def download_one(cc):
|
||||||
|
image = get_flag(cc)
|
||||||
|
show(cc)
|
||||||
|
save_flag(image, cc.lower() + '.gif')
|
||||||
|
return cc
|
||||||
|
|
||||||
|
|
||||||
|
def download_many(cc_list):
|
||||||
|
with futures.ProcessPoolExecutor() as executor: # <1>
|
||||||
|
res = executor.map(download_one, sorted(cc_list))
|
||||||
|
|
||||||
|
return len(list(res))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main(download_many)
|
||||||
|
# END FLAGS_PROCESSPOOL
|
25
metaprog/plainpoint.py
Normal file
25
metaprog/plainpoint.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
"""
|
||||||
|
A class equivalent to the class statement below would be generated by this code:
|
||||||
|
|
||||||
|
>>> import collections
|
||||||
|
>>> Point = collections.plainclass('Point', 'x y')
|
||||||
|
"""
|
||||||
|
|
||||||
|
class Point(object):
|
||||||
|
__slots__ = ['x', 'y'] # save memory in the likely event there are many instances
|
||||||
|
|
||||||
|
def __init__(self, x, y):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return 'Point({!r}, {!r})'.format(self.x, self.y)
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
if not isinstance(other, Point):
|
||||||
|
return NotImplemented
|
||||||
|
return self.x == other.x and self.y == other.y
|
||||||
|
|
||||||
|
def __iter__(self, other): # support unpacking
|
||||||
|
yield self.x
|
||||||
|
yield self.y
|
Loading…
Reference in New Issue
Block a user