Skip to content

Instantly share code, notes, and snippets.

View titovanton's full-sized avatar

Anton Titov titovanton

View GitHub Profile
def to_dict(table, *keys, **kwargs):
"""
Coverts table like 2D list to dict
Args:
table (list of dict): result of values_list ORM method,
lets interpret each dict item of the list as a `row`.
*keys: keys in order from highest to lowest dimension
kwargs[flat_val_from] (str): if provided - returns flat key -> atomic dict,
other wise returns key -> `row`.
import pdb
import traceback
class DebugOnError:
def __init__(self, suppress=False):
self.suppress = suppress
def __enter__(self):
return None
import logging
log = logging.getLogger(__name__)
def enum_reduce(enum_name, _enum, exclude):
"""
Returns Enum type, based on specified _enum,
but reduced by iterable object of keys.
# Hardcode versions for all packages! <package>==x.y.z
psycopg2-binary
django
# REST Framework
djangorestframework
# Tests
FROM python:3.7
RUN mkdir /app
WORKDIR /app
# make docker user
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
# turn off bell
RUN echo 'set bell-style none' >> ~/.inputrc
from collections import defaultdict, OrderedDict
class DefaultOrderedDict(OrderedDict, defaultdict):
def __init__(self, default_factory=None, *args, **kwargs):
super(DefaultOrderedDict, self).__init__(*args, **kwargs)
self.default_factory = default_factory
class BiDict(object):
'''
Maps both directions: key->value and value->key
Requires both keys and values are unique. If you pass duplication,
then key/value be overwritten and you get not what expected.
'''
origin = {} # counters the {key->key} case
bidict = {}
class FileUploadSerializer(serializers.Serializer):
attachment = serializers.FileField()
class FileUploadView(views.APIView):
'''
FileUploadParser - is some sort of retardnes,
it takes entier http body and parses it as a file 0_0.
Very useful! Specially trash headers, prepended to the text file...
So, just don't use it ;-> Use MultiPartParser instead.
from decimal import Decimal
from django.db import connection
from django.utils.decorators import ContextDecorator
class CountQueries(ContextDecorator):
'''
Either logs(if logger instance has passed) or prints number of queries.