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.
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}, |
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 | |
""" |
# prepend shebang to a script | |
sed -i '1i #!/bin/bash' my-script.sh |
<pre> {% filter force_escape %} {% debug %} {% endfilter %} </pre> |
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 |
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) |
import timeit | |
def func(x): | |
pass | |
def func_2(): | |
pass | |
def wrapper(func, *args, **kwargs): | |
def wrapper(): |
import timeit | |
# Add a memoization method? | |
def factorial(n): | |
if n == 0: | |
return 1 | |
else: | |
return n * factorial(n - 1) | |
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 |