Skip to content

Instantly share code, notes, and snippets.

View ncopiy's full-sized avatar
💭
🌚 weekend developer

ncopiy ncopiy

💭
🌚 weekend developer
View GitHub Profile
@ncopiy
ncopiy / field.py
Last active February 17, 2019 13:42
JSON Text Field Filter (django + django-filters)
class JSONTextFieldFilter(Filter):
json_field_name = ""
choices = {}
def __init__(self, json_field_name=None, choices=None, **kwargs):
self.choices = choices if choices else {}
self.json_field_name = json_field_name if json_field_name else ""
super().__init__(**kwargs)
def modify_value(self, value):
@ncopiy
ncopiy / defaultattr.py
Created June 26, 2019 16:30
defaultattr (like defaultdict but for attributes)
class defaultattr:
def __init__(self, expected_type):
self.expected_type = expected_type
def __getattr__(self, expected_type):
if isinstance(self.expected_type, type):
return self.expected_type()
return self.expected_type
@ncopiy
ncopiy / field_save_serializer.py
Created June 27, 2019 10:37
serializer without removing fields
import copy
from collections import Mapping
from rest_framework import serializers
from rest_framework.fields import SkipField
from rest_framework.relations import PKOnlyObject
def dict_merge(dct, merge_dct):
""" Recursive dict merge. Inspired by :meth:``dict.update()``, instead of
@ncopiy
ncopiy / asserts.py
Created November 25, 2019 17:03 — forked from kmike/asserts.py
assertNumQueries decorator and context manager
import functools
import sys
import re
from django.conf import settings
from django.db import connection
def shrink_select(sql):
return re.sub("^SELECT(.+)FROM", "SELECT .. FROM", sql)
def shrink_update(sql):
@ncopiy
ncopiy / tree.md
Created June 19, 2023 10:13 — forked from hrldcpr/tree.md
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!