Skip to content

Instantly share code, notes, and snippets.

View defnull's full-sized avatar

Marcel Hellkamp defnull

View GitHub Profile
Verifying I am +defnull on my passcard. https://onename.com/defnull
# Pickle and unpickle objects in a secure way. Useful for cookies.
# Warning: The data is NOT encrypted, but signed. The user can read the data,
# but not change it.
import hmac
import cPickle
def encode(data, key):
''' Encode and sign a pickle-able object. Return a string '''
msg = cPickle.dumps(data, -1).encode('base64').strip()
class ContextLocal(object):
__slots__ = ('_local_init_args', '_local_contexts', '_local_context_ident')
def __new__(cls, *args, **kwargs):
self = object.__new__(cls)
object.__setattr__(self, '_local_init_args', (args, kwargs))
object.__setattr__(self, '_local_contexts', {})
object.__setattr__(self, '_local_context_ident', lambda: 0)
return self
def set_context_ident(self, func):
object.__setattr__(self, '_local_context_ident', func)
@defnull
defnull / bench.py
Created January 20, 2011 21:44
Python Framework Benchmark
import time
import sys
import StringIO
class Bench(object):
def __init__(self, flags=None):
''' Flags:
* cgi: Build a new app every time.
* dny: Return the :name part of a /hello/:name url.
'''
@defnull
defnull / gist:1036791
Created June 20, 2011 22:51
Bottle "Hello World" benchmark
import sys
import bottle
from time import time
app = bottle.Bottle()
@app.route('/')
def hello():
return 'Hello World!'
def bench(n):
@defnull
defnull / bench.py
Created August 28, 2011 18:57
Small WSGI benchmark (removes the HTTP or socket layer)
import sys
import bottle
from time import time
app = bottle.Bottle()
@app.route('/')
def hello():
return 'Hello World!'
def bench(n):
@defnull
defnull / gist:1216497
Created September 14, 2011 13:06
Subclassable ViewPlugin
class ViewPlugin(object):
''' Automatically renders a template for callbacks that return a dictionary.
Subclasses may overrule some methods to implement engine specific
features. They must implement :meth:`prepare_file` and
:meth:`prepare_source`, should implement :meth:`prepare_factory` and may
implement :meth:`assemble_config`, :meth:`is_source`, :meth:`locate` and
additional methods.
'''
@defnull
defnull / example.py
Created March 13, 2012 20:52
Bottle ResourceManager Preview
from bottle import Bottle
app = Bottle()
# Resource configuration (relative to current file)
app.resources.add_path('./data/', base=__file__)
# Override default resources with files in test directory
app.resources.add_path('/home/marc/dev/test_data/', index=0)
@defnull
defnull / dl.py
Created October 4, 2012 21:27
Easiest way to download bottle
wget http://bottlepy.org/bottle.py
curl http://bottlepy.org/bottle.py > bottle.py
pip install bottle
easy_install bottle
python -c 'import urllib; open("bottle.py", "wb").write(urllib.urlopen("http://bottlepy.org/bottle.py").read())'
python3 -c 'import urllib.request; open("bottle.py", "wb").write(urllib.request.urlopen("http://bottlepy.org/bottle.py").read())'
'''
For a tree of nested lists with integer leaves, calculate the sum of all leaves.
'''
def recurse(root):
# Slow because it creates a lot of lists (generators are even slower)
return sum([child if isinstance(child, int) else recurse(child) for child in root])
def recurse2(root):
# Faster because less intermediate objects are created.