Updating multiple attributes from multiple arguments?

This is the place for queries that don't fit in any of the other categories.

Updating multiple attributes from multiple arguments?

Postby russ » Wed Mar 13, 2013 10:15 am

Code: Select all
class Database(object):

    def __init__(self):
        self._by_name = {}
        self._by_tel = {}
        self._by_cell = {}
        self._by_house = {}
        self._by_street = {}
        self._by_town = {}
        self._by_city = {}
        self._by_zipcode = {}

    def new_entry(self, name, tel=None, cell=None, house=None,
                  street=None, town=None, city=None, zipcode=None):
        entry = Entry(name, tel, cell, house, street, town, city, zipcode)
        # example of updating one attribute
        if name in self._by_name:
            self._by_name.append(entry)
        else:
            self._by_name = [entry]

    def lookup(self, name):
        if name in self._by_name:
            return self._by_name[name]
        return False

    def delete_entry(self, entry):
        return

    def edit_entry(self, entry):
        return

    def find_entry(self, name=None, tel=None, cell=None, house=None,
                   street=None, town=None, city=None, zipcode=None):
        return


After the comment is an if else block which updates the _by_name attribute (which is a dictionary). Other than coding 8 different if else blocks like that one to update each dictionary, what's the correct or pythonic way to do this. I imagine there must be a few ways.

I was trying out some things with self.__dict__ and self.__getattribute__() but am not satisfied.

The purpose of having 8 separate dictionaries originally was for the find_entry() method. Maybe the structure of the class so far is just wrong. Any suggestions appreciated.
russ
 
Posts: 18
Joined: Sat Mar 02, 2013 8:59 am

Re: Updating multiple attributes from multiple arguments?

Postby russ » Wed Mar 13, 2013 12:50 pm

Ok so this is what I have atm, but it doesn't work. The new_entry() method in the Database class is the problem. I'm guessing it has something to do with too much juggling around of references to objects. It produces no errors but doesn't do anything either.

Code: Select all
class Entry(object):

    def __init__(self, name, tel=None, cell=None, house=None,
                 street=None, town=None, city=None, zipcode=None):
        self._name = name
        self._tel = tel
        self._cell = cell
        self._house = house
        self._street = street
        self._town = town
        self._city = city
        self._zipcode = zipcode

    def __str__(self):
        form = """Name: %s\nTel: %s\nCell: %s\nHouse No.: %s\nStreet: %s
Town: %s\nCity: %s\nZipcode: %s"""
        return form % (self._name, self._tel, self._cell, self._house,
                       self._street, self._town, self._city, self._zipcode)

    def name(self): return self._name

    def tel(self): return self._tel

    def cell(self): return self._cell

    def house(self): return self._house

    def street(self): return self._street

    def town(self): return self._town

    def city(self): return self._city

    def zipcode(self): return self._zipcode

    def address(self):
        street_addr = ((self._house if self._house != None else "")
                       + (self._street if self._street != None
                          else "<street unknown>"))
        town = self._town if self._town != None else "<town unknown>"
        city = self._city if self._city != None else "<city unknown>"
        zipcode = self._zipcode if self._zipcode != None else "<zipcode unknown>"
        return "%s\n%s\n%s\n%s" % (street_addr, town, city, zipcode)


class Database(object):

    def __init__(self):
        self._by_name = {}
        self._by_tel = {}
        self._by_cell = {}
        self._by_house = {}
        self._by_street = {}
        self._by_town = {}
        self._by_city = {}
        self._by_zipcode = {}
        self._data = [self._by_name, self._by_tel, self._by_cell,
                      self._by_house, self._by_street, self._by_town,
                      self._by_city, self._by_zipcode]

    def lookup(self, name):
        if name in self._by_name:
            return self._by_name[name]
        return False

    def new_entry(self, name, tel=None, cell=None, house=None,
                  street=None, town=None, city=None, zipcode=None):
        args = [name, tel, cell, house, street, town, city, zipcode]
        entry = Entry(*args)
        for attr, detail in zip(self._data, args):
            attr.get(detail, []).append(entry)

    def delete_entry(self, entry):
        return

    def edit_entry(self, entry):
        return

    def find_entry(self, name=None, tel=None, cell=None, house=None,
                   street=None, town=None, city=None, zipcode=None):
        return


Once I get that working I'm thinking of doing away with the entry class entirely. It will then use a uniquely generated integer to represent each entry, and each dictionary will map both details to keys and keys to details.
russ
 
Posts: 18
Joined: Sat Mar 02, 2013 8:59 am

Re: Updating multiple attributes from multiple arguments?

Postby Yoriz » Wed Mar 13, 2013 1:05 pm

If you want to represent a database as objects you should consider using sqlalchemy
A Database object would contain tables, tables would contain records.
a record would have a unique id as well as its attributes, it could then be stored in the table against its id.
The table would be stored in the database against its unique tablename.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 838
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests