Skip to content

Instantly share code, notes, and snippets.

View kurtbrose's full-sized avatar
💭
carbon based

Kurt Rose kurtbrose

💭
carbon based
View GitHub Profile
@kurtbrose
kurtbrose / gist:3094967
Created July 12, 2012 01:10
slightly higher level struct
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
@kurtbrose
kurtbrose / sockstr.py
Created July 24, 2012 18:28
Class to make socket behave like string
'''
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
@kurtbrose
kurtbrose / split.py
Created August 1, 2012 22:20
python split and isplit
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)
@kurtbrose
kurtbrose / gist:3282071
Created August 7, 2012 05:47
simulation explodes
>>> physics.test()
[[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
[[ 0 0 0]
[ 0 100 0]
[ 0 0 0]]
[[ 0 0 0]
@kurtbrose
kurtbrose / thread_actor.py
Created August 28, 2012 20:36
simple gEvent thread-actor
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
@kurtbrose
kurtbrose / desx.py
Created October 20, 2012 03:29
implementing DES-X (mode CBC) using PyCrypto
'''
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
@kurtbrose
kurtbrose / middleware.py
Created November 17, 2012 00:16
middleware.py
import inspect, types
AUTO = ['req', 'request', 'next', 'context', 'ctx']
class Middleware(object):
provides = ()
endpoint = None
render = None
request = None
@kurtbrose
kurtbrose / gist:4113750
Created November 19, 2012 20:44
nonlocal
>>> 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>
@kurtbrose
kurtbrose / crc16.py
Created November 27, 2012 01:15
create an efficient(-ish) CRC16 computation from an arbitrary taps polynomial
'''
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):
@kurtbrose
kurtbrose / aes_unwrap.py
Created November 30, 2012 01:20
AES key unwrapping as defined in RFC 3394
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