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
# ncd anywhere in your home directory tree! | |
NCDPATH=".:`find ~ -type d | \ | |
grep -v '\/\.' | \ | |
awk '{ print length, $0 }' | \ | |
sort -n | \ | |
cut -d" " -f2- | \ | |
tr '\n' ':'`" | |
alias ncd="CDPATH=$NCDPATH cd" |
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
""" | |
jQuery templates use constructs like: | |
{{if condition}} print something{{/if}} | |
This, of course, completely screws up Django templates, | |
because Django thinks {{ and }} mean something. | |
Wrap {% verbatim %} and {% endverbatim %} around those | |
blocks of jQuery templates and this will try its best |
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 queryset_generator(queryset, chunksize=1000): | |
""" | |
Iterate over a Django Queryset ordered by the primary key | |
This method loads a maximum of chunksize (default: 1000) rows in its | |
memory at the same time while django normally would load all rows in its | |
memory. Using the iterator() method only causes it to not preload all the | |
classes. | |
Note that the implementation of the generator does not support ordered query sets. |
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
# Generously shared with me by marienz on freenode's #python --atdt | |
# Copyright: 2006 Vadim Gelfer <[email protected]>: GPL2 | |
# Copyright: 2007 Marien Zwart <[email protected]>: GPL2/BSD | |
# Copyright: 2009-2010 Brian Harring <[email protected]>: GPL2/BSD | |
# License: GPL2 | |
"""Demand load things when used. | |
This uses :py:func:`Placeholder` objects which create an actual object on |
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
# via shove (in pypi) | |
def synchronized(func): | |
'''Decorator to lock and unlock a method (Phillip J. Eby). | |
@param func Method to decorate | |
''' | |
def wrapper(self, *__args, **__kw): | |
self._lock.acquire() | |
try: |
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
""" | |
Given a Django model instance, get all of its children. | |
""" | |
__author__ = 'Ori Livneh' | |
__email__ = '[email protected]' | |
from django.core.exceptions import ObjectDoesNotExist | |
def get_children(instance): | |
related = instance._meta.get_all_related_objects() |
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
""" | |
Property attributes in memcached | |
:copyright: (c) 2011 by Ori Livneh | |
:license: Public Domain | |
""" | |
from django.core.cache import cache | |
def memcached_property(key_function, timeout=cache.default_timeout, doc=None): |
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 partial_operator(oper, b): | |
""" Returns a partially applied operator with the first parameter | |
unspecified. Example:: | |
>>> less_than_ten = partial_operator(operator.lt, 10) | |
The operator is annotated with an attribute 'b' which contains the | |
partially applied operand. """ | |
@wraps(oper) | |
def wrapped(a): |
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 xmlrpclib | |
pypi = xmlrpclib.ServerProxy('http://pypi.python.org/pypi') | |
def get_pypi_downloads(package, all_versions=False): | |
versions = pypi.package_releases(package, all_versions) | |
return sum(downloads | |
for version in versions | |
for filename, downloads | |
in pypi.release_downloads(package, version)) |
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
# using a tuple for self.buf was the fastest out of handful of | |
# different implementations I played around with. | |
class peekable(object): | |
def __init__(self, iterable): | |
""" | |
Extends iterators with a `peek` method that returns the next element | |
that the iterator will yield. Raises StopIteration if there is no next | |
element. |
OlderNewer