ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

Dave Cinege dave at linkscape.net
Tue Dec 11 21:57:24 CET 2012


On Tuesday 11 December 2012 01:41:38 Ian Kelly wrote:

> running into bugs like this:
> >>> thes = Thesaurus()
> >>> thes.update = 'now'
> >>> thes.update
> 
> <built-in method update of Thesaurus object at 0x01DB30C8>

I've noticed this but it's mostly pointless, as meaningful code does work.
(Also you stepped on the existing 'update()' dictionary method.)

>>> import thesaurus
>>> t = thesaurus.Thesaurus()
>>> t.s = 'now'
>>> t.s + ' this'
>'now this'

Works perfect. However I'll take a look at __getattribute__  as from what you 
say it's more proper.

> Second, in __getitem__ you start a loop with "for i in
> range(len(l)):", and then you use i as an index into l several times.
> It would be cleaner and more Pythonic to do "for i, part in
> enumerate(l):", and then you can replace every occurrence of "l[i]"

My python is still 'old school' due to being stuck on old versions for in 
production embedded system python applications.

> It's not clear to me what the isinstance call here is meant to be
> testing for.

It's used to determine if it's the root instance of the recursive string 
because self.data, not self must be used to access that. Can you offer a better 
way?

> The prior statements require key to be a string.  If key
> is a string, then by construction l[0] is also a string.  So it seems
> to me that the isinstance check here will always be False.

OK, try and remove it and then get back to me. :-)

> In any case, the key splitting here seems to be done primarily to
> support the use of formatting placeholders like "%(L.l.1)s" in the
> examples.  I want to point out that this use case is already well
> supported (I might even say "better" supported since it cleanly
> distinguishes index elements from attributes with syntax) by the

Thesaurus recursion works with modules like Template, in addition to allowing 
easy hierarchical organization of (global) data. It's not about string 
formatting. It's about organizing data and recursive retrieval of that data.

> Lastly, you have several bare "except" clauses in the code.  Bare

Not going to get into religion. There is no 'correct' way to do this code at 
all because it's not established normal python. I left it simple and it suits 
my needs. I've converted several production commercial utilities and 
applications to use Thesaurus, and have seen nothing but benefit.

If anyone needs more to this, have at it, I'm busy

Dave




More information about the Python-list mailing list