[Tutor] The __setattr__ method

Andrei Kulakov ak@silmarill.org
Mon, 27 Aug 2001 14:10:19 -0400


On Tue, Aug 28, 2001 at 02:03:29AM +0800, Justin Ko wrote:
> 
> 	Hey everyone. I am working on a module to provide access to an mp3 file's 
> ID3 tags. A class seemed to be the best way to represent the information 
> represented in an ID3 tag's fields. However, i would like to be able to 
> tell when the information in the tag is changed so that the information can 
> be written back into the ID3 tag. This is my dilemma.
> 
> 	The __init__ method reads the information from the file.
> 
>   def __init__(self, filename = ""):
>     "Initialize things to default values or read them from a file"
>     if filename != "":
>       try:
>         file = open(filename, "r")
>         file.seek(-128, 2)
>         if file.read(3) == "TAG":
>           self.filename = filename
>           self.title = file.read(30)
>           self.artist = file.read(30)
>           self.album = file.read(30)
>           self.year = file.read(4)
>           self.comment = file.read(30)
>           self.genre = self.genres[ord(file.read(1))]
>           self.modified = 0
> 
> 	The __setattr__ method tells me when something is changed
> 
>   def __setattr__(self, name, value):
>     "Set class attributes"
>     self.__dict__[name] = value
>     self.__dict__['modified'] = 1
> 
> 	However,  the statements in the __init__ method cause __setattr__ to be 
> called.
> 	So this is my question. How do I set a classes attributes without calling 
> __setattr__ ? I guess it would be possible to replace all the self.variable 
> = file.read( ) statements with something like self.__dict__[variable] = 
> file.read( ), but that seems rather inelegant and ugly. I hope there is a 
> better solution to this problem.
> 
> 	- Justin Ko
> 
> 
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

There are two modules that do that, already - ID3.py and mp3.py. The
latter one is the most complete, and I fixed a serious bug in it and
packaged it with cymbaline (in sig). The version on vaults still has this
bug, afaik.

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org