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 | |
import logging | |
import requests | |
logger = logging.getLogger(__name__) | |
class HealthCheck: | |
def __init__(self, ping_url:str, time_out:int=30) -> None: | |
self.ping_url = ping_url |
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 annotate_for_search( | |
queryset: QuerySet, | |
seach_value: str, | |
fields_weights_map: Dict[str, float], | |
annotation_name='search_rank', | |
order_descending_by_rank=True, | |
exclude_non_matched=False | |
): | |
''' | |
Annotates the `queryset` with relevancy of each row to the `search_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
sudo apt-get install graphviz | |
poetry add pydotplus django-extensions | |
INSTALLED_APPS = ( | |
... | |
'django_extensions', | |
... | |
) | |
python manage.py graph_models -a -o myapp_models.png |
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 django.db import models | |
from django.core.cache import cache | |
class Config(models.Model): | |
CACHE_KEY = 'project_config' | |
class Meta: | |
ordering = ['id'] | |
def save(self, *args, **kwargs): |
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 dataclasses import dataclass, asdict | |
import json | |
@dataclass | |
class Cache: | |
'''this class is used as the database''' | |
some_field: int | |
@staticmethod |
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 typing import List, Iterable | |
class DjangoChoice: | |
"""A class to simplify usage of choices in django model fields. This class can actually be used as small database.""" | |
def get_attr_dict(self): | |
res = dict() | |
for atr in type(self).__dict__.keys(): | |
if not atr.startswith("_"): |
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 functools | |
def log_with_args(logger): | |
def dec(f0): | |
@functools.wraps(f0) | |
def wrapper(*args, **kwargs): | |
called_with = list(zip(f0.__code__.co_varnames, args)) + list(kwargs.items()) | |
called_with = [f'{v1}={v2}' for v1, v2 in called_with] | |
called_with = ', '.join(called_with) | |
logger.info(f'received {f0.__name__} with {called_with}') |
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
# pip install django-admin-rangefilter | |
from rangefilter.filters import NumericRangeFilter | |
from django.utils import timezone | |
class MinutesSinceRangeFilter(NumericRangeFilter): | |
def __init__(self, *args, **kwargs) -> None: | |
super().__init__(*args, **kwargs) | |
self.title = f'minutes since {self.title}' |