Skip to content

Instantly share code, notes, and snippets.

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 / 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
"""
@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 / 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 / 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 / django_url_viewnames.py
Created March 9, 2018 08:56
Programatically retrieve Django View names from urlconfs
from django.urls import URLResolver, URLPattern, include
from django.conf import settings
from importlib import import_module
def view_names_from_urlpatterns(urlpatterns, names=None):
""" Get all view names specified by a set of given urlpatterns
# import some urlpatterns
>>> from myapp.urls import urlpatterns
@SteadBytes
SteadBytes / multiple_value_order.py
Created December 24, 2017 08:20
Order by multiple values
lis = [(18, 4), (19, 4), (14, 3), (15, 3), (31, 3), (25,4)]
# sort by tuple[1], then tuple[0]
print(sorted(lis, key=lambda x: (x[1], x[0])))
# >>> [(14, 3), (15, 3), (31, 3), (18, 4), (19, 4), (25, 4)]
# also use for max()/min()
print(max(lis, key=lambda x: (x[1], x[0])))
# >>> (25, 4)
print(min(lis, key=lambda x: (x[1], x[0])))
# >>> (14, 3)
@SteadBytes
SteadBytes / timeit_boilerplate.py
Last active December 11, 2017 09:01
Boilerplate code for using the timeit module within a python module
import timeit
def func(x):
pass
def func_2():
pass
def wrapper(func, *args, **kwargs):
def wrapper():
@SteadBytes
SteadBytes / factorial.py
Last active November 17, 2017 18:30
Timing comparisons for recursive and iterative factorial functions
import timeit
# Add a memoization method?
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
@SteadBytes
SteadBytes / is_prime.py
Created November 12, 2017 08:46
Few methods for testing primality, with timing for comparison
import timeit
import math
def is_prime(n):
if n <=1:
return False
elif n <=3:
return True
elif n % 2 ==0 or n%3 == 0:
return False
i = 5