Skip to content

Commit

Permalink
use yaml CLoader for improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Felk committed Aug 29, 2020
1 parent 568b945 commit 4d74a87
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions musiccat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# pip3 dependencies
import Levenshtein
import yaml
from yaml import Loader
from yaml import CLoader

from . import winamp

Expand All @@ -30,7 +30,7 @@ class NoMatchError(ValueError):
def __init__(self, song_id):
super().__init__("Song ID {} not found.".format(song_id))
self.song_id = song_id

class SongIdConflictError(ValueError):
"""Raised when a song id occurs twice."""
def __init__(self, song_id):
Expand Down Expand Up @@ -96,7 +96,7 @@ def refresh_song_list(self):
def _import_metadata(self, metafilename):
"""Import metadata given a metadata filename. Assumed to be one game per metadata file."""
with open(os.path.join(self.library_path, metafilename), encoding="utf-8") as metafile:
gamedata = yaml.load(metafile, Loader=Loader)
gamedata = yaml.load(metafile, Loader=CLoader)
path = os.path.dirname(metafilename)
newsongs = {}

Expand Down Expand Up @@ -128,15 +128,15 @@ def _import_metadata(self, metafilename):
song["ends"] = [int(minutes)*60 + int(seconds)]
else:
raise ValueError(song["ends"])


#if no tags provided, say so explicitly
if "tags" not in song:
song["tags"] = None
#convert single end time to list
elif type(song["tags"]) == str:
song["tags"] = [song["tags"]]

newsong = Song(**song)

# some sanity checks
Expand Down Expand Up @@ -179,11 +179,11 @@ def search(self, keywords, cutoff=0.3, required_tag=None):
num_keywords = len(keywords)
results = []
for song in self.songs.values():

if required_tag is not None:
if song.tags is None or required_tag not in song.tags:
continue

# search in title and gametitle
haystack1 = set(song.title.lower().split())
haystack2 = set(song.game.title.lower().split())
Expand All @@ -198,11 +198,11 @@ def search(self, keywords, cutoff=0.3, required_tag=None):
# assume low ratios are no match
ratio += subratio
ratio /= num_keywords

if ratio > cutoff:
# random cutoff value
results.append((song, ratio))

return sorted(results, key=lambda s: s[1], reverse=True)

def play_song(self, song_id):
Expand Down

0 comments on commit 4d74a87

Please sign in to comment.