[Tutor] How to map different keys together ?

Alan Gauld alan.gauld at btinternet.com
Sat Apr 17 21:55:16 CEST 2010

"Damon Timm" <damontimm at gmail.com> wrote

> I am struggling, on a theoretical level, on how to map the various
> filetype's tag/key naming conventions.  I have already created my own
> MusicFile objects to handle each filetype (with functions for
> encoding/decoding) but I can't wrap my head around how to map the
> tags.

I'd define a mapping table per file type that maps from a standad
set of keys to the file specific tag.

Then define a class that works with the generic tags.
You can then eirther subclass per file type and use the file specific
mapping(a class variable) to translate internally or just create a function
that takes the file mapping as a parameter(maybe in the init() ) and sets
it up for the generic methods to use.

> And here is what I would need to do to find the song's TITLE text:
>>>> flac['title']
> [u'Christmas Waltz']
>>>> mp3['TIT2'].text  #notice this one takes another additional step, as well, 
>>>> by specifying text !
> [u'Christmas Waltz']
>>>> mp4['\xa9nam']
> [u"Christmas Waltz"]

So if the file is mp3 the generic


will inside the setTag method do

def setTag(self, tag, value)
       key = self.mapping[tag]   # get file specific tag
       fileData[key] = value       # and use it
       # or
       self.value = fileData[key]

> approach this.  I know I could manually assign each key to a class
> property (using the @property tag) ... but this seems tedious:

That would definitely be overklilll and not bvery flexible for adding new file 


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list