Skip to content

Instantly share code, notes, and snippets.

View fielder's full-sized avatar

Chris Fielder fielder

View GitHub Profile
@fielder
fielder / iterdiff.py
Last active August 29, 2015 14:22
Generator to run through differences between sequences; originally used to compare binary strings
"""
A method to give a rudimentary 'diff' of 2 sequences.
"""
import collections
# Holds a single set of continuous differences between two sequences.
# The offset gives the offset in the original sequences where the
# differences begin. The a and b will hold the respective differing
# elements.
@fielder
fielder / databuf.py
Created November 9, 2012 22:22
binary stream data buffer read/write object
"""
A data buffer read/write utility. Intended for handling network binary
packet data. Works by using basic python strings which include binary
bytes. The string can be tossed directly into a socket send() or
sendto(). Similarly, can be used to parse out data types out of a binary
string.
Note this isn't restricted to network data; it can be used to read/write
any binary data string.
"""
@fielder
fielder / splittext.py
Created September 22, 2012 14:11
A generator parsing tokens out of a multi-line string.
"""
Another simple token parser. Generator yielding tokens out of a
multi-line string.
Tokens are white-space, quote, or comment dilimited. Quoted tokens
with escaped characters are supported.
Example:
' token1 token2 "quoted token" # comment until end of line '
' token1 "quoted with \"escape\" sequences" '
@fielder
fielder / tokenizer.py
Created July 18, 2012 20:44
Simple token parser. Understands quoted tokens, escape characters, and single-line comments.
"""
A simple token parser.
"""
import string
class Tokenizer(object):
"""
Utility to parse out tokens from a possibly multi-line string.
@fielder
fielder / sunpack.py
Created July 17, 2012 22:30
Struct Unpacker: using the struct module's unpack() method, and a simple string formatter, unpack a binary structure into a dictionary
"""
Struct Unpacker: use a simply formatted string to tell how to unpack
binary data into a dict.
Example: "x <i, y <i, name 8s"
{
"x": a signed 32-bit int,
"y": a signed 32-bit int,
name": a string of 8 characters
}