Skip to content

Instantly share code, notes, and snippets.

@adoc
adoc / thredis.py
Last active August 29, 2015 13:57
"""Just some simple threaded redis pool classes. Also some useful
primitive data models.
"""
# Date: 2014/03/19
# Author: https://github.com/adoc/
# © 2014 Nicholas Long. All Rights Reserved.
import logging
log = logging.getLogger(__name__)
@adoc
adoc / __init__.py
Last active August 29, 2015 13:57
safedict: A threadsafe dict-like object.
"""
"""
import threading
__all__ = ('SafeDict', )
class SafeDict:
"""A threadsafe dict-like object.
@adoc
adoc / __init__.py
Last active August 29, 2015 13:57
orderedset: Ordered set implementation. (src: http://code.activestate.com/recipes/576694/)
"""Ordered set implementation.
http://code.activestate.com/recipes/576694/
"""
import collections
__all__ = ('OrderedSet', )
class OrderedSet(collections.MutableSet):
@adoc
adoc / qbytes.py
Last active August 29, 2015 13:59
def qbytes(*args):
"""Accepts argument list of variables that might be bytes and
decodes, otherwise passing the value through."""
# This is a bit hackish and the static-typers are throwing their
# hands up. :P
return tuple([arg.decode()
if isinstance(arg, bytes)
else arg
for arg in args])
@adoc
adoc / ex_unique.py
Created April 25, 2014 00:06
Unique constraint
"""
Python Pseudocode implementation of a unique constraint.
uniques - Set of unique KEYS. Ex: {'username'}
obj - Dict object being persisted. Ex: {'id': 12345,
'username'}
obj_set - Set of `obj` key/val pairs.
all_ - Set of ID of every record.
id_ - ID of object we are checking
@adoc
adoc / __init__.py
Created April 27, 2014 16:20
threadutils: Threading utility functions.
import time
import threading
THROTTLE = 0.01
class KillableThread(threading.Thread):
"""Subclass of threading.Thread with kill signal functionality.
"""
@adoc
adoc / cb.py
Last active August 29, 2015 14:02
"""
Bitcoin Daemon RPC wrapper for simple calculations, top address list,
maybe block explorer verification, etc.
"""
import time
import operator
import pickle
import json
@adoc
adoc / py2rangereplace.py
Created June 18, 2014 20:41
Python 2.x `range` replacement
try:
range = xrange
log.warning("Replacing `range` with `xrange`.")
except NameError:
pass
@adoc
adoc / pyr_validate_view.py
Last active August 29, 2015 14:03
pyr_validate_view.py: Pyamid View Decorator for Validation
def validate_model(params=None, match=None, headers=lambda r: tuple(),
json=json,
invalid_params_exc=pyramid.httpexceptions.HTTPBadRequest,
invalid_match_exc=pyramid.httpexceptions.HTTPNotFound
):
"""Basic validation decorator for usage in `view_config`.
Takes `params` and `match` as arguments.
`params` - Schema to use to and instruct to validate requests.params
`match` - Schema to use to and isntruct to validate request.match
@adoc
adoc / gist:4694d8072fb73b335d92
Created July 25, 2014 22:29
formencode upload / image validators
#---
import os
import shutil
import pickle
import cgi
import tempfile
import formencode
import hashlib