Skip to content

Instantly share code, notes, and snippets.

View chadselph's full-sized avatar

Chad Selph chadselph

View GitHub Profile
@chadselph
chadselph / webmaildebug.py
Created October 29, 2010 20:33
yet another tool for debugging emails your application is sending. This script accepts email as an SMTP server and lets you read the emails contents via http. It can use sqlite3 or a python list to save data.
import asyncore, asynchat
import email
import re
import smtpd
import sqlite3
import socket, string
import StringIO, mimetools
# HTTP based on examples at http://effbot.org/librarybook/asynchat.htm
class HTTPChannel(asynchat.async_chat):
for scope in path_scopes:
filter_constraints = cgi.parse_qs(scope.params.get('params', ''))
filter_keys = set(request.args.keys()) & set(filter_constraints.keys())
def arg_satisfies_filter(f):
return set(request.args[f]).issubset(set(filter_constraints[f]))
if all(arg_satisfies_filter(f) for f in filter_keys):
# if for every parameter, the values are a subset of allowed params, we good!
return True
@chadselph
chadselph / reqeusts_bug.py
Created October 20, 2011 03:22
requests gevent demo
"""
requests uses a singleton AuthManager, which when used with gevent's monkey patching
(or presumably, the threading module) can be set by one thread, and read by another.
The AuthManager maps urls to credentials which assumes for any url you will always
request it with the same credentials. This turned out to be false for us, and thus,
the bug. Here's code to reliably reproduce it.
"""
import base64
import gevent.monkey
@chadselph
chadselph / jscanary.py
Created October 20, 2011 20:49
redirect caching experiments
from flask import Flask
import flask
from gevent.pywsgi import WSGIServer
app = Flask(__name__)
app.debug = True
color = 'green'
@chadselph
chadselph / patternmatching.py
Last active February 15, 2024 14:45
Functional language style pattern matching in python with a decorator
from collections import defaultdict
class BadMatch(NameError):
"""Exception when your args don't match a pattern"""
pass
class Any(object):
"""
>>> 'wutup' == Any()
True
@chadselph
chadselph / webui.py
Created February 24, 2012 17:26
simpler solution
import threading
from wsgiref.simple_server import make_server
from mylongtask import progress_status, run_task
# simple wsgi app; could replace with one from flask with templates,
# url dispatching, etc.
def simple_app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return [str(progress_status)]
@chadselph
chadselph / 1-original.md
Created April 12, 2012 23:39
gsm encoding for python

(From http://stackoverflow.com/questions/2452861/python-library-for-converting-plain-text-ascii-into-gsm-7-bit-character-set, ran out of space in the comment section.)

Running the original file does not work in either Python 2 or 3.

In Python2, the program prints this:

64868d8d903a7390938d85

(which is wrong) because it is using the indexes of gsm which do not map to the index of their GSM encodings due to the fact that it is a bytestring with some characters taking up multiple bytes. gsm is actually equal to

@chadselph
chadselph / okcupidgirl.py
Created January 3, 2013 23:36
TCP server that listens for connections but never replies. Useful for testing certain kinds of timeout behaviors.
# okcupidgirl.py ignores you
from __future__ import print_function
import argparse
import socket
import select
def listen(port):
print("Listening on port {}".format(port))
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@chadselph
chadselph / runningwc.py
Last active December 11, 2015 21:09
Like `wc -l` but for `tail -f`. Example: tail -f some.log | grep ERROR | ./runningwc.py -i 5
#!/usr/bin/python -u
from twilio.rest import TwilioRestClient
import sys
import time
import threading
import argparse
total = 0
class Reporter(threading.Thread):
/**
* SCALA!!
*/
object DisjointGraphs extends App {
val disjoints = findThem(List((1,2), (2,3), (5,6), (7,8), (1,8)))
println(disjoints)
def findThem(edges: List[(Int, Int)]) = {
val initial = List[Set[Int]]() // empty list of int sets