[Tutor] Using __setattr__

Tony Cappellini tony@tcapp.com
Sun Apr 20 05:21:02 2003


--=====================_23074289==_.ALT
Content-Type: text/plain; charset="us-ascii"; format=flowed


Hi Danny,


 >>Out of curiosity: are you using __setattr__ as an experiment with the
> >>language feature, or is there a particular bit of your code that can be
> >>improved if you use __setattr__?


Both, actually.

I've created a class, which for all practical purposes, contains fields such as

class   Address:

         def __init__(self, address_str):

                 name    = address_str
                 address = address_str
                 phone    = address_str


In all actuality, I'm working on a program for work, so I don't want to 
give out any sensitive information.
However, the concept is the same, the class members have been changed to 
protect the innocent.

The name, address, phone data is passed to the application, as a colon 
separated string- like this
Name:Mr Smith;Address:123 Main St;Phone:408-123-4567;

Since I don't want the application to deal with parsing the data, I thought 
I would experiment with doing it inside the class itself.
It actually seems to work quite well, but it took me awhile to get it working.

My __setattr__() looks like this

        def      __setattr__(self, name, addr_str):

                if name in ['Name', 'Address', 'Phone' ]:
                    name_loc = string.find(value_str, name)
                    if name_loc != -1:
                        pair = string.split(value_str[name_loc:], ';',1)
                        pair1 = string.split(pair[0], ':',1)
                        self.__dict__[name] = pair1[1]
                    else:
                        print"\nname %s NOT found" % name
                        raw_input("\nPAUSED")


After looking through ActiveState, I've found some posts which showed a 
__setattr__()
that contained this
self.__dict__['modified'] = 1

But I don't understand what this is, or when I need to use it.
In fact, the guy who posted it said that
self.__dict__['modified'] = 0 had to be done in __init__(), or the __setattr__
he posted, would not work.


I tried to go back and find that post, but I couldn't find it.
The original poster was talking about cataloging MP3's, so I should go back 
to Active State
and search for MP3, so I can point you to the specific example.

Anyway, I was intrigued with the technique, and it works in my program.
It may not be an elegant way of coding in Python, but it works.

I'd be interested in hearing your thoughts.

thanks


Tony






>If it's the second reason, there might
>be an easier way to tackle what you're trying to do.  Hmmm... can you tell
>us more about the class that you're writing?
>
>
>Good luck!

--=====================_23074289==_.ALT
Content-Type: text/html; charset="us-ascii"

<html>
<br>
Hi Danny,<br><br>
<br>
&gt;&gt;Out of curiosity: are you using __setattr__ as an experiment with
the<br>
<blockquote type=cite class=cite cite>&gt;&gt;language feature, or is
there a particular bit of your code that can be<br>
&gt;&gt;improved if you use __setattr__?&nbsp; </blockquote><br><br>
Both, actually.<br><br>
I've created a class, which for all practical purposes, contains fields
such as<br><br>
class<x-tab>&nbsp;&nbsp;&nbsp;</x-tab>Address:<br><br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>def
__init__(self, address_str):<br><br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>name&nbsp;&nbsp;&nbsp;
= address_str<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>address
= address_str<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>phone&nbsp;&nbsp;&nbsp;
= address_str<br><br>
<br>
In all actuality, I'm working on a program for work, so I don't want to
give out any sensitive information.<br>
However, the concept is the same, the class members have been changed to
protect the innocent.<br><br>
The name, address, phone data is passed to the application, as a colon
separated string- like this<br>
Name:Mr Smith;Address:123 Main St;Phone:408-123-4567;<br><br>
Since I don't want the application to deal with parsing the data, I
thought I would experiment with doing it inside the class itself.<br>
It actually seems to work quite well, but it took me awhile to get it
working.<br><br>
My __setattr__() looks like this<br><br>
<font face="Courier New, Courier">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
def<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>__setattr__(self,
name, addr_str):<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if name in ['Name', 'Address', 'Phone' ]:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
name_loc = string.find(value_str, name)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if name_loc != -1:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pair = string.split(value_str[name_loc:], ';',1)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pair1 = string.split(pair[0], ':',1)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
self.__dict__[name] = pair1[1]<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
else:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
print&quot;\nname %s NOT found&quot; % name<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
raw_input(&quot;\nPAUSED&quot;)<br><br>
<br>
</font>After looking through ActiveState, I've found some posts which
showed a __setattr__()<br>
that contained this<br>
<font face="Courier New, Courier">self.__dict__['modified'] = 1 
<br><br>
But I don't understand what this is, or when I need to use it.<br>
In fact, the guy who posted it said that<br>
self.__dict__['modified'] = 0 had to be done in __init__(), or the
__setattr__<br>
he posted, would not work.<br><br>
<br>
I tried to go back and find that post, but I couldn't find it.<br>
The original poster was talking about cataloging MP3's, so I should go
back to Active State<br>
and search for MP3, so I can point you to the specific example.<br><br>
Anyway, I was intrigued with the technique, and it works in my
program.<br>
It may not be an elegant way of coding in Python, but it works.<br><br>
I'd be interested in hearing your thoughts.<br><br>
thanks<br><br>
<br>
Tony<br><br>
<br><br>
<br><br>
<br>
</font><blockquote type=cite class=cite cite>If it's the second reason,
there might<br>
be an easier way to tackle what you're trying to do.&nbsp; Hmmm... can
you tell<br>
us more about the class that you're writing?<br><br>
<br>
Good luck!</blockquote></html>

--=====================_23074289==_.ALT--