Skip to content

Instantly share code, notes, and snippets.

View j2labs's full-sized avatar

The Ghost Of J2 Labs j2labs

View GitHub Profile
>>> class Foo(object):
... x = 5
... y = Foo()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in Foo
NameError: name 'Foo' is not defined
>>>
>>> import ujson as json
>>> jd = json.dumps
>>> json_is_ujson = True
>>>
>>>
>>> def dumps(data, sort_keys=False):
... if json_is_ujson:
... if sort_keys:
... raise Exception('ujson does not support sort_keys flag')
... return jd(data)
@j2labs
j2labs / gist:3489416
Created August 27, 2012 15:16
Javascript comments
function flash(id) {
// Fades the colored panel into 70% opacity on top of an image stored in
// the value stored in ".img-holder img". This has the effect of a square
// fading into a tinted photograph.
...
}
Flexibility and productivity are still motivators, but they are not the
primary motivators. Safety always trumps other considerations, and
performance also tends to rank very highly in the software-conservative's
value system.
@j2labs
j2labs / gist:2949770
Created June 18, 2012 18:10
functoring
type comparison = Less | Equal | Greater;;
module type ORDERED_TYPE =
sig
type t
val compare: t -> t -> comparison
end;;
module Set =
functor (Elt: ORDERED_TYPE) ->
@j2labs
j2labs / gist:2945706
Created June 17, 2012 20:50
Rollin down the street, rolling braces, sippin on gin n juice. Laid back. With my braces on one line and one line for my braces.
val service = {
path("orders") {
authenticate(httpBasic(realm = "admin area")) { user =>
get {
cacheResults(LruCache(maxCapacity = 1000, timeToIdle = Some(30.minutes))) {
encodeResponse(Deflate) {
completeWith {
// marshal custom object with in-scope marshaller
getOrdersFromDB}}}} ~
@j2labs
j2labs / gist:2877458
Created June 5, 2012 20:02
name of wrapped fun
>>> def deco(method):
... def wrapped(*a, **kw):
... retval = method(*a, **kw)
... return retval
... return wrapped
...
>>>
>>> def foo():
... return 4
...
@j2labs
j2labs / gist:2872483
Created June 5, 2012 03:41
msgpack support in dictshield?
>>> m.to_json(encode=False)
{'owner': '557554ab-757f-421a-9838-8783d8555df3', '_types': ['Media'], '_cls': 'Media', 'title': u'Misc Media'}
>>> p = msgpack.packb(m.to_json(encode=False))
>>> p
'\x84\xa5owner\xda\x00$557554ab-757f-421a-9838-8783d8555df3\xa6_types\x91\xa5Media\xa4_cls\xa5Media\xa5title\xaaMisc Media'
>>> d = msgpack.unpackb(p)
{'owner': '557554ab-757f-421a-9838-8783d8555df3', '_types': ('Media',), '_cls': 'Media', 'title': 'Misc Media'}
>>> Movie(**d)
@j2labs
j2labs / gist:2839669
Created May 30, 2012 23:58
simple partial demo
>>> import functools
>>> def foo(a,b,c):
... return a*b*c
...
>>> times_two = functools.partial(foo, 2)
>>> times_two(3,4)
24
@j2labs
j2labs / gist:2788569
Created May 25, 2012 14:55
gzip with requests
import requests
import gzip
class GzipWrap(object):
"""The normal gzip interface requires writing / reading from gzipped files
on the file system. I think that will cause significant slow-down in
performance so this is a wrapper that implements a file-like interface, but
works entirely from memory instead.