Skip to content

Instantly share code, notes, and snippets.

class Foo(object):
def __init__(self, f):
self.f = f
def __call__(self, *args):
self.f(*args)
@Foo
def dec_me(str):
print "hello %s!" % str
ghci> max 1 3
3
ghci> let x = max 6
ghci> x 9
9
ghci> x 3
6
## {{{ http://code.activestate.com/recipes/52549/ (r3)
class curry:
def __init__(self, fun, *args, **kwargs):
self.fun = fun
self.pending = args[:]
self.kwargs = kwargs.copy()
def __call__(self, *args, **kwargs):
if kwargs and self.kwargs:
kw = self.kwargs.copy()
results = SomeModel.objects.filter(somefield__isnull=True).order_by("?")[:5]
# within the template, the sort doesn't work.
myField = forms.ModelChoiceField(myModel.objects.all().order_by('name'), empty_label="please select an option")
# but this works.
myField = forms.ModelChoiceField(myModel.objects.order_by('name'), empty_label="please select an option")
@qoelet
qoelet / gist:1295911
Created October 18, 2011 16:48
South's freeze_apps
def freeze_apps(apps):
"""
Takes a list of app labels, and returns a string of their frozen form.
"""
if isinstance(apps, basestring): # @kenny: checks that the passed in arg is str/unicode
apps = [apps] # if 'apps' is a str/unicode, convert to list
frozen_models = set()
# For each app, add in all its models
for app in apps:
for model in models.get_models(models.get_app(app)):
<select>
<option value="1">Original Field Name</option>
<!-- etc -->
</select>
<!-- TO -->
<select>
<option value="1">Customized Name</option>
</select>
$('#id_form_field_example')[0].options[1].text = 'Custom Name';
class MyCustomModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
mapping = {
"A1": "B1",
"A2": "B2",
# ... etc
}
if obj.name in mapping.keys():
return "New type - %s" % (mapping[obj.name])
else:
import time
from collections import deque
def normal_list(d):
start = time.time()
r = []
for i in d:
r.append(i)
end = time.time()
print "normal:"