Skip to content

Instantly share code, notes, and snippets.

View philpennock's full-sized avatar

Phil Pennock philpennock

View GitHub Profile
@philpennock
philpennock / private_cache_in_func.py
Created January 12, 2011 09:36
Demo of Python one-time eval of function declaration default params, used for caching.
def foo(a, b=None, cache={}):
if b is not None:
cache[a] = b
return b
if a in cache:
return cache[a]
raise KeyError('Bleh, no %s' % a)
@philpennock
philpennock / pdebug.py
Created March 20, 2012 09:30
Sample WSGI debug application
#!/usr/local/bin/python2.7
from __future__ import print_function
import os
import threading
import time
startup_lock = threading.Lock()
COUNTER = None
@philpennock
philpennock / gist:3505352
Created August 28, 2012 23:38
Demonstrate python function *args & **kwargs
>>> def foo(alpha, *args, **kwargs):
... print 'Foo called, alpha = %s' % repr(alpha)
... for a in args:
... print 'Arg: {%s}' % repr(a)
... for k, v in kwargs.iteritems():
... print 'Arg \"%s\" = \"%s\"' % (k, v)
...
>>> foo(234234, 'snert', 'wibble', 'bleurgh', fred=42, barney=-1)
Foo called, alpha = 234234
Arg: {'snert'}
@philpennock
philpennock / exit_check.go
Created October 24, 2012 19:59
Showing interactions of defer, os.Exit and panic in Golang.
package main
import (
"container/list"
"flag"
"fmt"
"os"
"sync"
"time"
)
@philpennock
philpennock / foreach-beginend
Last active May 25, 2022 17:21
Used to iterate over PEM-encoded items in a file
#!/usr/bin/env perl
#
# For a file containing PEM objects or PGP objects or whatever, emit the file
# filtering the objects through a command-line which takes the object on stdin.
#
# foreach-beginend bundle.crt openssl x509 -noout -text
# The filename may be specified as '-' to act as a filter reading from stdin.
#
use strict;
// this is C99
// compile: gcc -std=c99 -ggdb -O3 -Wall guarded_memory_alloc_test.c
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#!/usr/bin/env python3.2
"""
polygon: moo.com polygon puzzle solver
moo.com business cards might have a polygon puzzler on a promotional card in
the pack. This tool finds the words.
"""
__author__ = 'syscomet@gmail.com (Phil Pennock)'
@philpennock
philpennock / git-dessicate
Created November 13, 2013 01:17
git subcommand to work on serious cleanup and space reclamation
#!/bin/sh
SUBDIRECTORY_OK=true
[ -d /opt/local/bin ] && PATH="/opt/local/bin:$PATH"
. "$(git --exec-path)/git-sh-setup"
set -e
git fsck --full
git prune -v
@philpennock
philpennock / git-pushgroup
Created November 13, 2013 01:59
git: Push a branch to a group of remotes
#!/bin/sh
groupname="${1:?Need a group to push to}"
branch="${2:-master}"
for r in $(git config "remotes.${groupname}"); do
echo >&2 "Pushing '${branch}' to '${r}'"
git push "$r" "$branch"
done
#!/bin/sh -u
CMD_NOT_FOUND=127
PORT="${1:-8000}"
python3 -m http.server "$PORT"
ev=$?
[ $ev -ne $CMD_NOT_FOUND ] && exit $ev
python2 -m SimpleHTTPServer "$PORT"