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 MyModelChoiceField(ModelChoiceField): | |
def label_from_instance(self, obj): | |
return "hello" | |
# or | |
class MyForm(Form): | |
def __init__(self, *args, **kwargs): | |
super(MyForm, self).__init__(*args, **kwargs) | |
if 'myfield' in self.fields: |
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
@contextmanager | |
def copyable(obj): | |
oldval = copy(obj.pk) | |
obj.pk = None | |
yield obj | |
obj.pk = oldval | |
with copyable(obj) as tosave: # I might've forgotten the as part initially. | |
newobj = tosave.save() |
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
querysets = ( | |
Page.objects.filter(...), | |
Something.objects.exclude(...), | |
Etc.objects.filter(...), | |
) | |
results = {qs.count(): (index, qs) for index, qs in enumerate(querysets)} | |
best = max(results) | |
worst = min(results) | |
if best > 0: | |
best_counts = results[best][1] |
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 operator | |
teammembers = request.GET.getlist("teammembers") | |
all_qs = [] | |
for teammate in teammembers: | |
name = teammate.strip().split() | |
# nb: assuming only one space ("forename surname") isn't great. | |
try: | |
first_name = name[0].strip() | |
except IndexError: |
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 collections import namedtuple, defaultdict | |
from operator import itemgetter | |
ClassUsage = namedtuple('ClassUsage', 'cls attrs') | |
AttributeUsage = namedtuple('AttributeUsage', 'name count') | |
attrname = itemgetter(0) | |
attrcount = itemgetter(1) | |
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 os, codecs, functools | |
from collections import namedtuple | |
from django.template.loader import get_template | |
from django.template import TemplateDoesNotExist | |
MISSING = object() | |
def django_handler(path): |
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.template import TemplateDoesNotExist, get_template | |
def path_to_file(request, template_name): | |
# assert template_name in some_safe_list if you want. | |
try: | |
get_template(template_name) | |
except TemplateDoesNotExist: | |
return render("some_fallback.html", request, context) | |
return render(template_name, request, context) | |
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 | |
import os | |
import sys | |
from wrapt import CallableObjectProxy, wrap_object | |
def _dot_lookup(thing, comp, import_path): | |
try: | |
return getattr(thing, comp) | |
except AttributeError: |
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 re | |
import markdown2 | |
z = re.compile('\<(?P<lol>h[1-6])\>(.+?)\<\/(?P=lol)\>') | |
x = """ | |
#### lol | |
## lol | |
<h1>lol</h1> | |
\n``` | |
<h2>heh</h2> | |
``` |
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 re | |
x = r""" | |
/* comment 1 */ | |
/*** | |
comment 2 | |
***/ | |
/*================= | |
comment 3 | |
*/ | |
/*comment 4*/ |