Skip to content

Instantly share code, notes, and snippets.

View peterbe's full-sized avatar

Peter Bengtsson peterbe

View GitHub Profile
class TEST:
def test_foo(self):
self.assertEqual('some very long string bla bla bla' in response.headers['Something'])
@peterbe
peterbe / manage.py
Created April 20, 2011 17:05
allows for setting settings with --settings
#!/usr/bin/env python
import os
import site
import sys
ROOT = os.path.dirname(os.path.abspath(__file__))
path = lambda *a: os.path.join(ROOT,*a)
# Adjust the python path and put local packages in front.
try:
from settings_local import *
except ImportError:
from settings_base import *
@peterbe
peterbe / gist:1018510
Created June 10, 2011 08:56
How do you start all your little python script files?
#!/usr/bin/env python
def main(*args):
return 0
if __name__ == '__main__':
import sys
sys.exit(main(*sys.argv[1:]))
#!/usr/bin/env python
"""
Usage: licence_check.py [directory]
Finds all checked in files that suspiciously lack a MPL licensing header.
Options:
-h, --help show this help message and exit
(c) peterbe@mozilla.com, June 2011
"""
class dict_plus(dict):
def __init__(self, *args, **kwargs):
if 'collection' in kwargs: # excess we don't need
kwargs.pop('collection')
dict.__init__(self, *args, **kwargs)
self._wrap_internal_dicts()
def _wrap_internal_dicts(self):
for key, value in self.items():
@peterbe
peterbe / app.py
Created June 23, 2011 20:52
How I start my tornado app
def main(): # pragma: no cover
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
print "Starting tornado on port", options.port
http_server.listen(options.port)
try:
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
@peterbe
peterbe / gist:1066927
Created July 6, 2011 09:46
authenticated_plus decorator
import functools
import urllib
import urlparse
# taken from tornado.web.authenticated
def authenticated_plus(extra_check):
"""Decorate methods with this to require that the user be logged in."""
def wrap(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
@peterbe
peterbe / gist:1066929
Created July 6, 2011 09:47
authenticated_plus decorator
import functools
import urllib
import urlparse
# taken from tornado.web.authenticated
def authenticated_plus(extra_check):
"""Decorate methods with this to require that the user be logged in."""
def wrap(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
@peterbe
peterbe / tornado_static.py
Created July 12, 2011 14:02
tornado_static
"""
tornado_static is a module for displaying static resources in a Tornado web
application.
It can take care of merging, compressing and giving URLs ideal renamings
suitable for aggressive HTTP caching.
(c) mail@peterbe.com
"""