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 / dynstats.py
Last active December 18, 2015 16:19
Implementation of piece-wise parabolic quantile estimation, algorithm defined in http://www.cs.wustl.edu/~jain/papers/ftp/psqr.pdf
'''
Implementation of piece-wise parabolic quantile estimation, algorithm defined in
http://www.cs.wustl.edu/~jain/papers/ftp/psqr.pdf
'''
from math import copysign
DEFAULT_LEVELS = (0.1, 1, 2, 5, 10, 25, 50, 75, 80, 85, 90, 95, 98, 99, 99.5, 99.8, 99.9, 99.99)
class Measure(object):
@kurtbrose
kurtbrose / stats.py
Created June 21, 2013 07:31
putting a bow on the stats collector
'''
Efficient online statistics gathering. Create a Stats object to track
as many statistics as desired.
Implementation of piece-wise parabolic quantile estimation, algorithm defined in
http://www.cs.wustl.edu/~jain/papers/ftp/psqr.pdf
'''
from math import copysign
@kurtbrose
kurtbrose / stream_sample.py
Created June 24, 2013 00:04
implementation of reservoir sample
class Sample(object):
'''
This class implements Reservoir Sampling to keep a random sample of an infinite stream.
See http://gregable.com/2007/10/reservoir-sampling.html for one description.
'''
def __init__(self, sample_size=2**14, type='f'):
self.sample = array.array(type)
self.sample_size = sample_size
self.num_vals = 0
@kurtbrose
kurtbrose / rest_param.py
Created July 17, 2013 09:20
parsley RESTful parameter grammar
import parsley
GRAMMAR = '''
digit = :x ?(x in '0123456789') -> x
digit1_9 = :x ?(x in '123456789') -> x
digits = (digit1_9:first <digit*>:rest -> first + rest) | '0'
integer = ('-' | -> ''):sign digits:ds -> int(sign + ds)
alpha_ = :x ?(x.isalpha() or x == '_') -> x
alphanum_ = :x ?(x.isalnum() or x == '_') -> x
@kurtbrose
kurtbrose / stats.py
Last active December 21, 2015 01:28
online calculation of higher order stats
'''
Computation of higher order statistics based on
http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Higher-order_statistics
Intended for use in long-running applications.
'''
class Stats(object):
def __init__(self):
@kurtbrose
kurtbrose / jks.py
Created September 18, 2013 22:27
short, incomplete, and mostly wrong extension of python jks library to support writing as well as reading jks files
'''
JKS file format decoder.
Use in conjunction with PyOpenSSL to translate to PEM, or load private key and certs
directly into openssl structs and wrap sockets.
See http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/security/provider/JavaKeyStore.java#JavaKeyStore.engineLoad%28java.io.InputStream%2Cchar%5B%5D%29
'''
import struct
import hashlib
@kurtbrose
kurtbrose / queuesim.py
Last active December 26, 2015 07:19
Getting very close to useful. Will convert into a real github repo soon.
import heapq
import collections
import time
import random
import greenlet
#import faststat
class Simulation(object):
@kurtbrose
kurtbrose / green_repl.py
Last active December 27, 2015 00:49
greenlet based REPL -- note dependent on 1.0 gEvent which adds the fileobject module
import sys
import code
from gevent import fileobject
_green_stdin = fileobject.FileObject(sys.stdin)
_green_stdout = fileobject.FileObject(sys.stdout)
def _green_raw_input(prompt):
_green_stdout.write(prompt)
return _green_stdin.readline()[:-1]
@kurtbrose
kurtbrose / objbrowser.py
Last active August 29, 2015 13:55
Very early bones of a Tkinter object browsing GUI. Should be useful for debugging GC.
import threading
import functools
import gc
import types
class ObjBrowserThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.daemon = True
@kurtbrose
kurtbrose / sprite_bounce.py
Created February 26, 2014 08:33
working on pyglet stuff for having a dynamic set of 2d nodes
'''
Two main jobs:
1- calculate the desired position
2- accelerate towards that position
Several acceleration profiles:
"spring" acceleration -- acceleration proportional to displacement from ideal
"rocket" acceleration -- max acceleration to mid point, followed by max de-celleration