Skip to content

Instantly share code, notes, and snippets.

@aliles
aliles / geonames.py
Created July 24, 2011 12:50
Refined Avro benchmarking using Geonames dataset
from collections import namedtuple
import functools
import math
from avro import schema, datafile, io
SCHEMA_STR = """{
"type": "record",
"name": "Feature",
"fields": [
@aliles
aliles / .startup.py
Created July 25, 2011 11:25
Enhancements to Python REPL
"""REPL enhancements for Python interactive interpreter.
This module adds:
- tab completion (requires readline support)
- command history between sessions
- syntactic sugar for 'help(stmt)' via 'stmt?'
- output history
To enable set the environment variables:
@aliles
aliles / nlz.py
Created August 3, 2011 12:59
Calculate the number of leading zeros on an unsigned integer using ctypes.
"""Calculate the number of leading zeros on an unsigned integer.
Uses (or abuses) floating-point post-normalization to calculate the number of
leading zeros on an unsigned integer of 32 bits or less.
Adapted from figure 5-12 of Hacker's Delight by Henry Warren.
http://www.hackersdelight.org/HDcode/nlz.c.txt
"""
import ctypes
import sys
@aliles
aliles / example.py
Created August 12, 2011 13:09
Syntactic sugar to replace '__init__' idiom with class name.
"Usage example of InitProtocl metaclass"
import magic
class Base(object, metaclass=magic.InitProtocol):
"""Example of class using init protocol constructor.
The Base method (Which is named to match the class name.) will be renamed
to __init__ by the metaclass.
"""
@aliles
aliles / example.py
Created August 14, 2011 13:07
An initial '__init__' protocol implementation.
"Usage example of InitProtocl metaclass"
import magic
class Base(object, metaclass=magic.InitProtocol):
"""Empty Base class.
Implements the getattr protocol for a 'null' attribute.
"""
@aliles
aliles / start.py
Created August 18, 2011 12:01
Enhancements to Python REPL
"""REPL enhancements for Python interactive interpreter.
This module adds:
- tab completion (requires readline support)
- command history between sessions
- syntactic sugar for 'help(stmt)' via 'stmt?'
- user 'more' as pager command
- output history
@aliles
aliles / namedtuple.py
Created August 21, 2011 12:06
Implementation of collections.namedtuple without using exec.
"collections.namedtuple implementation without using exec."
from collections import OrderedDict
from keyword import iskeyword
from operator import itemgetter
import itertools
import sys
__all__ = ['NamedTuple', 'namedtuple']
@aliles
aliles / cookbook.rst
Created March 2, 2012 09:36
Fictional Python Packaging Cookbook

Python Packaging Cookbook

Fictional list of recipes for a fictional cookbook on Python packaging.

Basic Packaging

  1. Package a single, pure Python module
@aliles
aliles / self_extract.py
Created March 15, 2012 23:09
A method of creating self extraction for Python scripts.
"""Demonstrate self extraction of data from a Python script.
**YOU PROBABLY DON'T WANT TO DO THIS**
If run with any command line arguments, this script will extract the Zen of
Python from the embedded data at the end of the script and print it to standard
out.
$ python self_extract.py
The Zen of Python, by Time Peters
@aliles
aliles / aliles.der
Created April 10, 2012 13:06
openssl asn1parse -inform der -in $FILE
0:d=0 hl=4 l= 380 cons: SEQUENCE
4:d=1 hl=4 l= 294 cons: SEQUENCE
8:d=2 hl=2 l= 9 prim: INTEGER :D5628EC478688A35
19:d=2 hl=2 l= 13 cons: SEQUENCE
21:d=3 hl=2 l= 9 prim: OBJECT :sha1WithRSAEncryption
32:d=3 hl=2 l= 0 prim: NULL
34:d=2 hl=2 l= 69 cons: SEQUENCE
36:d=3 hl=2 l= 11 cons: SET
38:d=4 hl=2 l= 9 cons: SEQUENCE
40:d=5 hl=2 l= 3 prim: OBJECT :countryName