Skip to content

Instantly share code, notes, and snippets.

@SteadBytes
SteadBytes / django_debug_tag.html
Created March 9, 2018 12:13
Show formatted Django debug template tag
<pre> {% filter force_escape %} {% debug %} {% endfilter %} </pre>
@SteadBytes
SteadBytes / 2-4-spaces.md
Last active July 12, 2018 11:13
Convert 2->4 space indentation in files

Convert 2->4 Space Indentation In place

Converts all .py files from 2->4 space indentation

find ./ -iname "*.py" -type f -exec perl -i -pe's{^((?: {2})*)}{" " x (4*length($1)/2)}e' {} +

Change -iname "*.py" to match other file types.

@SteadBytes
SteadBytes / prepend.sh
Last active July 23, 2018 15:59
sed prepend to file
# prepend shebang to a script
sed -i '1i #!/bin/bash' my-script.sh
@SteadBytes
SteadBytes / function_timeit.py
Created December 29, 2018 07:26
Time python function
from functools import partial
import timeit
def measure_time(n, f, *args, **kwargs):
"""
Args:
n (int): number of tests
f (func): function to time
*args: any arguments to pass to f
"""
from itertools import combinations
from typing import Dict
def dictionary_combinations(d: Dict, min_keys=1, max_keys=None):
"""
Produce all possible combinations of sub-dictionaries from a dictionary
within a length range.
>>> dictionary_combinations({'key1': 1, 'key2': 2, 'key3': 3})
[{'key1': 1},
@SteadBytes
SteadBytes / dot2dict.py
Created February 28, 2019 15:25
Transform dot-notation keys into nested dictionary
def dot2dict(a):
dict_tree = lambda: defaultdict(dict_tree)
output = dict_tree()
for k, v in a.items():
path = k.split(".")
target = reduce(lambda d, k: d[k], path[:-1], output)
target[path[-1]] = v
return output
import time
from functools import wraps
class RetryExhaustedError(Exception):
pass
def retry(*exceptions, retries=5, cooldown=1, verbose=True):
def decorator(func):

Keybase proof

I hereby claim:

  • I am steadbytes on github.
  • I am steadbytes (https://keybase.io/steadbytes) on keybase.
  • I have a public key ASACj6MbJtDw50SKFTABlfMdwp9p0LLEYKhVHOdZ0HKW0Qo

To claim this, I am signing this object:

from functools import reduce
from typing import Iterable
def dict_values_same_types(key, cls, *dicts):
return all(isinstance(d[key], cls) for d in dicts)
def dict_merger(original: dict, incoming: dict):
"""
In place deep merge `incoming` into `original`.
@SteadBytes
SteadBytes / runs.py
Created October 1, 2019 06:32
Find maximal runs of an iterable.
"""
Find maximal runs of an iterable.
For example, natural merge-sort https://en.wikipedia.org/wiki/Merge_sort#Natural_merge_sort uses
maximal runs to improve sorting efficiency.
Run tests using pytest --doctest-modules runs.py
- Requires hypothesis: https://hypothesis.readthedocs.io/en/latest/index.html
"""
from itertools import chain, tee, zip_longest