Skip to content

Instantly share code, notes, and snippets.

View BasicWolf's full-sized avatar

Zaur Nasibov BasicWolf

View GitHub Profile
@BasicWolf
BasicWolf / cache_value_decorator.py
Created June 17, 2015 13:27
Cache data and pass it to a method
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
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
@BasicWolf
BasicWolf / tictoc
Last active June 13, 2021 17:57
Python Tic-Toc decorator
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}")
# -- 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))