Skip to content

Instantly share code, notes, and snippets.

View nfarring's full-sized avatar

Nathan Farrington nfarring

View GitHub Profile
@nfarring
nfarring / barcode.xsd
Created September 4, 2011 06:30
Barcode XML Schema
<xs:simpleType name="UPC-E-Barcode">
<xs:annotation>
<xs:documentation>
Reference: http://www.barcodeisland.com/upce.phtml
The UPC-E barcode has 7 digits.
</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:token">
<xs:pattern value="[0-9]{7}"/>
</xs:restriction>
@nfarring
nfarring / gist:1175192
Created August 27, 2011 09:38
Python script template
#!/usr/bin/env python
# http://docs.python.org/dev/library/argparse.html
import argparse
if __name__=='__init__':
parser = argparse.ArgumentParser(description='')
args = parser.parse_args()
print(args)
@nfarring
nfarring / bind.c
Created August 24, 2011 22:27
Templates for C sockets
#include <stdio.h> // PERROR(3)
#include <stdlib.h> // EXIT(3)
#include <sys/socket.h> // BIND(2)
int retval = bind(
/* int sockfd */
/* const struct sockaddr *addr */
/* socklen_t addrlen */
);
if (retval == -1) {
@nfarring
nfarring / gist:1169363
Created August 24, 2011 21:52
Register a signal handler for SIGINT.
#include <stdlib.h>
#include <signal.h>
/*
* Gracefully exits when Ctrl+C is pressed.
*/
void sigint_handler(int signum) {
exit(0);
}
@nfarring
nfarring / wordsize.c
Created August 24, 2011 19:22
Prints the sizes of various integral types in the C language on a POSIX/Linux/UNIX machine.
#include <stdint.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("sizeof(char): %lu\n", sizeof(char));
printf("sizeof(unsinged char): %lu\n", sizeof(unsigned char));
printf("sizeof(signed char): %lu\n", sizeof(signed char));
printf("sizeof(int8_t): %lu\n", sizeof(int8_t));
printf("sizeof(uint8_t): %lu\n", sizeof(uint8_t));
printf("sizeof(short): %lu\n", sizeof(short));
@nfarring
nfarring / index.html
Created July 29, 2011 06:17
Ubuntu Apache default index.html file
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
@nfarring
nfarring / gist:1110611
Created July 27, 2011 23:41
Python thread example
#!/usr/bin/env python
# This script creates a daemon thread named 'watchdog_timer' that runs the watchdog_timer() function.
# The main thread sleeps in an infinite loop until you press control-C.
# Since the watchdog_timer thread is a daemon, exiting the main thread will exit the entire process.
import threading
import time
def watchdog_timer():
@nfarring
nfarring / gist:1078911
Created July 12, 2011 20:32
Script to install lastest version of Python on a Ubuntu/Debian system.
# Make sure we have the basics.
sudo apt-get install build-essential
# Install the build dependencies for Python.
# Replace with python2.7 if your distro supports it.
sudo apt-get build-dep python2.6 python-gdbm python-bsddb3
# Use pythonbrew to actually compile Python.
curl -kL http://xrl.us/pythonbrewinstall | bash
pythonbrew install 2.7.2
@nfarring
nfarring / typecheck.py
Created January 28, 2011 13:55
Python runtime type checking decorator function
import functools
def typecheck(*args1,isclassmethod=False):
"""Python is a language with typed objects, but untyped references. This means that there is no compiler to help catch type errors at design time. This typecheck function acts as a function decorator so that you can declare the type of each function or method argument. At runtime, the types of these arguments will be checked against their declared types.
Example:
@typecheck(types.StringType, Decimal)
def my_function(s, d):
pass
@nfarring
nfarring / TemplateClass.py
Created January 28, 2011 01:43
A template for a Python module that implements a single Python class.
# Copyright (c) 2011, Nathan Farrington
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright