This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
def cache(**cached_kwargs): | |
def wrap(f): | |
@wraps(f) | |
def wrapped(self, *args, **kwargs): | |
kwargs.update(cached_kwargs) | |
return f(self, *args, **kwargs) | |
return wrapped | |
return wrap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import partial | |
itself = lambda x: x | |
plus = lambda x, y: x + y | |
minus = lambda x, y: x - y | |
class _FBase: | |
def __init__(self, f1=itself): | |
self.f1 = f1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
def measure(f): | |
import timeit | |
@wraps(f) | |
def wrapped_f(*args, **kwargs): | |
tic = timeit.default_timer() | |
ret = f(*args, **kwargs) | |
toc = timeit.default_timer() | |
print(f"TICTOC: {toc - tic}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -- Filter -- | |
def filter_range(range_start, range_end, *exclusions): | |
def make_prange(start, stop): | |
return lambda x: start <= x <= stop | |
exclusion_predicates = [make_prange(start, stop) | |
for (start, stop) in exclusions] | |
filtered_range = (n for n in range(range_start, range_end + 1) | |
if not any(ef(n) for ef in exclusion_predicates)) |