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 / param_types.py
Created July 21, 2011 18:01
simple parameter type decorator
import inspect
def accepts(*a, **kw):
"""
Decorates a function to add type checking to calls.
Raises TypeError if the decorated function is called with parameters that violate the type constraints.
@accepts can be called in the same way as the function it wraps, with positional and keyword arguments replaced by types.
Any arguments of the decorated function whose type are not specified will not be type checked.
Any arguments of the decorated function with default values will have those default values checked immediately.
"""
@kurtbrose
kurtbrose / gist:2138652
Created March 20, 2012 17:51
The idea is to create a SuperTree with jQuery like functionality on arbitrary data.
'''
The idea is to create a SuperTree with jQuery like functionality on arbitrary
data.
The key is, chain and accept a selector function in place of css.
Here are some jQuery methods that could be implemented:
add() -- adds to the set of matched elements
Nodes should only be in one place -- one parent;
table = {}
def f(n, k):
if (n, k) not in table: #compute if not yet visited
if n == 1:
table[n,k] = k-1
if n == 2:
table[n,k] = k*(k-1)
if n >= 3:
table[n,k] = (k-1)*f(n-1, k) + (k-1)*f(n-2, k)
@kurtbrose
kurtbrose / gist:2389072
Created April 15, 2012 00:48
open LPT1, read from it, save changes to a file
import ctypes
import time
import datetime
#a bunch of win32 constants; unfortunately must be hardcoded here
GENERIC_READ = 0x80000000
SHARE_NONE = 0
OPEN_EXISTING = 3
FILE_ATTRIBUTE_NONE = 0
>>> class A(object):
... def __getattribute__(self, k): return "AAA"
...
>>> class B(A): pass
...
>>> b = B()
>>> b.a
'AAA'
>>> b.b = 2
>>> b.b
@kurtbrose
kurtbrose / exec_secure.py
Created June 18, 2012 05:53
exec_secure
import struct
import hashlib
import zlib
LONG = struct.Struct('>L')
def exec_secure(secure_file):
with open(secure_file, "rb") as f:
sec_data = f.read()
sec_data = zlib.decompress(sec_data)
@kurtbrose
kurtbrose / updict.py
Created June 26, 2012 17:44
chaining dictionary
class updict(dict):
def __new__(cls, up=None, *a, **kw):
return dict.__new__(cls, *a, **kw)
def __init__(self, up=None, *a, **kw):
dict.__init__(self, *a, **kw)
self.up = up
def mro(self):
def mro_gen():
@kurtbrose
kurtbrose / class-based parsing
Created June 28, 2012 00:14
performance comparison of class versus closure based parsing
BINARY PARSING TEST.....
without instrumentation:
1.2692489624 seconds
Wed Jun 27 17:01:13 2012 profile.tmp
1431592 function calls (1343306 primitive calls) in 2.614 CPU seconds
Ordered by: internal time
List reduced from 99 to 30 due to restriction <30>
@kurtbrose
kurtbrose / gist:3009357
Created June 28, 2012 05:46
experimenting with bytearray
maybe a way to squeeze a bit more out of these short string appends
>>> def list_method():
... l = list()
... for i in range(10000):
... l.append(b'\x01')
... return "".join(l)
...
>>> a = list_method()
>>> def byte_array_method():
@kurtbrose
kurtbrose / gist:3078448
Created July 9, 2012 19:38
javascript parseInt(Infinity) lolwut
1/0
Infinity
parseInt(1/0, 5)
NaN
parseInt(1/0, 19)
18
parseInt(1/0, 20)
18
parseInt(1/0, 45)
NaN