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 / typesnap.py
Created January 9, 2019 19:27
very light dependency injection
'''
Dependency-Injection lite.
The input is key-values of constructors, the output
is key-values of constructed values.
Keys in the arguments to this functions are distributed
to the arguments as values are constructed.
IDIOMS:
To pass a constant value: keys which are not callable
will be left as-is.
To pass a callable as a value: simply wrap it in a lambda.
'''
Given two modules that want to reference each other in a way that would
otherwise create an import loop, one mechanism for handling this is
a deferred import.
def foo():
import bar
bar.bar()
@kurtbrose
kurtbrose / pairwise.py
Last active December 14, 2018 22:25
some utilities for walking over all possible sets of pairs with a minimum number of creations / deletions
import itertools, math
def pairwise_walk(items):
'''
given a list of items, return a list of instructions of which
ordered pairs of relationships to add or remove, in order to
go over every possible set of pairwise relationships existing
or not existing while only adding or removing one at a time
@kurtbrose
kurtbrose / stevens_glom_start.ipynb
Created December 13, 2018 21:48
stevens glom strategy
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kurtbrose
kurtbrose / sqlite_matrix.py
Last active December 13, 2018 23:44
matrices in a sqlite database (mostly as a prototype for linear algebra in larger SQL databases)
'''
implementation of some linear algebra in SQL
'''
import sqlite3
import time
import weakref
_MISSING = object()
@kurtbrose
kurtbrose / complement_set.py
Last active November 3, 2018 00:32
an inverse set
class ComplementSet(object):
'''
inverse of a normal set -- everything except the set is in there
'''
def __init__(self, missing):
assert type(missing) in (set, frozenset)
self.missing = missing
def __repr__(self): return 'ComplementSet({' + ', '.join([repr(e) for e in self.missing]) + '})'
@kurtbrose
kurtbrose / scopespecs.py
Created November 2, 2018 20:15
glom spec for evaluating other specs into the scope of a child
from glom import glom, T, S
class ScopeSpecs(object):
'''
given a dict of specs, when evaluating
evaluate each of those specs against the same target,
then evalaute the child spec with the results of
those specs in the corresponding keys of the scope
so the child can see them
@kurtbrose
kurtbrose / even_division.py
Last active November 4, 2022 03:12
integer division that keeps track of remainders / rounding over a group of numbers
import copy
def closest_round_division(numbers, fractions):
'''
Given a list of numbers and a list of fractions, this function performs a more
sophisticated version of:
[[round(n * f) for f in fractions] for n in numbers]
Rather than rounding each number individually, the algorithm rounds in order that
the sum of each row and the sum of each column both remain within 1 of their exact
@kurtbrose
kurtbrose / glom_if.py
Last active September 20, 2018 18:43
glom if-branch spec
import glom
class If(object):
def __init__(self, cond, if_branch, else_branch=None):
self.cond, self.if_branch, self.else_branch = cond, if_branch, else_branch
def glomit(self, target, scope):
g = lambda spec: scope[glom.glom](target, spec, scope)
if g(self.cond):
@kurtbrose
kurtbrose / bits.py
Last active January 25, 2019 06:55
An immutable bit-string or bit-array object.
import binascii
class Bits(object):
'''
An immutable bit-string or bit-array object.
Provides list-like access to bits as bools,
as well as bitwise masking and shifting operators.