Skip to content

Instantly share code, notes, and snippets.

View lost-theory's full-sized avatar

Steven Kryskalla lost-theory

View GitHub Profile
import os
from fnmatch import fnmatch
def find(pat, root):
for path, dirs, files in os.walk(root):
for f in files:
if fnmatch(f, pat):
yield os.path.join(path, f)
if __name__ == "__main__":
@lost-theory
lost-theory / enqueue.py
Created April 8, 2011 22:08
pyres: defining a class and enqueueing it from the same file
class Adder(object):
queue = "Adder"
@staticmethod
def perform(a, b):
return a+b
if __name__ == "__main__":
import random
from pyres import ResQ
@lost-theory
lost-theory / gist:1592662
Created January 11, 2012 02:46
git-branched
#!/usr/bin/env python
"""
git-branched - show metadata about where a branch came from (and optionally,
what commits were on the branch)
Based on the method here:
http://stackoverflow.com/a/4991675/75956
Some examples:
#!/usr/bin/env python
"""
Command line tool for showing the result of a simple python expression.
Some common modules are imported for you (e.g. math, datetime, pprint, etc.).
Exceptions are caught and printed, instead of showing a full traceback.
Examples:
$ py "2+2"
@lost-theory
lost-theory / fab.py
Created February 14, 2012 23:43
fabric elocal - "local" command that raises an exception
#!/usr/bin/env python
from fabric.api import task, local, settings
class CommandFailed(Exception):
def __init__(self, message, result):
Exception.__init__(self, message)
self.result = result
def elocal(*args, **kwargs):
with settings(warn_only=True):
@lost-theory
lost-theory / gist:1990526
Created March 7, 2012 02:35
decorate fabric "lcd" with build step, build #, and time elapsed
import time
from fabric.api import local, lcd
_step = 1
def format_step(msg1, step, desc, msg2, fillchar=">", cols=80):
out = "%s %s %d (%s) %s" % (
fillchar*3,
msg1,
step,
@lost-theory
lost-theory / passenger_wsgi.py
Created April 26, 2012 13:49
passenger WSGI stuff
import sys, os
INTERP = "/home/username/domain/env/bin/python"
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
def testapp(environ, start_response):
import pprint
start_response('200 OK', [('Content-type', 'text/plain')])
yield sys.version
yield pprint.pformat(environ)
@lost-theory
lost-theory / gist:2576645
Created May 2, 2012 13:50 — forked from isa/gist:2571012
Convert in less than 30 lines
data = '''\
A, B, C
A, C, E
E, F, D
D, A, J
E, D, J'''
import itertools, collections
print "\n".join([" "+", ".join(a + (str(b),)) for (a,b) in sorted(collections.Counter(sum([list(itertools.combinations(sorted(x.strip().split(', ')), 2)) for x in data.split('\n')], [])).items(), key=lambda t: t[0])])
#output:
@lost-theory
lost-theory / gist:2871500
Created June 4, 2012 23:35
my new fabric wrapping technique is unstoppable
DESC = "command description goes here"
def usage():
print "%s - %s\n" % (__file__, DESC)
print "Note: use %s --fab-help to get help on arguments specific to" % __file__
print " fabric (roles, hosts, parallel execution, etc.)\n"
new_argv = ['fab', '-f', __file__, '--list']
sys.argv = new_argv
fabric.main.main()
@lost-theory
lost-theory / gist:3005268
Created June 27, 2012 16:35
consume JSON REST API blueprint inside flask app
from flask import Flask, jsonify, Blueprint, current_app
import json
## REST api ###################################################################
api = Blueprint('api', __name__)
@api.route("/users")
def users():
return jsonify(users=[