Skip to main content.
April 23rd, 2007

Python - Subclassing List to make it N-based

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 »

Mini Searches with Answers

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.

Clonezilla
Looks 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?

Simple Encryption Library for Python
I’m not sure if it’s secure or not but at least it’s easy to use.

How do you edit DVD’s you have ripped to your hard drive?
This discussion suggests some good tools to try.

Greater Washington Initiative
A list of technology incubators and shared office arrangements in the DC area.

A Really Exciting Way to Learn LISP
I just read a bit, but it looks like this site’s articles could hold my interest enough to learn some basics of LISP.

Tags: , , , , , , , , , , , , , , , , , , , , , , , ,

Posted by Greg Pinero (Primary Searcher) as Uncategorized at 3:30 AM MST

Comments Off

April 20th, 2007

Edit Any Webpage in Firefox - Any Part of the Page at All

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:
BaseCamp Certified Mail2.png

Posted by Greg Pinero (Primary Searcher) as Other, Firefox at 11:01 AM MST

Comments Off

April 17th, 2007

Mini Searches with Answers

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.

NdisWrapper: The Ultimate Guide - LinuxQuestions.org
This should be a thorough guide to using the Ndis Wrapper. Does the NdisWrapper only work for Wifi though? I thought you could use it for any drivers?

ASPN : Python Cookbook : Easy Cross Platform Excel Parsing With Xlrd
Get data into Python from Excel without ever opening Excel or Windows! Yay! Hope it works.

ASPN : Python Cookbook : Cross-platform startfile and mailto functions
This recipe provides a couple of cross-platform utility functions, ‘open’ and ‘mailto’, for opening files and URLs in the registered default application and for sending e-mails using the user’s preferred composer.

consensus is a collaborative filter library written in Python.
This may be the way to go if you want to get a collaberative filter algorithm up and running quickly. I haven’t tried it yet though.

Tags: , , , , , , , , , , , , ,

Posted by Greg Pinero (Primary Searcher) as Uncategorized at 10:51 AM MST

Comments Off

April 13th, 2007

Python - Really Simple cPickle

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: , , , , ,

Posted by Greg Pinero (Primary Searcher) as Python at 12:37 PM MST

Comments Off

« Previous Entries