Skip to content

Instantly share code, notes, and snippets.

View kezabelle's full-sized avatar

Keryn Knight kezabelle

View GitHub Profile
@kezabelle
kezabelle / example.py
Created April 11, 2022 15:38
What test methods (python unittest style) call this? A dirty hack of a snippet for me to re-use as I explore a new project.
for frame in inspect.getouterframes(inspect.currentframe()):
if frame.function.startswith("test_"):
repr = "<unknown>"
caller = ""
if "self" in frame.frame.f_locals:
caller = frame.frame.f_locals["self"]
elif "cls" in frame.frame.f_locals:
caller = frame.frame.f_locals["cls"]
if caller:
@kezabelle
kezabelle / setprofile.py
Created May 7, 2021 14:47
lets play at tracking calls and the locals at the point of return ...
import sys
import threading
from operator import itemgetter
from pprint import pprint, saferepr
keyonly = itemgetter(0)
class ZMixin:
def mixin_via_z(self) -> int:
varz = 999
@kezabelle
kezabelle / forms.py
Created May 21, 2020 09:54
Capture all incoming bound data which passed validation into a new querydict, such that a form can return something which may be output as a querystring where appropriate. Obviously it's not always appropriate (eg: POST forms, forms with files etc). Also I can't entirely remember how well it works for `&multiple=of&multiple=the&multiple=samething`
class F(forms.Form):
def to_querydict(self):
if not self.is_bound or not hasattr(self, 'cleaned_data'):
return QueryDict('')
qd = QueryDict('', mutable=True)
get_or_default = self.data.get
if isinstance(self.data, QueryDict):
get_or_default = self.data.getlist
for key, value in self.cleaned_data.items():
prefixed_key = self.add_prefix(key)
@kezabelle
kezabelle / query.py
Created November 29, 2019 12:39
for thnee in #django IRC
# Given this doesn't work
qs = Song.objects
qs = qs.prefetch_related(
db.models.Prefetch(
"releases", queryset=SongRelease.objects.raw(
"""
SELECT id, target FROM tungsten_song_release;
"""
),
)
@kezabelle
kezabelle / django_ors.py
Created November 22, 2019 10:21
Different ways to OR in Django
from django.db.models import Q
from functools import reduce
from operator import or_
values = (
('myfield__iexact', 1),
('myotherfield__iexact', 2),
('myfield__iexact', 3),
('mythirdfield__icontains', 'test'),
)
@kezabelle
kezabelle / bets.md
Created October 4, 2019 10:37
Taking bets on when Django will do certain stuff

As of 2019-10-04

  • Initial ASGI support is confirmed as landing in 3.0 - I think a fully async story won't land (and that will include docs for it all) until 3.2 (LTS)
  • WSGI support will get dropped at 4.2, presuming that's the next LTS after 3.2
@kezabelle
kezabelle / exclude_nullable_blankables.py
Last active September 27, 2019 09:11
discussing excluding nullable charfields with gcbirzan on #django IRC
>>> class TestModel(models.Model):
... test_field = models.CharField(blank=True, null=True)
... class Meta:
... app_label="test"
>>> TestModel.objects.exclude(test_field='').query.__str__()
'SELECT "test_testmodel"."id", "test_testmodel"."test_field" FROM "test_testmodel" WHERE NOT ("test_testmodel"."test_field" = AND "test_testmodel"."test_field" IS NOT NULL)'
>>> import django
>>> django.VERSION
(2, 2, 4, 'final', 0)
>>> from django.conf import settings
@kezabelle
kezabelle / bad.py
Created June 19, 2019 13:57
Django inconsistency around setting TZ into the environ depending on how settings are configured
>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE'] = ''
>>> from django.conf import settings
>>> settings.configure(TIME_ZONE='UTC')
>>> os.environ['TZ']
KeyError...
@kezabelle
kezabelle / models.py
Created June 11, 2019 11:00
Hacking on Django temp tables ORMish stuff
class TempTabler(object):
__slots__ = ("queryset", "model", "name", "create_template", "drop_template")
def __init__(self, query, model, name):
# type: (QuerySet, Type[Model], str) -> None
self.queryset = query
class TempTable(model):
class Meta(model.Meta):
abstract = False
managed = False
db_table = name
@kezabelle
kezabelle / gist:bf445af7c173234091f50c4d378297bc
Created May 24, 2019 09:17
Well this is fun. null byte trying to readlink in Django 2.2.1 ... sometimes.
/Users/kez
/Users/kez/.pyenv
/Users/kez/.pyenv/versions
/Users/kez/.pyenv/versions/3.6.2
/Users/kez/.pyenv/versions/3.6.2/lib
/Users/kez/.pyenv/versions/3.6.2/lib/python3.6
/Users/kez/.pyenv/versions/3.6.2/lib/python3.6/asyncio
/Users/kez/.pyenv/versions/3.6.2/lib/python3.6/asyncio/selector_events.py
/Users
Traceback (most recent call last):