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
""" | |
When a model field defines a `choices` attribute, Django doesn't actually | |
generate a database constraint for that, which means it's possible to insert | |
data that doesn't match the choices. | |
See https://adamj.eu/tech/2020/01/22/djangos-field-choices-dont-constrain-your-data/ | |
This command scans your database to try and find which fields have mismatched | |
data. | |
By default it scans all fields of all models of all installed apps, doing one |
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
import random | |
from enum import Enum, EnumMeta, IntEnum | |
class GoatEnumMeta(EnumMeta): | |
def __getitem__(cls, item): | |
if isinstance(item, int): | |
item = cls._member_names_[item] | |
return super().__getitem__(item) |
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
def my_tuple_function(t): | |
""" | |
Return a new tuple of the same length containing only zeroes. | |
""" | |
tuple_class = type(t) | |
return tuple_class(*(0 for _ in t)) # works for namedtuple | |
return tuple_class((0 for _ in t)) # works for plain tuple |
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://mobile.twitter.com/dabeaz/status/1199367834691932160 | |
""" | |
As a oneliner: | |
ᚐ = type('ᚐ',(object,),{'ᚐ':{},'__getattr__':lambda s,a:s.ᚐ[a],'__setattr__':lambda s,a,v:s.ᚐ.__setitem__(a,s.ᚐ[a]+v)if a in s.ᚐ else s.ᚐ.__setitem__(a,v)})() | |
for ᚐ.i, ᚐ.s in [(600, 'sa'), (60, 'ta'), (6, 'n')]: | |
pass |
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
import argparse | |
from functools import partial | |
import unicodedata | |
# based on https://www.fileformat.info/info/unicode/category/Nd/list.htm | |
ALPHABETS = { | |
'ARABIC-INDIC', | |
'EXTENDED ARABIC-INDIC', | |
'NKO', | |
'DEVANAGARI', |
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 MyClass: | |
my_attr = 42 | |
print(MyClass.my_attr) | |
print(MyClass.my_attr) | |
print(getattr(MyClass, 'my_attr')) | |
print(getattr(MyClass, 'my_attr')) |
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
03000000-1 | |
03100000-2 | |
03110000-5 | |
03111000-2 | |
03111100-3 | |
03111200-4 | |
03111300-5 | |
03111400-6 | |
03111500-7 | |
03111600-8 |
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.forms.widgets import MultiWidget | |
def form_to_POST(form): | |
""" | |
Given a form, convert its initial data into a dict that can simulate | |
the request.POST that would be generated if that form was submitted as-is. | |
This will respect the form's prefix and will convert all field values to | |
strings. | |
""" |
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
Python 2.7.3 (default, Mar 13 2014, 11:03:55) | |
[GCC 4.7.2] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
(InteractiveConsole) | |
>>> from django.core.mail import send_mail | |
>>> send_mail('hello', 'world', '[email protected]', [u'Báptiste [asdf] <[email protected]>']) | |
Traceback (most recent call last): | |
File "<console>", line 1, in <module> | |
File "/path/to/venv/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail | |
return mail.send() |
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
""" | |
ValueError: not enough values to unpack (expected 3, got 2) | |
ValueError: too many values to unpack (expected 3) | |
""" | |
def unpack(iterable, length): | |
i = iter(iterable) | |
ret = [x for x, _ in zip(i, range(length))] |