Have you ever wanted to use a Python list object but make the index start at 1 instead of 0? I needed to do just that today so that the indices in the list I was building would match how the spec was written.
Here’s a little class I came up with to that. It behaves exactly like a list except that the first item is at the position you specify, and the other items come after that.
Let me know if you have improvements or any dire warnings about why this is harmful somehow
class NbasedList(list):
"""Overrides the list class to make it behave as if it where based at the
specified base value instead of 0. I.e., the first item in the list appears
to be at index position N
Usage:
>>>a=NbasedList([1,2,3],1)
>>>a[1]
1
"""
def __init__(self,alist,base):
list.__init__(self,alist)
self.base=base
def __getitem__(self,k):
return list.__getitem__(self,k-self.base)
def __setitem__(self,k,value):
list.__setitem__(self,k-self.base,value)
def __delitem__(self,k):
list.__delitem__(self,k-self.base)
Posted by Greg Pinero (Primary Searcher) as Python at 1:46 PM MST
8 Comments »
These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but still super useful.
ClonezillaLooks like an easy way to make disk images and more. I can’t tell if it does the boot sector, I’ve had trouble with partimage with windows XP before, maybe this fixes it?
Tags: Avi, Backup, Becky, Blendedtechnologies, Clone, Coworking, Dc, Dd, Disk, Diskimage, Dvd, Education, Encryption, Entrepreneur, Ghost, Incubator, Lisp, Local, Office, Partimage, Python, Toread, Ubuntu, Video_Editor_Research, Work
Posted by Greg Pinero (Primary Searcher) as Uncategorized at 3:30 AM MST
Comments Off
Update: Here is a bookmarklet to turn on edit page mode. I couldn’t get the bookmarklet to turn it off to work though.
Edit Page Mode On
This cool javascript that lets you edit any web page. Just copy and paste into your address bar:
javascript:document.body.contentEditable='true'; document.designMode='on'; void 0
To cancel EDIT MODE enter:
javascript:document.body.contentEditable='false'; document.designMode='on'; void 0
I discovered this trick in this forum post.
Here’s what I used this for today, by the way:

Posted by Greg Pinero (Primary Searcher) as Other, Firefox at 11:01 AM MST
Comments Off
These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but still super useful.
Tags: Browser, Datamining, Excel, Linux, Machine_Learning, Mailto, Ndiswrapper, Netflixprize, Open, Python, Statistics, Ubuntu, Wifi, Work
Posted by Greg Pinero (Primary Searcher) as Uncategorized at 10:51 AM MST
Comments Off
The cPickle module lets you quickly save an object in your Python program to disk for later use. It’s pretty useful if you want your program to save some information but don’t feel like figuring out a file format, and then implementing it.
In the programming world, they call this concept “object serialization”.
Below is a code snippet that I use when I want to pickle something. See the official documention for more examples and sophisticated uses.
import cPickle
SAVE_PATH=r'C:\my_object.p'
def save(your_object):
# Pickle the list using the highest protocol available.
cPickle.dump(your_object, file(SAVE_PATH, 'wb'), -1)
def load():
if os.path.exists(SAVE_PATH):
return cPickle.load(file(SAVE_PATH,'rb'))
else:
return None
NOTE: If you like this concept but find yourself in this situation:
1. Pickling is too slow.
2. The object I want to serialize is made up only of Python basic data types.
3. I don’t care if the saved information doesn’t work across Python versions.
Then you’re a prime candidate for the Marshal module.
Tags: Pickle, cPickle, Marshal, Object Serialization, Serialization, code sample
Posted by Greg Pinero (Primary Searcher) as Python at 12:37 PM MST
Comments Off