From: Francesc A. <fa...@py...> - 2004-04-05 17:45:54
|
Hi David, A Divendres 02 Abril 2004 21:43, David Sumner va escriure: > I am using PyTables 0.8 and am setting a dictionary as an attribute on a > table. > > When the table is created, I add a dictionary as an attribute of the > table like so: > site.attrs.totalTime = {entry.day: 0} > > Later I modify the value like so: > Site.attrs.totalTime[entry.day] += timespan > > After I have done all my processing, I flush the hdf5 file and then > close it. When I open it up and get the table, the site.attrs.totalTime > variable exists, the key I added also exists, but the value is set to 0, > instead of the last value it contained before the last closing. While > modifying the value of the totalTime dictionary I am printing the new > value to the screen for debugging purposes and I am seeing that the > value is being modified. No, you should be lost in some place. Right now, you can't update an attribute by modifying the python space of it. I.e. the next does not work (procedure 1): >>> t1.attrs.test2 = {"day":0} >>> t1.attrs.test2 {'day': 0} >>> t1.attrs.test2["day"] += 1 >>> t1.attrs.test2 {'day': 0} # Same value for "day" key! If you want to do that, you should do (procedure 2): >>> test2 = t1.attrs.test2 >>> test2 {'day': 0} >>> test2["day"] += 1 >>> t1.attrs.test2 = test2 >>> t1.attrs.test2 {'day': 1} # Value correctly updated This is a consequence of the fact that, when t1.attrs.test2 is called, a python object (in this case, a dicitonary) is returned. If you try to update that object in the way "t1.attrs.test2["day"] += 1", only the returned object in memory will be updated, not on disk. So, if you want to update parts of complex attributes like dictionaries or lists, you will have to use procedure 2. Perhaps it would be better to document better this little trick. Regards, -- Francesc Alted |