Skip to content

Instantly share code, notes, and snippets.

View cowbert's full-sized avatar

PL cowbert

View GitHub Profile
def countAllList(lst):
# define the list of accumulators:
counts = [0]*10
# counts == [0,0,0...0] length of 10
# iterate over lst to process each string at a time
for s in lst:
# lets turn the string into a list of letters
s_list = list(s)
@cowbert
cowbert / gen_rnd_uuid1.py
Last active April 2, 2019 04:58
RFC 4122 Version 1 UUID with CSPRNG-based node ID and clock sequence
"""
We patch uuid.uuid1() to take a random `node` (used to be the 48 bit MAC
address) according to RFC 4122 section 4.5 and CSPRNG 14 bit `clockseq`
See: https://docs.python.org/2/library/uuid.html#uuid.uuid1
and https://svn.python.org/projects/python/branches/release27-maint/Lib/uuid.py
and https://tools.ietf.org/html/rfc4122#section-4.5
The primary use-case for a UUID like this is to guarantee a virtually
collision-free monotonically-increasing value (based on the clock) such
that it can replace an object like an 'autoincrementing' integer primary
@cowbert
cowbert / foo.py
Last active August 15, 2023 22:09
class decorators with staticmethod
from tornado.web import RequestHandler
class BaseHandler(RequestHandler):
def prepare(self):
self.current_user = None
def _render_401(self):
if self._req_was_rest:
self.set_status(401)
from uuid import UUID as py_UUID
from sqlalchemy.dialects.postgresql import BYTEA as pg_BYTEA
from sqlalchemy.dialects.oracle import RAW as ora_RAW
from sqlalchemy.types import TypeDecorator, BINARY
class sqla_type_UUID(TypeDecorator):
impl = BINARY
def __init__(self):
@cowbert
cowbert / main.py
Created August 23, 2018 02:42
tornado virtual url path patching
# import tornado bits
import tornado.ioloop
import tornado.web
# Import some controllers
from controllers import RequestHandler1, RequestHandler2, RequestHandler3
# original routes
routes = [
(r'/path1', RequestHandler1),
@cowbert
cowbert / method_decorator.py
Last active January 9, 2018 23:50
Inherit an argument-accepting method decorator pattern
"""
I want to enforce coupling between a decorator that takes params and the method
it should apply to (invoking the decorator outside of the class would make no
semantic sense)
"""
class Foo(object):
attr = ['a','b','c']
def __init__(self, *args, **kwargs):
for i, arg in enumerate(args):