[Baypiggies] Idiotmatic Savanica

Warren Stringer warren at muse.com
Tue May 15 04:44:03 CEST 2007


Cool; it works! Your suggestion about simplifying __setattr__ seems to work,
as well. 

 

I like encapsulating any redundancy into a def - which is another form of
documentation. So, took your suggestion about adding a _setInChild(name). 

 

I did diverge, shown here: www.muse.com/tr3/tr3_martelli_tweaked.py   ; with
some trepidation, I use double underscores for __setInChild__ and
__ok_attrs__ . Why? Because it simplifies watching variables, during debug.
The ActiveState IDE for Visual Studio hides attributes with __myname__
convention. Does any other IDE do this? Tracing through thousands of
embedded dictionaries will be much easier.

 

\~/

 

Heh, just had an odd thought; I've probably spent as much time reading
Martelli as Tolstoy.

 

  _____  

From: Alex Martelli [mailto:aleax at google.com] 
Sent: Monday, May 14, 2007 4:47 PM
To: Warren Stringer
Cc: Python
Subject: Re: [Baypiggies] Idiotmatic Savanica

 

On 5/14/07, Warren Stringer <warren at muse.com> wrote:

Last Thursday's newbie night was inspiring!


Glad you liked it!

Is there a more idiomatic way of expressing this? Perhaps a faster, yet
pythy solution?

 

I would change things to be more idiomatic in several ways:
a. use "if key in somedict" rather than "if somedict.has_key(key)"
b. know that __getattr__ is only called if OTHER normal ways to
    get the attribute fail (so don't bother checking if one of such
    normal ways would have succeeded!) -- __setattr__ is different 
c. add unit tests to make sure one can change and refactor with
    much more confidence

I'm not going to tackle (c), but, without testing, here's roughly how I
might rephrase things...:

 
class Tr3(dict):





    _ok_attrs = set('who what child parent'.split())





    def __init__(self, parent=None, who='tr3'):


        self.who = who


        self.what = None


        
self.child = {}


        self.parent = parent


      


    def __call__(self, v=None):


        self.what = v





    def __str__(self):


        if self.parent is not None:


            return '%s.%s' % (
self.parent, self.who)


        else:


            return self.who





    def __getattr__(self, name):


        if name[:2] == '__' == name[-2:]: 


            return dict.__getattr__(self, name)


        if name not in 
self.child:


            self.child[name] = Tr3(self, name)


        return self.child[name]





    def __setattr__(self, name, value):


        if name in self._ok_attrs or name in self.__dict__:


            self.__dict__[name] = value



        else:


            if name not in self.child:


                self.child[name] = Tr3(self, name)


            self.child[name].what = value









The intent is to get the same semantics as you had on your page, just in a
slightly more fluid and idiomatic way.


(I'm not quite sure how any name except those in _ok_attrs would ever end up
in self.__dict__, so I do



suspect that test in __setattr__ can be simplifited, but as I said I'm
trying to keep the same semantics as


you had, so I avoided performing that simplification myself).





As I abhor repetition so deeply, I'd probably add a tiny auxiliary method:






    def _setInChild(self, name):


        if name not in 
self.child:


            self.child[name] = Tr3(self, name)





and call self._setInChild(name) in the two places which now have, instead,
repetitions of this guarded



assignment (but I realize that most people don't abhor repetition as
intensely as I do:-).








Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/baypiggies/attachments/20070514/e86ac4dd/attachment.html 


More information about the Baypiggies mailing list