[Tutor] How to map different keys together ?
ALAN GAULD
alan.gauld at btinternet.com
Sun Apr 18 21:23:29 CEST 2010
> My first stumbling block is that M4A files store the track number and
> track total in a single tuple ... but I need them as separate fields
> one of many hurdles -- I need a way to accomplish more than a
> one-to-one data map.
In that case map to a function which can return the value.
Use lambdas for the trivial cases:
'Title': lambda : fileData['title']
For 1-1 maps:
'Artist' : lambda : fileData['ART']
For more complex maps:
'Track' : lambda : flieData['TRK'][0]
'Total' : lambda : fileData['TRK'][1]
And for more complex things define a function:
def complexMap(key):
# do something here
return result
'WeirdThing' : lambda : complex ('WeirdThing')
Then just call the funcyion when needed:
myDatya = map[key]() # parens calls the function
> having trouble. I feel there may be a way to pass functions through
> my tag_map dictionary (maybe a lambda?!) but I can't get my head
> around what approach is best
Does what I've shown make sense?
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/
________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100418/2d577ced/attachment.html>
More information about the Tutor
mailing list