Skip to content

Instantly share code, notes, and snippets.

"""easy string compression, based on the old compressed bitmap"""
class listComp(object):
@staticmethod
def compactLst(lstIn, lastOne = ''):
if lastOne == '':
listComp.myList = []
if lstIn[0] != lastOne:
#start a new acumulator
class myStr(str):
def pogoCase(self,jumpInt):
return "".join([myCar.lower() if myInt % jumpInt else myCar.upper() for myInt, myCar in enumerate(self)])
thisThing = myStr("The quick brown fox jumped over the lazy dog.")
print thisThing
print thisThing.pogoCase(3)
print thisThing.pogoCase(2)
print type(thisThing) is str
#!/usr/bin/env python
from Queue import Queue
from threading import Thread
from urllib2 import urlopen
from time import time
from random import shuffle
hosts = ["yahoo.com", "google.com", "amazon.com","ibm.com", "apple.com",
"bbc.co.uk", "npr.org", "cnn.com", "ubuntu.com", "soundcloud.com",
"bogus.url",'ebay.com','linux.org','osuosl.org','renren.com','alibaba.com',
listA = [1,2,3,4] #listA now holds a refrence to a list object
listB = listA #a refrence to the refrence held by listA
listC = list(listA) #a new copy of the object reffered to in the refrence held by listA
listA.append('dog') #update the object reffered to in the refrence held by listA
print(listB) #a refrence to the refrence held by listA -> the object reffered to in the refrence held by listA
print(listC) #the copy we made before updating is, of course, unchanged
class dictWrap(object):
def __init__(self, dictIn = False, fnXform = False):
self.noneVal = None
if dictIn:
if fnXform:
dictIn = dict((k,fnXform(v)) for k,v in dictIn.items())
self.__dict__.update(dictIn)
def __getattribute__(self, attr):
try:
return object.__getattribute__(self, attr)
class ConfigInfo(object):
rootDir = "/home/jpmcgrady/workspace"
subDir = "src"
class GrepDict(object):
def __init__(self, projName, findMe):
self.config = ConfigInfo
self.projName = projName
self.findMe = findMe
@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
@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 / 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 / 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__]))