This file contains hidden or 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
def timeit(n): | |
def timeit_decorator(func): | |
async def process(func, *args, **params): | |
if asyncio.iscoroutinefunction(func): | |
return await func(*args, **params) | |
else: | |
return func(*args, **params) | |
async def helper(*args, **params): | |
times = [] |
This file contains hidden or 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
@contextlib.contextmanager | |
def no_stdout(): | |
save_stdout = sys.stdout | |
sys.stdout = io.BytesIO() | |
yield | |
sys.stdout = save_stdout |
This file contains hidden or 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
@contextmanager | |
def avoid( | |
exception: type(Exception), | |
raises: Exception = None, | |
exception_manager: Callable = None, | |
finally_manager: Callable = lambda: (), | |
): | |
try: | |
yield | |
except exception as e: |
This file contains hidden or 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 contextlib import contextmanager | |
import pytest | |
@contextmanager | |
def not_raises(exception): | |
try: | |
yield | |
except exception as e: |
This file contains hidden or 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
requirements.txt: pyproject.lock | |
poetry run pip freeze > $@ |
This file contains hidden or 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
class FunctionHolder(object): | |
def __init__(self, function): | |
self.func = function | |
self.called_count = 0 | |
def __call__(self, *args, **kwargs): | |
try: | |
return self.func(*args, **kwargs) | |
finally: | |
self.called_count += 1 | |
This file contains hidden or 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
def search_view(request): | |
if request.GET.get('q', None): | |
django_admin_keyword_search(User, request.GET['q'], ['search_field_1', 'search_field_2']) | |
def django_admin_keyword_search(model, keywords, search_fields): | |
all_queries = None | |
for keyword in keywords.split(' '): # breaks query_string into 'Foo' and 'Bar' | |
keyword_query = None |
This file contains hidden or 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 | |
from time import time | |
def timeit(f): | |
@wraps(f) | |
def wrapper(*args, **kwargs): | |
start = time() | |
result = f(*args, **kwargs) | |
end = time() | |
print('Elapsed time: {}'.format(end - start)) |
This file contains hidden or 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
def to_str(bytes_or_str: Union[str, bytes]) -> str: | |
if isinstance(bytes_or_str, bytes): | |
value = bytes_or_str.decode('utf-8') | |
elif isinstance(bytes_or_str, bytearray): | |
value = bytes(bytes_or_str).decode('utf-8') | |
else: | |
value = bytes_or_str | |
return value | |
NewerOlder