This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct | |
def make_struct_class(name_format_tuple_list, endian="@"): | |
names, codes = zip(*name_format_tuple_list) | |
struct_code = endian + "".join(codes) | |
serializer = struct.Struct(struct_code) | |
class StructClass(object): | |
__slots__ = names | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Small class to make a socket behave like a string. | |
The idea is to be able to pass an string-like thing to a parser that doesn't know in advance how much data it will require. | |
This requires a lot of copying; reading data is O(n**2). It is probably better to make a parser which works off of a stream. | |
If the data is available as a string, but the parser works with a stream, StringIO is well behaved. | |
''' | |
class SocketString(object): | |
def __init__(self, sock): | |
self.sock = sock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def split(pred, array): | |
return filter(pred, array), filter(lambda e: not pred(e), array) | |
import itertools | |
def isplit(pred, iterable): | |
pos, neg = itertools.tee(iterable, 2) | |
return itertools.ifilter(pred, pos), itertools.ifilterfalse(pred, neg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> physics.test() | |
[[[ 0 0 0] | |
[ 0 0 0] | |
[ 0 0 0]] | |
[[ 0 0 0] | |
[ 0 100 0] | |
[ 0 0 0]] | |
[[ 0 0 0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gevent.queue | |
import gevent.threadpool | |
class ThreadActor(object): | |
def __init__(self, process_items, process_error=None, queue_size=4096): | |
self.queue = gevent.queue.Queue(queue_size) | |
self.pool = gevent.threadpool.ThreadPool(1) | |
self.process_items = process_items | |
self.process_error = process_error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This code implements DES-X mode CBC efficiently using PyCrypto | |
''' | |
from Crypto.Cipher import DES | |
def des_x(plaintext, key, iv='\0'*8): | |
'key must be 24 bytes, plaintext must be multiple of 8 bytes' | |
pre_whiten = XOR.new(key[ 8:16]).encrypt #XOR -- encrypt/decrypt are the same | |
post_whiten = XOR.new(key[16:24]).encrypt | |
encrypt = DES.new(key[:8], mode=DES.MODE_ECB).encrypt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect, types | |
AUTO = ['req', 'request', 'next', 'context', 'ctx'] | |
class Middleware(object): | |
provides = () | |
endpoint = None | |
render = None | |
request = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> def foo(): | |
... a = 2 | |
... def bar(): | |
... print a | |
... return bar | |
... | |
>>> foo().func_closure | |
(<cell at 0x02515FF0: int object at 0x023AA6CC>,) | |
>>> foo().func_closure[0] | |
<cell at 0x02589ED0: int object at 0x023AA6CC> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
See 16 bit values from here: | |
http://en.wikipedia.org/wiki/Cyclic_redundancy_check#Commonly_used_and_standardized_CRCs | |
for some commonly used values for taps. | |
Since this implementation uses right shifting, take the values from the 'reversed' column. | |
Some examples: 0xA001, 0x8408, 0xEDD1, 0xA6BC, 0x91A0, 0xD405 | |
These are all maximal taps; that is, the permutation over integers mod 2**16 that they define | |
has one giant cycle rather than multiple small cycles. | |
''' | |
class CRC16(object): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct | |
from Crypto.Cipher import AES | |
QUAD = struct.Struct('>Q') | |
#key wrapping as defined in RFC 3394 | |
#http://www.ietf.org/rfc/rfc3394.txt | |
def aes_unwrap(kek, wrapped, iv=0xa6a6a6a6a6a6a6a6): | |
n = len(wrapped)/8 - 1 | |
#NOTE: R[0] is never accessed, left in for consistency with RFC indices |