I'm working on a python program which organizes a music library. The program will basically help me detect misspelled file or directory names, wrong FLAC tags or unknown/superfluous files. It will also make sure that the FLAC files are named correctly according to their FLAC tags etc.

The structure I though of looks as follows (/ being the root of the music library):

            [year] - [release]
                [track number] - [title].flac
                [artist] - [release].jpg
                [artist] - [release].cue
                [artist] - [release].log
        Various Artists
            [artist_0 [, artist_1 [, ...]]] - [year] - [release]
                [track number] - [title].flac
                [artist_0 [, artist_1 [, ...]]] - [release].jpg
                [artist_0 [, artist_1 [, ...]]] - [release].cue
                [artist_0 [, artist_1 [, ...]]] - [release].log

I started writing some code but pretty soon I noticed that this is actually not a too easy problem to solve. I wrote 4 functions, category, artist, release and file, which are called accordingly while iterating through all the directories. Basically like that (pseudo code):

func category:
    do some checks
    for each file in this directory:

func artist:
    do some checks
    for each file in this directory:

func release:
    do some checks
    for each file in this directory:

func file:
    do some checks (FLAC tags etc)

for each file in the root directory:

Now I'm wondering, are there any known algorithms or techniques which could be used to solve this problem in an elegant way?

  • Another need for the Elegant Pattern. – JeffO Mar 31 '14 at 20:01
  • 1
    It seems like you're worried about the code repetition you think you are seeing. But you're operating on different fields, so I don't see how you really have any repetition here. – Robert Harvey Mar 31 '14 at 20:39

There is absolutely nothing wrong with your approach.

Note that if the "do some checks" part was the same in every function, then indeed you could simplify and have a single function that calls itself recursively on sub-folders. Perhaps this is what you had in mind... But this is not the case here as each function is clearly different.

Since you are using Python, you might want to check the os module, esp. os.walk(), which may help traverse the directory structure.

  • Thanks for your input. And yes you are right, the "do some checks" is in each function clearly different. – watain Apr 1 '14 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.