Skip to content

Instantly share code, notes, and snippets.

@progrium
progrium / gist:1175906
Created August 27, 2011 21:56
UpgradableWSGIHandler for gevent
import gevent.pywsgi
class UpgradableWSGIHandler(gevent.pywsgi.WSGIHandler):
"""Upgradable version of gevent.pywsgi.WSGIHandler class
This is a drop-in replacement for gevent.pywsgi.WSGIHandler that supports
protocol upgrades via WSGI environment. This means you can create upgraders
as WSGI apps or WSGI middleware.
If an HTTP request comes in that includes the Upgrade header, it will add
@progrium
progrium / cluster.py
Created September 22, 2011 02:56
A distributed group membership module
"""A distributed group membership module
This provides distributed group membership for easily building clustered
applications with gevent. Using this in your app, you just provide the IP
of another node in the cluster and it will receive the IPs of all nodes in
the cluster. When a node joins or drops from the cluster, all other nodes find
out immediately.
The roster is managed by a leader. When you create a cluster, you tell the
first node it is the leader (by simply pointing it to its own IP). As you
@progrium
progrium / DisketteLabel.xml
Created October 23, 2011 11:42
Dymo label XML for an image filling a Diskette (Large) label
<?xml version="1.0" encoding="utf-8"?>
<DieCutLabel Version="8.0" Units="twips">
<PaperOrientation>Landscape</PaperOrientation>
<Id>Label</Id>
<PaperName>30258 Diskette</PaperName>
<DrawCommands>
<RoundRectangle X="0" Y="0" Width="3060" Height="3960" Rx="270" Ry="270"/>
</DrawCommands>
<ObjectInfo>
<ImageObject>
@progrium
progrium / test.badge.js
Created October 23, 2011 12:01
RaphaelJS script to test badges
var badge = Raphael(200, 0, 225, 187);
// Header
var attrs = {'fill': '#f90', 'stroke': 'none', 'font-size': 11, 'font-family': 'helvetica'};
badge.rect(5, 3, 225, 14).attr(attrs);
badge.circle(212, 8, 16).attr(attrs);
var textAttrs = {'fill': '#fff', 'font-size': 11, 'font-family': 'helvetica', 'text-anchor': 'end'};
var text = badge.text(198, 9, "SuperHappyDevHouse");
text.attr(textAttrs);
var num = badge.text(211, 9, "48");
@progrium
progrium / badger.html
Created October 23, 2011 14:54
Proof of concept for entirely self-contained HTML badge printing system
<html>
<head>
<style type="text/css">
svg { margin-left: -2000px; }
</style>
<script type="text/javascript" src="http://github.com/DmitryBaranovskiy/raphael/raw/master/raphael-min.js"></script>
<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/rgbcolor.js"></script>
<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/canvg.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.latest.js" type="text/javascript" charset="UTF-8"> </script>
@progrium
progrium / bash
Created November 13, 2011 11:39
parse_http for nc http
# writes host to a host file
nc -l -p 9998 -c "./parse_http>/tmp/cgi;. /tmp/cgi>/dev/null && echo $HTTP_HOST > host"
# pipemill the request body
nc -l -p 9998 -c "./parse_http>/tmp/cgi;. /tmp/cgi | while read line; do something; done"
# route on the path
# uhh, well, sure, just use if on $HTTP_PATHINFO
def pseudo_code_example():
"""Compare to the current implementation of start()"""
class Service:
def start(self, block_until_ready=True):
self.state("start_services")
for child in self._children:
child.start(block_until_ready)
self.state("services_started")
ready = not self.do_start()
if not ready and block_until_ready:

Ginkgo Tutorial: Introduction

Frameworks

Remember what it was like building web apps before modern web frameworks? It was a lot of work! The structure and conventions that web frameworks gave us brought on a new level of rapid development, best practices, and collective understanding of the problem domain. Frameworks lowered the barrier to entry in building web sites and web applications.

ws = new WebSocket("ws://localhost:8181/v1/metric/get")
ws.onmessage = function (data) { console.log(data.data) }
ws.send(JSON.stringify({metric: "biz.calls", aggregate: "sum", step: 20, start: "2012-02-15T01:05:00Z", stop: "2012-02-15T02:05:00Z"}))
class GlobalContext(object):
"""Context manager mixin for stackable singletons
Use this mixin when a class has a global singleton set somewhere that can
be temporarily set while in the context of an instance of that class::
class Foo(GlobalContext):
instance = None # where we'll keep the singleton
singleton_attr = (Foo, 'instance') # tell mixin where it is