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
# fork of https://github.com/django/django/blob/b7c7209c67f742eda8184c46f139e0e1cb16a1f4/django/core/management/commands/shell.py | |
import os | |
import select | |
import sys | |
import traceback | |
from django.core.management import BaseCommand, CommandError | |
from django.utils.datastructures import OrderedSet | |
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 inspect | |
_SENTINEL = object() | |
class super: | |
"""The built-in super "function" re-implemented.""" |
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
""" | |
Just some code that needlessly converts unicode codepoints to UTF-8. | |
Example: | |
$ python utf8ify.py U+2728 | |
Bytes: 0xe2 0x9c 0xa8 | |
Text: ✨ | |
$ python utf8ify.py U+1F3F3 U+FE0F U+200D U+1F308 |
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
""" | |
Tool that names each unicode character based on their name or their aliases | |
Must be run from the cpython repo root directory: | |
https://github.com/python/cpython/tree/3.10 | |
Relies on Tools.unicode being an importable path due to implicit packages | |
""" | |
from contextlib import redirect_stderr, redirect_stdout | |
from io import StringIO |
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
""" | |
Script to print all Unicode flag emoji are also a valid flag when reversed. | |
Output of this script: | |
🇦🇬 (Antigua and Barbuda) reverses to 🇬🇦 (Gabon) | |
🇦🇱 (Albania) reverses to 🇱🇦 (Lao People's Democratic Republic) | |
🇦🇲 (Armenia) reverses to 🇲🇦 (Morocco) | |
🇦🇶 (Antarctica) reverses to 🇶🇦 (Qatar) | |
🇦🇸 (American Samoa) reverses to 🇸🇦 (Saudi Arabia) |
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
""" | |
while n < 10 and chunk := f.read(256): # SyntaxError | |
while n < 10 and (chunk := f.read(256)): # Works | |
while chunk := f.read(256) and n < 10: # chunk will be a boolean 😮 | |
""" | |
from io import StringIO |
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 Rectangle: | |
def __init__(self, width, height): | |
self.width, self.height = width, height | |
def __repr__(self): | |
return f"Rectangle({self.width}, {self.height})" | |
@property | |
def area(self): |
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 easy_repr(obj): | |
""" | |
Function to find the type of an object and its attributes | |
Example: | |
>>> class Point: | |
... def __init__(self, x, y, z, color=None): | |
... self.x, self.y, self.z = x, y, z | |
... self.color = color |
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
""" | |
Demonstration of how slow sum can be for flattening lists-of-lists | |
Output on my machine: | |
0.019118324998999014 itertools.chain.from_iterable | |
0.03325132900135941 comprehension | |
10.947899631002656 sum | |
Using sum on a list results in a loop inside a loop (a new list is made for | |
each new sub-list) so the more sub lists there are, the bigger the timing |
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
# django.template.defaulttags.autoescape rewritten using Python 3.10's match/case statement | |
@register.tag | |
def autoescape(parser, token): | |
""" | |
Force autoescape behavior for this block. | |
""" | |
# token.split_contents() isn't useful here because this tag doesn't accept variable as arguments | |
match token.contents.split(): | |
case [_, ('on' | 'off') as arg]: | |
nodelist = parser.parse(('endautoescape',)) |
NewerOlder