Skip to content

Instantly share code, notes, and snippets.

View lsbardel's full-sized avatar
☀️
magic

Luca Sbardella lsbardel

☀️
magic
View GitHub Profile
@lsbardel
lsbardel / pylink.sh
Created November 11, 2009 22:22 — forked from zvoase/pylink.sh
Link a python module to your site packages. Run dos2unix command on the file before use.
@lsbardel
lsbardel / LazyChoiceField.py
Created November 11, 2009 22:25
Lazy ChoiceField for Django. This field will unwind choices only when a new instance of a Form is created.
class LazyChoiceField(forms.ChoiceField):
'''
A Lazy ChoiceField.
This ChoiceField does not unwind choices until a deepcopy is called on it.
This allows for dynamic choices generation every time an instance of a Form is created.
'''
def __init__(self, *args, **kwargs):
# remove choices from kwargs.
# choices should be an iterable
self._lazy_choices = kwargs.pop('choices',())
@lsbardel
lsbardel / clean.py
Created November 12, 2009 14:13
clean python binary files from a directory structure
import os
def rmgeneric(path, __func__):
try:
__func__(path)
#print 'Removed ', path
return 1
except OSError, (errno, strerror):
print 'Could not remove %s, %s' % (path,strerror)
return 0
@lsbardel
lsbardel / redis-server-for-init.d-startup
Created December 15, 2009 21:01 — forked from mtodd/redis-server-for-init.d-startup
Init.d Redis script for Ubuntu
#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
'''
Script for setting up python-django environment in ubuntu
'''
import os
packages = lambda name: 'apt-get install %s' % name
sysinstall = 'apache2 libapache2-mod-wsgi postgresql python-psycopg2 python-setuptools python-imaging'
@lsbardel
lsbardel / Ordered Hash Table
Created February 2, 2010 13:40
Ordered hash table. The order is given by the order of key-value insertion.
template<typename K, typename V>
struct key2value {
typedef K argument_type;
typedef V mapped_type;
typedef boost::unordered_map<K,V> hash_type;
typedef typename hash_type::value_type result_type;
typedef boost::shared_ptr<hash_type> hash_ptr;
key2value(const hash_ptr& h):hash_(h) {
}
def sortlist(views, func):
def comp(a,b):
if func(a) > func(b):
return 1
else:
return -1
views.sort(comp)
return views
@lsbardel
lsbardel / Iterating on zipped list slices
Created November 14, 2010 13:21
Test the three algorithms for iterating on a zipped sliced lists
import timeit
from itertools import izip, islice
data = range(0,100000)
def loop1():
return zip(data[::2],data[1::2])
def loop2():
return izip(data[::2],data[1::2])
@lsbardel
lsbardel / jsoncls.py
Created November 26, 2010 07:53
A Json encoder/decoder for Python 2.6 or above
import time
from datetime import date, datetime
from decimal import Decimal
import json
def totimestamp(dte):
return time.mktime(dte.timetuple())
def totimestamp2(dte):
def _getnone():
return None
class NonePickler(object):
def __reduce__(self):
return (_getnone,())