Skip to content

Instantly share code, notes, and snippets.

@capttwinky
capttwinky / gitGc.py
Created October 21, 2011 23:52
run git gc on all your dirs!
import sys
import os
from subprocess import Popen, PIPE
myWorkDir = '/home/jpmcgrady/workspace'
def runGc(dirNameIn):
os.chdir(dirNameIn)
try:
output = Popen(["git", "gc"], stdout=PIPE).communicate()[0]
@capttwinky
capttwinky / fncObject.py
Created January 27, 2012 23:06
How OO is python? ... it's objects, all the way down
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def myFunction(**kwargs):
return attrSetter(myFunction,**kwargs)
def attrSetter(target,**kwargs):
for k,v in kwargs.iteritems():
setattr(target,k,v)
return True
#include <stdio.h>
#include <string.h>
int main(){int myi=1;char stout[26];char stmid[2];while(myi<=13){sprintf(stmid,"%02X",myi*2);strcat(stout,stmid);myi++;}printf("%s\n",stout);return 0;}
@capttwinky
capttwinky / procparse.py
Created March 20, 2012 22:07
beginnings of a /proc fs -> serialized object script
import re
from collections import namedtuple
from itertools import islice as itslice
def sdict(ntpl, keys, defval = None):
return {k: getattr(ntpl, k, defval) for k in keys}
class ParseHolder(object):
attribs = {}
def __init__(self):
import os
import sys
def findmod(targetMod):
lstOut = []
for myDir in sys.path:
if os.path.isdir(myDir) and "%s.py"%targetMod in os.listdir(myDir):
lstOut.append(myDir)
return lstOut
@capttwinky
capttwinky / getout.py
Created August 13, 2012 20:28
shell command to string
import shlex
import subprocess
from lib_joel import fnprint
class sp_error(StandardError):
pass
def get_out(cmd):
'''execute shell command in subprocess, return result'''
@capttwinky
capttwinky / hfonottest.py
Created August 13, 2012 21:24
test conditions for HFO object
with openHDFS('/my/path/filename','w') as HFO:
HFO.write('string A')
### on inpect /my/path/filename == 'string A'
with openHDFS('/my/path/filename','w') as HFO:
HFO.write('string B')
### on inpect /my/path/filename == 'string B'
with openHDFS('/my/path/filename','a') as HFO:
def powerSet[A](s: Set[A]) =
s.foldLeft(Set(Set.empty[A])) {
(set, element) =>
set union (set map (_ + element))
}
@capttwinky
capttwinky / inline_callback_task_queues.py
Created June 11, 2013 04:11
uses twisted inlinecallbacks generator to make simple task pools.
from twisted.internet import reactor, defer, task
import random
from contextlib import contextmanager
def dummyLRP(choices, max_wait = 30):
"""
dummy long running sort and select proces.
"""
d = defer.Deferred()
to_wait = random.uniform(0, max_wait)