From 1e48317e515dacffabd493cea4edf4d1dbbe2514 Mon Sep 17 00:00:00 2001 From: Philippe Mongeau Date: Fri, 21 Sep 2012 19:57:47 -0400 Subject: [PATCH] fuzzy_search: simplifiy some nested code --- beetsplug/fuzzy_search.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/beetsplug/fuzzy_search.py b/beetsplug/fuzzy_search.py index 4e93a4ee3..263502306 100644 --- a/beetsplug/fuzzy_search.py +++ b/beetsplug/fuzzy_search.py @@ -35,17 +35,15 @@ def is_match(query, item, album=False, verbose=False, threshold=0.7): values = [item.artist, item.album, item.title] s = max(fuzzy_score(query.lower(), i.lower()) for i in values) - if s >= threshold: - return (True, s) if verbose else True + if verbose: + return (s >= threshold, s) else: - return (False, s) if verbose else False + return s >= threshold def fuzzy_list(lib, config, opts, args): query = decargs(args) - path = opts.path fmt = opts.format - verbose = opts.verbose threshold = float(opts.threshold) if fmt is None: @@ -61,24 +59,18 @@ def fuzzy_list(lib, config, opts, args): else: objs = lib.items() - if opts.album: - for album in objs: - if is_match(query, album, album=True, threshold=threshold): - if path: - print_(album.item_dir()) - else: - print_(album.evaluate_template(template)) - if verbose: - print is_match(query, album, album=True, verbose=True)[1] - else: - for item in objs: - if is_match(query, item, threshold=threshold): - if path: - print_(item.path) - else: - print_(item.evaluate_template(template, lib)) - if verbose: - print is_match(query, item, verbose=True)[1] + items = filter(lambda i: is_match(query, i, album=opts.album, + threshold=threshold), objs) + for i in items: + if opts.path: + print_(i.item_dir() if opts.album else i.path) + elif opts.album: + print_(i.evaluate_template(template)) + else: + print_(i.evaluate_template(template, lib)) + if opts.verbose: + print(is_match(query, i, album=opts.album, verbose=True)[1]) + fuzzy_cmd = Subcommand('fuzzy', help='list items using fuzzy matching')