Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
numberoverzero / Small timer.py
Last active December 20, 2016 19:47
Updated noop callback to correctly handle multiple args.
import time
def timer():
start = time.time()
return lambda: time.time() - start
def timed(callback=None):
if callback is None:
callback = lambda *a, **kw: None
def wrapper(func):
def wrapped_call(*args, **kwargs):
@numberoverzero
numberoverzero / groupproxy.py
Last active December 15, 2015 19:38
treat a group of objects as a single instance
binary_operator_str = """
def __{name}__(self, obj):
gen = lambda: (item {op} obj for obj in self)
return _proxy(gen)
#return GroupProxy([item {op} obj for item in self])
def __r{name}__(self, obj):
gen = lambda: (obj {op} item for obj in self)
return _proxy(gen)
#return GroupProxy([obj {op} item for item in self])
@numberoverzero
numberoverzero / color.py
Last active December 16, 2015 20:19
Swizzle-able color attribute access. Part of the gritty module rewrite.
import re
import collections
CHANNELS = 'rgba'
DEFAULT_COLORS = [0, 0, 0, 255]
CHANNEL_SEARCH = re.compile('[^' + CHANNELS + ']').search
IS_MULTI_CHANNEL = lambda name: name and not bool(CHANNEL_SEARCH(name))
@numberoverzero
numberoverzero / factory.py
Created May 19, 2013 19:49
factory that captures the usual args, kwargs of instantiation and saves them so multiple copies can be created.
from functools import wraps
class Factory(object):
def __init__(self, cls, *args, **kwargs):
self._cls = cls
self._args = args
self._kwargs = kwargs
@property
def instance(self):
return self._cls(*(self._args), **(self._kwargs))
from functools import wraps
import collections
import bitstring
noop_init = lambda *a, **kw: None
passthrough = lambda v: v
def flatten(l, ltypes=collections.Sequence):
import types
import six
import sys
def handler(func):
'''
Decorator for creating handlers out of generator functions
Similar in usage to the @contextmanager decorator
public interface Poolable<E> {
E getNext();
void setNext(E next);
boolean isActive();
E reset();
}
public enum PoolBehavior {
EXPAND,
DESTROY,
@numberoverzero
numberoverzero / RollingAverage.java
Created January 21, 2014 08:36
rolling average of a float for a given duration + resolution
public class RollingAverage {
private float resolution;
private float remainderDt;
private float remainder;
private float[] samples;
private int nsamples;
private int oldest;
@numberoverzero
numberoverzero / profanity.py
Created March 26, 2014 22:34
Filter delimited profanity - does not handle character substitution
'''
Character substitution
====
Not handled at all. Should be reasonably straightforward to plug into the existing code.
Roughly:
def expand_substitutions(wordlist, sub_map):
for word in wordlist:
for substitution in expand_word(word, sub_map):
yield substitution
@numberoverzero
numberoverzero / worker.html
Last active August 29, 2015 14:00
Basic web worker
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
<div>Type some stuff and click send (or hit enter)</div>
<input type='text' id='input' onkeyup="onKeyUp(event)" autofocus="autofocus"></input>
<button onclick='send_text()'>Send</button>
<div id='output'></div>