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.
<pre> {% filter force_escape %} {% debug %} {% endfilter %} </pre> |
# prepend shebang to a script | |
sed -i '1i #!/bin/bash' my-script.sh |
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}, |
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): |
I hereby claim:
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`. |
""" | |
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 |