Skip to content

Instantly share code, notes, and snippets.

View atdt's full-sized avatar
💭
I may be slow to respond.

Ori Livneh atdt

💭
I may be slow to respond.
  • Google
  • New York City
View GitHub Profile
@atdt
atdt / ncd.sh
Created May 26, 2011 00:24
ncd to cd anywhere in your home directory tree
# 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"
"""
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
@atdt
atdt / queryset_generators.py
Created June 2, 2011 02:19 — forked from dbrgn/queryset_generators.py
queryset_generator and queryset_list_generator
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.
@atdt
atdt / lazy_import.py
Created July 18, 2011 19:27
Lazy Import
# 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
@atdt
atdt / synchronized.py
Created August 17, 2011 20:38
decorator to lock and unlock a method
# 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:
@atdt
atdt / get_children.py
Created September 2, 2011 20:24
get all children of a django instance
"""
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()
@atdt
atdt / memcached_descriptors.py
Created October 27, 2011 18:13
Memcached Property Attributes
"""
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):
@atdt
atdt / partial_operator.py
Created October 29, 2011 08:21
partial operator
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):
@atdt
atdt / pypi_download_count.py
Created December 23, 2011 22:35
total pypi downloads by package
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))
@atdt
atdt / peek.py
Created December 29, 2011 04:51
peekable iterator
# 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.