[Tutor] The __setattr__ method
steve
lonetwin@yahoo.com
Tue, 28 Aug 2001 18:16:17 +0530
Hi there,
On Monday 27 August 2001 23:33, you 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 informatio=
n
> 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.
> .....
> .....
> <snip>
Thought I'd just mention it,....I've written a similar module, you can h=
ave=20
a look at it at the Useless Python site:
http://www.lowerstandard.com/python/uselesspython12.html
I have used a UserDict to represent the IDTag data, like so:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
class mp3file(UserDict):
tagDataMap =3D {
"title" : ( 3, 33, stripnulls),
"artist" : ( 33, 63, stripnulls),
"album" : ( 63, 93, stripnulls),
"year" : ( 93, 97, stripnulls),
"comment" : ( 96, 126, stripnulls),
"genre" : (127, 128, ord) }
def __init__(self, flname =3D None):
UserDict.__init__(self)
self["name"] =3D flname
self.__parse()
self.dir, file =3D os.path.split(flname)
self["new_name"] =3D file
def __parse(self):
try:
fl =3D open(self["name"], "r")
try:
fl.seek(-128, 2)
tagdata =3D fl.read()
finally:
fl.close()
if tagdata[:3] =3D=3D 'TAG':
for tag, (start, end, func) in self.tagDataMap.items():
self[tag] =3D func(tagdata[start:end])
else:
for tag in self.tagDataMap.keys():
self[tag] =3D ''
except IOError,msg:
raise BigBooBoo("Error opening file %s\n%s" % (flname, ms=
g))
=20
def __setitem__(self, key, item):
if key in [ "title", "artist", "album", "comment" ]:
item =3D item[:30]
elif key =3D=3D "year":
item =3D item[:4]
elif key =3D=3D "genre":
item =3D self.getGenre(item)
self.data[key] =3D item
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
It might not be all that clear b'cos I have no comments within the file a=
bout=20
why I do what I do, but if you have questions about anything you could al=
ways=20
ask.
Hope you find it interesting
Peace
Steve
=20
-------------------------------------------------------------------------=
---
You will pay for your sins. If you have already paid, please disregard
this message.
-------------------------------------------------------------------------=
---