Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #779 (closed enhancement: fixed)

Opened 11 years ago

Last modified 10 years ago

turbogears.util.DictObj improvement

Reported by: sean@… Owned by: anonymous
Priority: normal Milestone: 0.9a5
Component: TurboGears Version: 0.9a4
Severity: normal Keywords:


I was just poking around util.py, and noticed DictObj?. This class seems only partially implemented (0.9a4), its purpose appears to be to "Make a dict look like an object." For this to be fully accurate it should also implement __setattr__ and __delattr__, as of 0.9a4 it only implements __getattr__. Also, the current implementation defined its own, very minimal, method, this is unnecessary as __getitem__ etc. have the same signature as __getattr__. My proposed implementation is as follows:

class DictObj(dict):
    """Make a dict look like an object."""
    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

This is all that's necessary to make a dict look, feel, and act like a classic object.


bunch.patch Download (4.8 KB) - added by alberto 11 years ago.
bunch2.patch Download (2.2 KB) - added by alberto 11 years ago.

Change History

comment:1 Changed 11 years ago by godoy

From discussions in the trunk mailing list I believe that this will be removed and Bunch will be the class that will be left. If somebody can confirm and close this bug as 'wontfix'/'invalid', I'd appreciate.

Changed 11 years ago by alberto

comment:2 Changed 11 years ago by alberto

Can someone please take a nosetests of this patch. My install is broken somewhere and I've got a bunch of errors and failures.

I guess it's correct because the only tests failing are the ones I had before... Anyway, I've tweaked Bunch to make it the only and only.

__getattr__ needs to be overrided so getattr(bunch, non_existent, default) == default. Else a KeyError? will raise and getattr will not give default values.

Changed 11 years ago by alberto

comment:3 Changed 11 years ago by alberto

Actually same goes for __delattr__. Better patch that keeps aliases for DictObj? and DictWarpper?. should they be deprecated?

comment:4 Changed 11 years ago by roger.demetrescu

Hi Alberto,

I tested both bunch.patch and bunch2.patch and the nosetests run fine...

[]s Roger

comment:5 Changed 11 years ago by michele

Over at the ASPN cookbook page for Bunch there are some discussion about other solution and memory leaks, better take a look here probably...

I don't have Python CookBook? 2ed but IIRC there is an improved solution by Alex there also.

Anyway need to sleep now!

comment:6 Changed 11 years ago by alberto

Sorry but I cannot see how on earth mapping __*attr__ to __*item, can leak any memory :/ No circular references that I can see there...

good night ;) Alberto

comment:7 Changed 11 years ago by michele

Actually I haven't even looked at your patch, I was going to sleep, noticed this and just remembered there were some discussions on the ASPN Bunch page about different implementations and I just thought it could be worth pointing it out as a reference, I haven't said your implementation is leaking memory, sorry. ;-)

comment:8 Changed 11 years ago by alberto

Apologies accepted. Now please accept mine, I've still got some adrenalin left from the "hair" in the ML... sorry ;)


P.S. Took a look at the ASPN Bunch page but couldn't find a link to Alex's solution, any link? (or is it only in the printed version?)

comment:9 Changed 11 years ago by michele

No problem Alberto, really! :D

Alex solution is the one proposed in the page (he is the author), I'm not 100% sure there was another better solution in the 2ed of Python CookBook? but I think I've read something saying this some times ago on comp.lang.python or it.comp.lang.python need to check... :D

Anyway I think your solution is already a good one...

comment:10 Changed 11 years ago by alberto

Ok, soon committing it then... Alberto

comment:11 Changed 11 years ago by alberto

  • Status changed from new to closed
  • Resolution set to fixed

Comitted at [1239]

Note: See TracTickets for help on using tickets.