Skip to content

Instantly share code, notes, and snippets.

Avatar

PL cowbert

View GitHub Profile
View reddit.py
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 Apr 2, 2019
RFC 4122 Version 1 UUID with CSPRNG-based node ID and clock sequence
View gen_rnd_uuid1.py
"""
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 Feb 17, 2020
class decorators with staticmethod
View foo.py
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)
View sqla_uuid.py
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 Aug 23, 2018
tornado virtual url path patching
View main.py
# 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 Jan 9, 2018
Inherit an argument-accepting method decorator pattern
View method_decorator.py
"""
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):
You can’t perform that action at this time.