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:1573951
Created January 7, 2012 06:05
API output for a REST call with multiple items.
$ curl http://localhost:6767/todo/ | python -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 241 100 241 0 0 54463 0 --:--:-- --:--:-- --:--:-- 117k
[
{
"_cls": "Todo",
"_id": "84b38d14-4a11-439a-8e3f-bbe5a499714b",
"_types": [
"Todo"
@j2labs
j2labs / gist:1573929
Created January 7, 2012 05:54
Backbone API
#!/usr/bin/env python
from brubeck.request_handling import Brubeck, AutoAPIBase, WebMessageHandler
from brubeck.queryset import DictQueryset
from brubeck.templating import Jinja2Rendering, load_jinja2_env
from dictshield.document import Document
from dictshield.fields import StringField, IntField, BooleanField
@j2labs
j2labs / gist:1436934
Created December 6, 2011 05:54
Hmm... I can't remember where I got to last night.
$ ./embedded_documents.py
Traceback (most recent call last):
File "./embedded_documents.py", line 130, in <module>
class Order(EmbeddedDocument):
File "./embedded_documents.py", line 133, in Order
line_items = ListField(EmbeddedDocumentField(Product))
File "/Users/jd/Projects/dictshield/dictshield/fields/compound.py", line 194, in __init__
'WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOORD')
dictshield.base.ShieldException: Invalid embedded document class provided to an EmbeddedDocumentField - <class '__main__.Product'>:WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOORD
@j2labs
j2labs / gist:1410555
Created November 30, 2011 20:00
imports happen once
>>> import time
>>> def time_it(fun):
... start = time.time()
... fun()
... end = time.time()
... print 'Time:', end - start
...
>>> def foo():
... import logging
... import multiprocessing
@j2labs
j2labs / arity_check.js
Created September 28, 2011 07:17
Node.js friendly form of the arity checking decorator for javascript
exports.arity_decorator = function(fun) {
function wrapped() {
if(arguments.length != fun.length)
throw new Error("Y U NO USE RIGHT?!")
fun.apply(this, arguments)
}
return wrapped;
}
@j2labs
j2labs / gist:1247166
Created September 28, 2011 06:46 — forked from gtzilla/gist:1247115
arity binding decorator for javascript
function arity_decorator(fun) {
function wrapped() {
if(arguments.length != fun.length)
throw new Error("Y U NO USE RIGHT?!")
fun.apply(this, arguments);
}
return wrapped;
}
function foo(x, y) {
> function foo(x,y) {
... args = Array.prototype.join.call(arguments, ",");
... console.log('X:' + x + ' Y:' + y + ' Args:' + args)
... }
> foo(1,2)
X:1 Y:2 Args:1,2
> foo(1,2,3,4,5)
X:1 Y:2 Args:1,2,3,4,5
@j2labs
j2labs / brubeck_installer.sh
Created September 26, 2011 03:30
Brubeck Installer for Ubuntu
#!/bin/sh
###
### Settings
###
ZMQ_VERSION="zeromq-2.1.9"
MONGREL2_VERSION="mongrel2-1.7.5"
PREV_DIR=$PWD
SRC_DIR=$HOME/src
@j2labs
j2labs / gist:1120681
Created August 2, 2011 17:12
one shot continuation, sorta, using python greenlets
#!/usr/bin/env python
from greenlet import greenlet
def test1(coro):
print 'test1: 1'
coro_self = greenlet.getcurrent()
coro.switch(coro_self)
raise Exception('Hey you cant do this!')
@j2labs
j2labs / gist:1088546
Created July 18, 2011 04:31
Concurrent pymongo inserts with Eventlet
#!/usr/bin/env python
###
### Concurrent pymongo inserts
###
import eventlet
pymongo = eventlet.import_patched('pymongo')
### Setup database connection