Skip to content

Instantly share code, notes, and snippets.

@saghul
saghul / gist:707899
Created November 20, 2010 15:33
lambda example 2
In [1]: def sum(x, y):
...: return x+y
...:
In [2]: f = lambda n: sum(n,1)
In [3]: f(1)
Out[3]: 2
In [4]: f(3)
@saghul
saghul / gist:707901
Created November 20, 2010 15:35
lambda example 3
In [1]: def sum(x,y):
...: return x+y
...:
In [2]: increment = 1
In [3]: f = lambda n: sum(n,increment)
In [4]: f(1)
Out[4]: 2
@saghul
saghul / gist:707903
Created November 20, 2010 15:37
lambda example 4
In [1]: def sum(x,y):
...: return x+y
...:
In [2]: increment = 1
In [3]: f = lambda n,j=increment: sum(n,j)
In [4]: f(1)
Out[4]: 2
@saghul
saghul / gist:707904
Created November 20, 2010 15:40
lambda example 5
In [1]: from functools import partial
In [2]: def sum(x,y):
...: return x+y
...:
In [3]: increment = 1
In [4]: f = pa
@saghul
saghul / gist:717782
Created November 27, 2010 10:44
Ack vs grep
; Find all Python files containing 'account'
; With grep:
saul@saghul-agp:~/work/src/python-sipsimple$ grep -R --include=*.py account *
; With ack
saul@saghul-agp:~/work/src/python-sipsimple$ ack --python account
@saghul
saghul / gist:724051
Created December 1, 2010 19:22
Phono hello world 1
<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script src="http://s.phono.com/releases/0.1/jquery.phono.js"></script>
</head>
<body>
<input id="call" type="button" disabled="true" value="Loading..." />
<span id="status"></span>
@saghul
saghul / registry1.py
Created January 9, 2011 18:30
Example of a plugin architecture with metaclasses
from application.python.util import Singleton
class PluginRegistry(object):
__metaclass__ = Singleton
def __init__(self):
self.plugins = []
def __iter__(self):
@saghul
saghul / registry2.py
Created January 9, 2011 18:37
Example of a plugin architecture using metaclasses and class decorator
from application.python.util import Singleton
class PluginRegistry(object):
__metaclass__ = Singleton
def __init__(self):
self.plugins = []
def __iter__(self):
@saghul
saghul / coredump.py
Created January 9, 2011 22:39
Example on using resourcelimit module to enable core dumps from Python
from optparse import OptionParser
from time import sleep
def do_something():
while True:
print "I'm doing something"
sleep(5)
@saghul
saghul / database.py
Created January 24, 2011 20:59
Decorator for deferring a function to a thread on Twisted
def defer_to_thread(func):
"""Decorator to run DB queries in Twisted's thread pool"""
def wrapper(*args, **kw):
return deferToThread(func, *args, **kw)
return wrapper