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 typing import Any, Mapping, Tuple, Callable | |
def mapping_to_choices( | |
mapping: Mapping[str, Any], | |
label_factory: Callable[[str], Any] = lambda v: v, | |
recursive: bool = True, | |
) -> Tuple[Tuple[Any, Any], ...]: | |
""" | |
Converts a mapping to Django field choices. |
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
class multiple_indexes(object): | |
""" Mixin """ | |
def __getitem__(self, index): | |
if not isinstance(index, tuple): | |
index = (index, ) | |
getter = super().__getitem__ | |
retval = [] | |
for i in index: | |
part = getter(i) | |
if isinstance(i, slice): |
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 transformation import pipeline, rename_fields, transform_field_value, replace, group_fields | |
if __name__ == '__main__': | |
car_transformer = pipeline( | |
rename_fields({"owner_yob": "owner_year_of_birth"}), | |
transform_field_value("owner_year_of_birth", int), | |
transform_field_value("owner_gender", replace({"female": 0, "male": 1}, None)), | |
group_fields("owner", ("owner_first_name", "owner_last_name", "owner_year_of_birth", "owner_gender")), | |
transform_field_value( |
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
''' | |
https://code.djangoproject.com/ticket/28960 | |
For Python 2.x you need to delete the annotations and related imports. | |
''' | |
from ctypes import c_double, c_int | |
from django.contrib.gis.geos.libgeos import GEOM_PTR | |
from django.contrib.gis.geos.prototypes.topology import Topology |
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
""" | |
Required: | |
* Python 3 (use 3.5+ or remove type hinting) | |
* requests (`pip3 install requests`) | |
""" | |
from typing import Optional | |
from urllib.parse import urljoin | |
from requests import Session |
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
class _Singleton(type): | |
_instances = {} | |
def __call__(cls, *pargs, **kwargs): | |
mcls = cls.__class__ | |
if cls not in mcls._instances: | |
mcls._instances[cls] = super().__call__(*pargs, **kwargs) | |
return mcls._instances[cls] | |
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_to_attr( | |
cache_attr_name, with_cache_flag_kwarg_name='with_cache', | |
default_with_cache_value=True, | |
): | |
""" | |
Декоратор, реализующий кэширование результата вызова метода | |
в атрибут экземпляра |
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 django.shortcuts import get_object_or_404, _get_queryset | |
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist | |
__all__ = ( | |
'get_object_or_create', 'get_object_or_404', 'get_object_or_none', | |
'get_first_or_create', 'get_first_or_404', 'get_first_or_none', | |
) | |
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
""" | |
Реализация `Soft delete` (удаления с поддержкой восстановления). | |
# ================== # | |
# ===== Пример ===== # | |
# ================== # | |
# --- Source: catalog.models --- # | |
from core.models.mixins import CUDMixin, TitleMixin | |
from core.models.soft_delete import SoftDeleteMixin |
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 socket import timeout as TimeoutException | |
from http.client import HTTPConnection, HTTPException | |
from time import sleep | |
from datetime import datetime | |
DEFAULT_TIMEOUT_SEC = 5 | |
DEFAULT_COUNT = None | |
DEFAULT_INTERVAL_SEC = 0.5 |
NewerOlder