3

I need to process all the MP3 files in a folder by reading/changing some ID3 tags as well as obtaining specifics about file size and so on. The end goal is to build an RSS file so these MP3's will be a custom podcast. I see the need for maybe up to 200 files (rows?) and 5 or 6 pieces of data (columns?) about each file. Need to read all data in, use the data to determine sort order, and build rss/xml file. Not sure best approach in Python regarding the way to handle the data.

Saw this code idea for a "dictionary of dictionaries", but this looks a bit clunky?

mydict = {'MP3_File_1.mp3': 
          {'SIZE': '123456789','MODDATE': '20120508', 'TRKNUM': '152'}, 
           'MP3_File_2.mp3': 
          {'SIZE': '45689654', 'MODDATE': '20120515', 'TRKNUM': '003'}, 
           'MP3_File_3.mp3': 
          {'SIZE': '98754651', 'MODDATE': '20130101', 'TRKNUM': '062'}}

Either a real database or pyTables seem like overkill. I'm also considering creating a custom class but don't have enough experience in Python (yet). Is there a module/best practice I am missing?

4 Answers 4

2

I would create a custom class to contain one MP3 file, one variable per field. This way, you can easily write functions to modify those fields. Then I would construct one object per file (using the filename as parameter to the constructor, and using the constructor to fill the fields), and put all objects into a list. This class would contain needed function to sort objects. Finally, I would write a custom function to generate the XML file from that list.

This is not the only way to do it, but this is how I would do.

class Mp3file(object):
    def __init__(self, filename):
        # read the file
        self.name = filename
        self.size = ...
        self.moddate = ...
        self.track_num = ...
        ...

    def to_xml(self):
        return ...

    def __lt__(self):
         ....
    def __eq__(self):
         ....
    ...

mp3list = []
for filename in directory:
    mp3list.append(Mp3file(filename))

def mp3list_to_xml(mylist):
    # write preamble
    for mf in sorted(mylist):
        x = mf.to_xml()
        # Add x to xml
    # write footer
Sign up to request clarification or add additional context in comments.

Comments

2

A list of dictionaries makes more sense to me.

mp3s = [
      {'NAME': 'lalala.mp3', 'SIZE': '123456789','MODDATE': '20120508', 'TRKNUM': '152'}, 
      {'NAME': 'lelele.mp3', 'SIZE': '45689654', 'MODDATE': '20120515', 'TRKNUM': '003'}, 
      {'NAME': 'lululu.mp3', 'SIZE': '98754651', 'MODDATE': '20130101', 'TRKNUM': '062'}]

If you want sorting is as simple as this:

sor = sorted(mp3s, key=lambda x: x['NAME'])

Comments

1

Why not just use sqlite it comes bundled for free.

You get sorting & searching builtin and the databases are disposable so extra processes to manage.

Also the likelihood that as your code develops you'll want to add more attributes to you data and by then a dict etc. will become unmanageable.

It helps to be able to look a database select and think

Yeah, my data looks good - the next part should be easy.

Comments

0

List of NamedTuples perhaps? The tuple should be one of the least mem-consuming types in Python AFAIK.

1 Comment

OP says he needs to modify fileds, tuples is out of the table, if not NamedTuples would be my first choice, the collections module has top quality structures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.