#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    3
    Rep Power
    0

    Adding Custom Objects to dictionaries


    Im working on writing a simple script that will keep track of my work hours over the summer. It consists of two classes,

    #1. A "work_record" class that holds information about a workday
    #2. A class bassed on a dictionary that holds the "work_records" and provides methods for sorting, extracting information ECT.

    The problem arises when i try to assign an instance of "work_record" to a key in the dictionary. I get this error:

    File "C:\Documents and Settings\Andy Kanyer\Desktop\workrecords.py", line 54, in add_record
    self.records[name]=obj
    TypeError: object does not support item assignment


    the assignment operation is:
    Code:
    self.records[label]=obj
    where obj is the class, and self.records is the dictionary.

    I know I'm missing something simple here, but I can't seem to find a solution. Any help would be appreciated.

    Thanks,

    Andy
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Is 'records' a regular dictionary, or a dictionary-like class? If it is a regular dictionary, and 'label' is a string, then there should not be any problems with the line you have shown.

    As far as I can tell from searching Google, that particular exception happens when you try to modify the __dict__ of a new-style class. However the code you have shown does not seem to be doing that.

    I think there is more information we need - can you give us the class and method definitions?

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    3
    Rep Power
    0
    heres the whole program:

    Code:
    import time, string
    
    class workRecord:
        def __init__(self):
            self.name=raw_input("Provide a name for this record: ")
            self.date=self.get_date()
            self.employer=self.get_employer()
            self.hours=self.get_hours()
            self.rate=self.get_rate()
    
    
        def get_date(self):
            #get date in format: (month, day, year)
            while 1:
                month=int(raw_input("Enter a number between 1-12 for Month: "))
                if month>0 and month<13:
                    break
            
            while 1:
                day=int(raw_input("Enter a number between 1-31 for Day: "))
                if day>0 or day<32:
                    break
            year=int(raw_input("Enter 4 digit Year: "))
            return (month, day, year)
        
        def get_employer(self):
            employer=raw_input("Enter Name of your Employer: ")
            return employer
    
        def get_hours(self):
            print "Enter times in form 'hour:min'"
            strttime=string.split(raw_input("Start time: "), ':')
            endtime=string.split(raw_input("End time: "), ':')
            return (strttime, endtime)
    
        def get_rate(self):
            return raw_input("Enter $ per hour: ")
    
        def get(self):
            return (self.name, self.date, self.employer, self.hours, self.rate)
    
    class test:
        def __init__(self):
            var="hello"
    
    
    class workDB:
        def __init__(self, recs="none", ord=0):
            if recs!="none":
                self.records={}
            else:
                self.records=recs
            self.order=ord
            
        def add_record(self, obj):
            name=obj.get()[self.order]
            self.records[name]=obj
    
        def display(self):
            for i in self.records.keys():
                print i+": "+self.records[i]
    
    
    if __name__=='__main__':
        db=workDB()
        rec=workRecord()
        db.add_record(rec)
        db.display()
    This is just a 10 minute program, so it might not be the most elegantly written, the thing thats throwing me off is that I've had this work before....

    Thanks for your help!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Originally Posted by remus 343
    Code:
    class workDB:
        def __init__(self, recs="none", ord=0):
            if recs!="none":
                self.records={}
            else:
                self.records=recs
            self.order=ord
            
        def add_record(self, obj):
            name=obj.get()[self.order]
            self.records[name]=obj
    This is the offending code.

    You are assigning "none" to self.records. Change it to this:

    Code:
    class workDB:
        def __init__(self, recs=None, ord=0):
            if recs is None:
                self.records={}
            else:
                self.records=recs
            self.order=ord
            
        def add_record(self, obj):
            name=obj.get()[self.order]
            self.records[name]=obj
    And you should be okay, I think.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    3
    Rep Power
    0
    Thanks a bunch.
    Now I feel just slightly stupid, but thats what I get for coding late at night.


    Thanks again,

    Andy
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    You might like to try out my persistent dictionary ptypes class:
    http://www.peck.org.uk/p/python/
    It makes it easy for loading and saving stuff (not that pickling is hard).

    Grim

IMN logo majestic logo threadwatch logo seochat tools logo