Skip to content

Instantly share code, notes, and snippets.

View j2labs's full-sized avatar

The Ghost Of J2 Labs j2labs

View GitHub Profile
@j2labs
j2labs / gist:2773773
Created May 23, 2012 07:54
A path towards understanding DictShield.
#!/usr/bin/env python
###
### Field Concepts
###
class IntField(object):
def __init__(self):
print 'new int field'
@j2labs
j2labs / gist:2632324
Created May 8, 2012 03:29
what xrange might look like
class xrange(object):
def __init__(self, x):
self.offset = 0
self.x = x
def __len__(self):
return self.x
def __iter__(self):
return self
def next(self):
if self.offset == self.x:
@j2labs
j2labs / gist:2502467
Created April 26, 2012 19:44
abusing python functions
>>> def foo():
... foo.x = 5
... foo.get = lambda: foo.x
... def set(x):
... foo.x = x
... foo.set = set
...
>>> foo()
>>> foo.get()
5
@j2labs
j2labs / gist:2491068
Created April 25, 2012 16:26
Inception
#!/usr/bin/env python
import tornado
import tornado.escape
import tornado.httpclient
import tornado.ioloop
import tornado.web
urls = [
"http://friendfeed-api.com/v2/feed/bret",
==> typespecs (get-deps)
[Building...]
==> typespecs (compile)
==> agner.SC7PqD (compile)
ERROR: OTP release R15B does not match required regex R14
make: *** [compile] Error 1
@j2labs
j2labs / gist:2287164
Created April 2, 2012 20:59
JSON, representing HTTP from Mongrel2
$ ./m2reader.py
34f9ceee-cd52-4b7f-b197-88bf2f0ec378 0 / 571:{"PATH":"/","x-forwarded-for":"127.0.0.1","accept-language":"en-US,en;q=0.8","accept-encoding":"gzip,deflate,sdch","connection":"keep-alive","accept-charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30","host":"localhost:6767","METHOD":"GET","VERSION":"HTTP/1.1","URI":"/","PATTERN":"/"},0:,
@j2labs
j2labs / gist:2276382
Created April 1, 2012 15:28
Possible brubeck templating.py overhaul
from request_handling import WebMessageHandler
### Rendering Mixing
class TemplateRendering(WebMessageHandler):
def render_template(self, template_file,
_status_code=WebMessageHandler._SUCCESS_CODE,
**context):
body = self.application.render_template(template_file, **context or {})
@j2labs
j2labs / gist:2252671
Created March 30, 2012 16:34
Brubeck vs. Tornado - Hello World-a-thon 2012
# All of this testing was done on an AWS m1.large with a single Python
# process. I ran the commands multiple times and the numbers below are
# typical of the behavior, though they are the results of two particular
# cases
# Brubeck with Gevent
$ siege -c500 -t10s localhost:6767/brubeck
Lifting the server siege... done.
@j2labs
j2labs / gist:2212434
Created March 27, 2012 04:02
attempting to make a poisonous Brubeck handler
#! /usr/bin/env python
from brubeck.request_handling import Brubeck, http_response
from brubeck.templating import load_jinja2_env
from brubeck.templating import Jinja2Rendering
app = Brubeck(mongrel2_pair=('ipc://127.0.0.1:9999',
'ipc://127.0.0.1:9998'),
@j2labs
j2labs / gist:2153051
Created March 21, 2012 21:30
New DictShield metaclass system
from dictshield.document import Document, EmbeddedDocument
from dictshield.fields import StringField, UUIDField
from dictshield.fields.mongo import ObjectIdField
class ThoughtsMixin(EmbeddedDocument):
"""Simple document that has one StringField member
"""
thoughts = StringField(max_length=255)
class Meta: