This file contains hidden or 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 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 |
This file contains hidden or 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
ghci> max 1 3 | |
3 | |
ghci> let x = max 6 | |
ghci> x 9 | |
9 | |
ghci> x 3 | |
6 |
This file contains hidden or 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
## {{{ 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() |
This file contains hidden or 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
results = SomeModel.objects.filter(somefield__isnull=True).order_by("?")[:5] |
This file contains hidden or 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
# 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") |
This file contains hidden or 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 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)): |
This file contains hidden or 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
<select> | |
<option value="1">Original Field Name</option> | |
<!-- etc --> | |
</select> | |
<!-- TO --> | |
<select> | |
<option value="1">Customized Name</option> | |
</select> |
This file contains hidden or 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
$('#id_form_field_example')[0].options[1].text = 'Custom Name'; |
This file contains hidden or 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 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: |
This file contains hidden or 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 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:" |