Skip to content

Instantly share code, notes, and snippets.

View impredicative's full-sized avatar

Ouroboros Chrysopoeia impredicative

View GitHub Profile
@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:
@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 / 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 / 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 / test_cases.py
Created April 18, 2017 14:44
TestCases class
class TestCases:
def __init__(self, **defaults):
self._defaults = defaults
self._cases = []
self._next_index = 0
def __iter__(self):
return self
@impredicative
impredicative / dict_util.py
Last active June 4, 2017 02:16
dict utils
import json
def pprint(obj):
print(json.dumps(obj, indent=2, sort_keys=True, default=str))
def nested_dict_recursive_with_single_key(nested_key, value):
head, _sep, tail = nested_key.partition('.')
return {head: (nested_dict_recursive_with_single_key(tail, value) if tail else value)}
def nested_dict_iterative_with_single_key(nested_key, value):
@impredicative
impredicative / pandas_util.py
Last active September 28, 2017 14:40
pandas_util
import io
import re
import pandas as pd
def _prepare_pipe_separated_str(str_input):
substitutions = [
('^ *', ''), # Remove leading spaces
(' *$', ''), # Remove trailing spaces
@impredicative
impredicative / fizzbuzz.py
Created October 6, 2017 23:23
Recursive FizzBuzz
def fizzbuzz_recursive(n):
if n == 0:
return
fizzbuzz_recursive(n - 1)
if n % 3 == 0 and n % 5 == 0:
print('FizzBuzz')
elif n % 3 == 0:
print('Fizz')
elif n % 5 == 0:
print('Buzz')
@impredicative
impredicative / cos_sin.py
Created November 21, 2018 17:48 — forked from anonymous/cos_sin.py
Input column name and max value in that column. Function converts values in to sin and cos components.
# Convert cyclical values from standard 0..N values in to radians
def degrees_2_rads(row):
radians = math.radians(row)
return radians
def rads_2_cos_val(row):
cos_val = math.cos(row)
return cos_val
def rads_2_sin_val(row):
@impredicative
impredicative / mismatched_etag_2019-05-27.log
Last active May 28, 2019 15:20
Mismatched export.arxiv.org ETag
# From warning and error log:
<• Feed[bot]> Etag test failed for https://export.arxiv.org/rss/cs.NE with etag '"Tue, 28 May 2019 00:30:00 GMT", "1559003400"'. The semantic content was unexpectedly found to be changed whereas the etag stayed unchanged. The previously cached content has 0 unique links and the dissimilar current content has 23.
<• Feed[bot]> The etag cache has been disabled for the duration of the bot process for all export.arxiv.org feed URLs. The semantic content mismatch should be reported to the site administrator.