Skip to content

Instantly share code, notes, and snippets.

View impredicative's full-sized avatar

Ouroboros Chrysopoeia impredicative

View GitHub Profile
@impredicative
impredicative / default_namedtuple.py
Last active April 15, 2017 00:13
Default collections namedtuple
from collections import namedtuple # pylint: disable=import-self
def default_namedtuple(typename='Case', **defaults):
NamedTuple = namedtuple(typename, defaults) # pylint: disable=invalid-name
default_tuple = NamedTuple(**defaults)
customizable_tuple = default_tuple._replace
return customizable_tuple
@impredicative
impredicative / locatable.py
Last active April 14, 2017 23:05
Python locatable class
import inspect
class Locatable:
def __new__(cls, *_args, **_kwargs):
# Background: http://eli.thegreenplace.net/2012/04/16/python-object-creation-sequence
obj = super().__new__(cls)
obj.location = obj._initialization_location() # pylint: disable=protected-access
return obj
@impredicative
impredicative / logger.py
Last active March 11, 2023 10:59
Python json logging using structlog and stdlib logging
"""
Usage example:
from logger import get_logger
log = get_logger()
log.info('my_event', my_key1='val 1', my_key2=5, my_key3=[1, 2, 3], my_key4={'a': 1, 'b': 2})
List of metadata keys in each log message:
event
_func
@impredicative
impredicative / integer_exponentiation.py
Created December 26, 2016 02:30
Integer exponentiation
import math
# Python ≥ 3.5
def recursive_linear(b, n):
if n < 0:
return (1/b) * recursive_linear(b, n+1)
if n == 0:
return 1
else: