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 django | |
django.setup() | |
from django.utils import translation | |
from django.utils.dates import MONTHS | |
def monthstr(i, lang): | |
translation.activate(lang) | |
return str(MONTHS[i]) |
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 sys, unicodedata | |
def int_to_english(n: int) -> str: | |
""" | |
Convert the given integer into english (only works reliably for numbers between 1 and 31). | |
""" | |
return unicodedata.name(chr(13279 + n)).split()[-1] | |
if __name__ == '__main__': |
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 as r, unicodedata | |
coolwords = [unicodedata.name(chr(119556+i)).split()[-1] for i in range(83)] | |
for w in r.sample(coolwords, r.randrange(9, 15)): | |
print(w, end=r.choice(' \n')) | |
print() |
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 pathlib import Path | |
from django import template | |
def gen_all_templates(): | |
""" | |
Generate the list of all available templates as tuples of 3 values: | |
- the template engine |
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
""" | |
How to update JSONField based on the value of another field. | |
For example: | |
class MyModel(models.Model): | |
name = models.CharField(...) | |
data = models.JSONField() | |
How to update the MyModel table to store the `name` field inside the `data` |
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', |
NewerOlder