Skip to content

Instantly share code, notes, and snippets.

@binarymatt
binarymatt / gist:3084475
Created July 10, 2012 16:29
raffle chooser
import random
f = open('raffle_recipients.txt')
recipient_list = set([i.strip() for i in f.readlines()])
chooser = random.SystemRandom()
print chooser.sample(recipient_list, 2)
def lookup(records, zipcode):
new_list = []
for item in record:
new_list.append(item/global_lookup(zipcode))
return new_list
map(lookup, [array of lists],[zipcode for range(len([array of lists])))
@binarymatt
binarymatt / gist:3030583
Created July 2, 2012 02:33
pyres decorator
from pyres import ResQ
def job(queue, resq=ResQ(),debug=False):
def wrapper(func):
def enqueue(*args):
if not debug:
class_name = '%s.%s' % (func.__module__, func.__name__)
resq.enqueue_from_string(class_name, queue, *args)
else:
return func(*args)
@binarymatt
binarymatt / s3stream.py
Created June 5, 2012 16:28
stream upload to s3
import sys, StringIO
import argparse
from boto.s3.connection import S3Connection
def read_in_chunks(file_object, chunk_size=1024):
"""Lazy function (generator) to read a file piece by piece.
Default chunk size: 1k."""
while True:
data = file_object.read(chunk_size)
if not data:
break
import sys
def bad_line(line, index):
if line[index] != 'U':
return True
def process_file(f, index):
index = int(index)
bad_records = []
for i,line in enumerate(f):
if line.strip():
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname IN ('public')
AND C.relkind <> 'i'
AND nspname !~ '^pg_toast'
ORDER BY pg_total_relation_size(C.oid) DESC
@binarymatt
binarymatt / redisns.py
Created March 25, 2012 01:19
Redis-py namespace idea
from redis import Redis
class RedisNS(Redis):
def __init__(self, *args, **kwargs):
self.namespace = None
ns = kwargs.pop('namespace', None)
if ns:
self.namespace = '%s:' % ns
super(RedisNS, self).__init__(*args, **kwargs)
def _ns(self, key):
@binarymatt
binarymatt / loader.py
Created February 17, 2012 13:53
load arbitrary functions - note, on the fs test1 and test2 should be in the lib directory
import os, os.path
import lib
import types
def init():
#walk lib and import modules
module_set = set([os.path.splitext(module)[0] for module in os.listdir('lib') if not module.startswith('__init__')])
module_set = [m for m in module_set]
mods = __import__('lib',globals(), locals(), module_set, -1)
return mods, module_set
@binarymatt
binarymatt / gist:1827430
Created February 14, 2012 15:06 — forked from briandailey/dev.md
Stratasan Developer Description

Who We Are

Stratasan is a Nashville-based startup in the healthcare sector that provides reporting tools that derive market intelligence from both public and private healthcare data. For example, we provide reports that show past and future market utilization (e.g., how many patients are expected to have heart problems in zip code 11105?), charges and costs, quality measures, etc.

Why You Should Talk To Us

  • We are a startup in the early stages of growth, so your skills and input will have an impact on the company itself (for good or for evil).
  • All of our products are on the open-source stack (Python/Django/Postgres/PostGIS), rare in healthcare.
  • We do some really neat things with GIS (mapping), if you're into that.
  • We concentrate on data (warehousing, analyzing, summarizing, etc), a burgeoning field.
@binarymatt
binarymatt / signals.py
Created February 12, 2012 16:24
Stripe Webhooks
from django.dispatch import Signal
charge_succeeded = Signal(providing_args=["event"])
charge_failed = Signal(providing_args=["event"])
charge_refunded = Signal(providing_args=["event"])
charge_disputed = Signal(providing_args=["event"])
customer_created = Signal(providing_args=["event"])
customer_updated = Signal(providing_args=["event"])
customer_deleted = Signal(providing_args=["event"])
customer_subscription_created = Signal(providing_args=["event"])