Skip to content

Instantly share code, notes, and snippets.

View ryanwitt's full-sized avatar

Ryan Witt ryanwitt

  • New York, NY
View GitHub Profile
#!/usr/bin/python
#jh- pasted from:
# http://peerit.blogspot.com/2007/07/multipartposthandler-doesnt-work-for.html
####
# 02/2006 Will Holcomb <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# $Id: pcap.py 26 2006-11-21 01:18:55Z dahelder $
"""Libpcap file format."""
import sys, time
import dpkt
TCPDUMP_MAGIC = 0xa1b2c3d4L
PMUDPCT_MAGIC = 0xd4c3b2a1L
#!/usr/bin/python
# -*- coding: utf8 -*-
import hashlib, random, timeit
def generate_it(bytes):
return ''.join(chr(random.getrandbits(8)) for x in xrange(bytes))
def hash_it(stuff):
return hashlib.md5(stuff).hexdigest()
@ryanwitt
ryanwitt / si_prefix.js
Created November 8, 2010 17:32
si_prefix.js
function si_prefix(size) {
var suffixes =['', 'k', 'M', 'G', 'T', 'P', 'E'];
if (size === 0) return '0';
base = Math.log(size) / Math.log(1000);
return suffixes[Math.floor(base)];
}
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand, CommandError
from django.core import serializers
from django.utils.datastructures import SortedDict
from optparse import make_option
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--format', default='json', dest='format',
@ryanwitt
ryanwitt / gist:2911560
Created June 11, 2012 17:46
Confusion matrix for a logistic glm model in R. Helpful for comparing glm to randomForests.
confusion.glm <- function(data, model) {
prediction <- ifelse(predict(model, data, type='response') > 0.5, TRUE, FALSE)
confusion <- table(prediction, as.logical(model$y))
confusion <- cbind(confusion, c(1 - confusion[1,1]/(confusion[1,1]+confusion[2,1]), 1 - confusion[2,2]/(confusion[2,2]+confusion[1,2])))
confusion <- as.data.frame(confusion)
names(confusion) <- c('FALSE', 'TRUE', 'class.error')
confusion
}
@ryanwitt
ryanwitt / huffman.py
Last active November 29, 2016 14:41
silly huffman coding example
__all__ = (
'frequency_histogram',
'endode_simple',
'decode_simple',
'huffman_tree',
'encode_character',
'encode_huffman',
'hist2dot',
'tree2dot',
)
@ryanwitt
ryanwitt / dijkstra.py
Created August 3, 2012 05:46
Dijkstra for the CSH in-class graph
#
# Version 1
#
# - data stored as node and edge sets
# - frontier is set of (src, dst, weight)
# - far too many O(n) loops
#
# Load graph
nodes = set()
@ryanwitt
ryanwitt / request.py
Created August 14, 2012 03:32
fun simulating django request objects
class Request(object):
def __init__(self, host, path):
self.get_host = lambda:host
self.get_full_path = lambda:path
@ryanwitt
ryanwitt / lcs.py
Created September 2, 2012 23:51
a naïve longest common substring impl
def static_lcs(one,two):
lcs, candidate = '', ''
for o,t in zip(one, two):
if o == t:
candidate = candidate + o
if len(candidate) > len(lcs):
lcs = candidate
else:
candidate = ''
return lcs