sorting a shelve

bjorn bjorn at roguewave.com
Wed Mar 8 19:31:03 EST 2000


You are really looking for relational functionality here, so Gadfly might
be your best choice.  If you insist on using shelve, you'll probably have
to manually create and maintain indices, e.g. something like (untested):

    class MyDB:
        def __init__(self, myshelve):
            self.lastnames = {}
            self.s = myshelve

        def addPerson(self, email, fistname, lastname):
            self.s[email] = (email, firstname, lastname)
            tmp = self.lastnames.get(lastname, [])
            tmp.append(email)
            self.lastnames[lastname] = tmp

        def find(self, lastname):
            tmp = []
            for email in self.lastnames[lastname]:
                tmp.append( self.s[email] )
            return tmp

basically, under each lastname in self.lastnames, you keep a list of email
addresses for people with that last name.

You'll probably want to store the indices in the shelve also.

-- bjorn


Anders Eriksson wrote:

> Hello!
> I'm thinking of creating a simple addressbook and using a shelve as
> the 'database'. Say I use the email address as the key and then as the
> value I have a list of all the things I want to save about this
> person, like first and last name, phone, fax, address, city, etc.
>
> Now I I know the email address then it's very easy to find the rest of
> the info and if I know the first name then I could just loop until I
> found it... but if I want to create a print out sorted by last name
> and only thos that live in London; how do I go about?
>
> // Anders
>
> --
> http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list