RPG Music’N’SFX – How to filter using tags

I’ve been pondering over the same question for the best part of a day today. It is a very simple question, but difficult to answer, because it depends on habits.

My filtering system relies on tags for each file, i.e. every file has a potentially unlimited amount of tags you can add to it. But the big question I have is: how should I filter it?

There are two possibilities that I can see; Say you are searching for the tags “safe” and… Dunno “arabic”. Does that now mean that only the files that have “both” those tags should show up or should all files that have at least one of them show up…

Talking python code, for the former option, I would imagine that I would have a dictionary of the tags as keys and lists of filepaths. These would feed into a set as a tracklist pool a bit like the following:

tag_dict = {"safe" : ["an/example/file.mp3", "another/file.wav"],
           "travel" : ["an/example/file.mp3",

# this would lead to playlist with both tags present looking
# as follows:

playlist = set(["an/example/file.mp3",

In the latter case, I would probably just have a dictionary of the filepath as the key and its tags as a list in the values. Then whenever another tag is added or removed, I would iterate over the dictionary and check if all tags are present in the list using set.intersection (That is something I learnt from here). If that is the case, it will be added to the playlist. A bit like so:

track_dict = {"an/example/file.mp3" : ["safe", "travel"],
              "another/file.wav" : ["safe"],
              "unrelated/other/file.ogg" : ["travel"]}
playlist = []

choices = set(["safe", "travel"])
# i.e. only the first should be in the playlist

for key in track_dict:
    tags = set(track_dict[key])
    intersect = list(tags.intersection(choices))
    if intersect == list(choices):

print playlist

My tendency, user-wise, goes towards the latter as it seems more logical to me, but I have noticed on several separate occasions that this means, in fact, nothing. My brain tends not realise the most “common” modus operandi.

I am tempted to try and implement both and give the user the choice…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s