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
import logging | |
from logger import log_level | |
def message(self, message): | |
with log_level(logging.INFO, __name__) as logger: | |
input_args = [EXECUTABLE_NAME, SEND_COMMAND] | |
try: | |
process = subprocess.Popen(input_args, | |
stdout=subprocess.PIPE, | |
stdin=subprocess.PIPE, |
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 enum import Enum | |
class TupleEnum(Enum): | |
@classmethod | |
def tuple(cls): | |
return tuple(((elem.name, elem.value) for elem in cls)) |
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 flatten_dict(d): | |
def expand(key, value): | |
if isinstance(value, dict): | |
return [(key + '.' + k, v) for k, v in flatten_dict(value).items()] | |
else: | |
return [(key, value)] | |
items = [item for k, v in d.items() for item in expand(k, v)] | |
return dict(items) |
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 | |
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 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
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
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
from contextlib import contextmanager | |
import pytest | |
@contextmanager | |
def not_raises(exception): | |
try: | |
yield | |
except exception as e: |
OlderNewer