Skip to content

Instantly share code, notes, and snippets.

@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'''
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 / 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):
#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 / 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
@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 / rplparse.py
Created September 28, 2011 20:59
an rplish expression parser
# -*- coding: latin-1 -*-
from decimal import Decimal
import re
import operator as op
d = lambda n: Decimal(n)
lstFns=['+','-','*','/']
dictFns=dict(zip(lstFns,[op.__add__,op.__sub__,op.__mul__,op.__truediv__]))
@capttwinky
capttwinky / threadping.py
Created September 28, 2011 19:46
threaded pinger
#!/usr/bin/env python
from Queue import Queue
from threading import Thread
from time import time, sleep
from subprocess import Popen, PIPE
class HostRangeGenerator(object):
def __init__(self, lstIn):
self.myList = [range(256) if x =="*" else [x] if hasattr(x,'real') else x for x in lstIn]
self.A, self.B, self.C, self.D = self.myList
@capttwinky
capttwinky / gist:1245740
Created September 27, 2011 17:51
example of adding sort functins to an object & using in a lambda
import time
class myNote(object):
def __init__(self, myInt):
self.name = myInt
self.provided_time = None
self.mytime = time.time()
#we're going to consider every thing above here imported
@capttwinky
capttwinky / gist:1245494
Created September 27, 2011 16:11
example of lambdas in sorts
def main():
lstIn = "dog,horse,cat,trouser,shark,artichoke,constantine,Christmas,Antartica,Ninja,Sanitizer".split(",")
lst1 = sorted(lstIn) #looks ok, but sorts on ord(c)
print lst1 #uppers before lowers
lst2 = sorted(lstIn, key=lambda x: x.lower()) #does a case insenstive sort
print lst2 #what you'd expect from an alphabetic sort
lstIn.sort(key=lambda x: (len(x),lst2.index(x))) #to sort on n vars, return an n-tupple
print lstIn #list sorted by length of word then alphabet