From 1ae6ce04dd8f0c7f0b948feb9c6f7d89d28eba13 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 27 May 2016 23:13:01 +0200 Subject: [PATCH 1/5] Run python-modernize across the codebase --- beets/autotag/hooks.py | 2 +- beets/library.py | 2 +- beets/ui/commands.py | 4 ++-- beets/util/confit.py | 2 +- beets/util/functemplate.py | 2 +- extra/release.py | 8 +++++--- test/test_thumbnails.py | 2 +- 7 files changed, 12 insertions(+), 10 deletions(-) diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index 5c8e0e2c5..8f0a98104 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -349,7 +349,7 @@ class Distance(object): # Convert distance into a negative float we can sort items in # ascending order (for keys, when the penalty is equal) and # still get the items with the biggest distance first. - return sorted(list_, key=lambda (key, dist): (0 - dist, key)) + return sorted(list_, key=lambda key_dist: (0 - key_dist[1], key_dist[0])) # Behave like a float. diff --git a/beets/library.py b/beets/library.py index ed1938671..bb6873d2d 100644 --- a/beets/library.py +++ b/beets/library.py @@ -559,7 +559,7 @@ class Item(LibModel): for key in self._media_fields: value = getattr(mediafile, key) - if isinstance(value, (int, long)): + if isinstance(value, (int, int)): if value.bit_length() > 63: value = 0 self[key] = value diff --git a/beets/ui/commands.py b/beets/ui/commands.py index a2669e688..2d9f3cdb1 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -280,7 +280,7 @@ def show_change(cur_artist, cur_album, match): # Tracks. pairs = match.mapping.items() - pairs.sort(key=lambda (_, track_info): track_info.index) + pairs.sort(key=lambda __track_info: __track_info[1].index) # Build up LHS and RHS for track difference display. The `lines` list # contains ``(lhs, rhs, width)`` tuples where `width` is the length (in @@ -442,7 +442,7 @@ def summarize_items(items, singleton): else: # Enumerate all the formats by decreasing frequencies: for fmt, count in sorted(format_counts.items(), - key=lambda (f, c): (-c, f)): + key=lambda f_c: (-f_c[1], f_c[0])): summary_parts.append('{0} {1}'.format(fmt, count)) if items: diff --git a/beets/util/confit.py b/beets/util/confit.py index ac1cb7023..6d35667bc 100644 --- a/beets/util/confit.py +++ b/beets/util/confit.py @@ -49,7 +49,7 @@ REDACTED_TOMBSTONE = 'REDACTED' PY3 = sys.version_info[0] == 3 STRING = str if PY3 else unicode BASESTRING = str if PY3 else basestring -NUMERIC_TYPES = (int, float) if PY3 else (int, float, long) +NUMERIC_TYPES = (int, float) if PY3 else (int, float, int) def iter_first(sequence): diff --git a/beets/util/functemplate.py b/beets/util/functemplate.py index b4097baba..16d21cd58 100644 --- a/beets/util/functemplate.py +++ b/beets/util/functemplate.py @@ -72,7 +72,7 @@ def ex_literal(val): """ if val is None: return ast.Name(b'None', ast.Load()) - elif isinstance(val, (int, float, long)): + elif isinstance(val, (int, float, int)): return ast.Num(val) elif isinstance(val, bool): return ast.Name(bytes(val), ast.Load()) diff --git a/extra/release.py b/extra/release.py index 7bbc080c4..cf368c8f3 100755 --- a/extra/release.py +++ b/extra/release.py @@ -3,6 +3,8 @@ """A utility script for automating the beets release process. """ +from __future__ import absolute_import +from __future__ import print_function import click import os import re @@ -110,7 +112,7 @@ def bump_version(version): out_lines.append(line) if not found: - print("No pattern found in {}".format(filename)) + print(("No pattern found in {}".format(filename))) # Write the file back. with open(filename, 'w') as f: @@ -212,7 +214,7 @@ def changelog_as_markdown(): def changelog(): """Get the most recent version's changelog as Markdown. """ - print(changelog_as_markdown()) + print((changelog_as_markdown())) def get_version(index=0): @@ -233,7 +235,7 @@ def get_version(index=0): def version(): """Display the current version. """ - print(get_version()) + print((get_version())) @release.command() diff --git a/test/test_thumbnails.py b/test/test_thumbnails.py index 30c5bb234..24a1bc92d 100644 --- a/test/test_thumbnails.py +++ b/test/test_thumbnails.py @@ -114,7 +114,7 @@ class ThumbnailsTest(unittest.TestCase, TestHelper): self.assertEqual(ThumbnailsPlugin().get_uri, giouri_inst.uri) giouri_inst.available = False - self.assertEqual(ThumbnailsPlugin().get_uri.im_class, PathlibURI) + self.assertEqual(ThumbnailsPlugin().get_uri.__self__.__class__, PathlibURI) @patch('beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok') @patch('beetsplug.thumbnails.ArtResizer') From 9518f28faaaee6327dcc0824684b1c21110fd33c Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 27 May 2016 23:15:21 +0200 Subject: [PATCH 2/5] Fix numeric types --- beets/library.py | 2 +- beets/util/confit.py | 2 +- beets/util/functemplate.py | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/beets/library.py b/beets/library.py index bb6873d2d..ed1938671 100644 --- a/beets/library.py +++ b/beets/library.py @@ -559,7 +559,7 @@ class Item(LibModel): for key in self._media_fields: value = getattr(mediafile, key) - if isinstance(value, (int, int)): + if isinstance(value, (int, long)): if value.bit_length() > 63: value = 0 self[key] = value diff --git a/beets/util/confit.py b/beets/util/confit.py index 6d35667bc..ac1cb7023 100644 --- a/beets/util/confit.py +++ b/beets/util/confit.py @@ -49,7 +49,7 @@ REDACTED_TOMBSTONE = 'REDACTED' PY3 = sys.version_info[0] == 3 STRING = str if PY3 else unicode BASESTRING = str if PY3 else basestring -NUMERIC_TYPES = (int, float) if PY3 else (int, float, int) +NUMERIC_TYPES = (int, float) if PY3 else (int, float, long) def iter_first(sequence): diff --git a/beets/util/functemplate.py b/beets/util/functemplate.py index 16d21cd58..6c8e3e73b 100644 --- a/beets/util/functemplate.py +++ b/beets/util/functemplate.py @@ -34,6 +34,8 @@ import ast import dis import types +from .confit import NUMERIC_TYPES + SYMBOL_DELIM = u'$' FUNC_DELIM = u'%' GROUP_OPEN = u'{' @@ -72,7 +74,7 @@ def ex_literal(val): """ if val is None: return ast.Name(b'None', ast.Load()) - elif isinstance(val, (int, float, int)): + elif isinstance(val, NUMERIC_TYPES): return ast.Num(val) elif isinstance(val, bool): return ast.Name(bytes(val), ast.Load()) From 0fc07c28bc08c840588c8e8c6efba610e73f0dc4 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 27 May 2016 23:16:07 +0200 Subject: [PATCH 3/5] Stylefixes --- beets/autotag/hooks.py | 3 ++- test/test_thumbnails.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index 8f0a98104..1c0408e2f 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -349,7 +349,8 @@ class Distance(object): # Convert distance into a negative float we can sort items in # ascending order (for keys, when the penalty is equal) and # still get the items with the biggest distance first. - return sorted(list_, key=lambda key_dist: (0 - key_dist[1], key_dist[0])) + return sorted(list_, + key=lambda key_dist: (0 - key_dist[1], key_dist[0])) # Behave like a float. diff --git a/test/test_thumbnails.py b/test/test_thumbnails.py index 24a1bc92d..152663091 100644 --- a/test/test_thumbnails.py +++ b/test/test_thumbnails.py @@ -114,7 +114,8 @@ class ThumbnailsTest(unittest.TestCase, TestHelper): self.assertEqual(ThumbnailsPlugin().get_uri, giouri_inst.uri) giouri_inst.available = False - self.assertEqual(ThumbnailsPlugin().get_uri.__self__.__class__, PathlibURI) + self.assertEqual(ThumbnailsPlugin().get_uri.__self__.__class__, + PathlibURI) @patch('beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok') @patch('beetsplug.thumbnails.ArtResizer') From 5c41a9e7670a14576d4be82d488027eda26a5897 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 27 May 2016 23:18:22 +0200 Subject: [PATCH 4/5] Remove superfluous parens --- extra/release.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extra/release.py b/extra/release.py index cf368c8f3..d8b1e9dac 100755 --- a/extra/release.py +++ b/extra/release.py @@ -112,7 +112,7 @@ def bump_version(version): out_lines.append(line) if not found: - print(("No pattern found in {}".format(filename))) + print("No pattern found in {}".format(filename)) # Write the file back. with open(filename, 'w') as f: @@ -214,7 +214,7 @@ def changelog_as_markdown(): def changelog(): """Get the most recent version's changelog as Markdown. """ - print((changelog_as_markdown())) + print(changelog_as_markdown()) def get_version(index=0): @@ -235,7 +235,7 @@ def get_version(index=0): def version(): """Display the current version. """ - print((get_version())) + print(get_version()) @release.command() From f2f18d9d566190aae98a4b7bea72320355dbb024 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 27 May 2016 23:50:01 +0200 Subject: [PATCH 5/5] Fix variable names --- beets/autotag/hooks.py | 6 ++++-- beets/ui/commands.py | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index 1c0408e2f..3de803899 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -349,8 +349,10 @@ class Distance(object): # Convert distance into a negative float we can sort items in # ascending order (for keys, when the penalty is equal) and # still get the items with the biggest distance first. - return sorted(list_, - key=lambda key_dist: (0 - key_dist[1], key_dist[0])) + return sorted( + list_, + key=lambda key_and_dist: (-key_and_dist[1], key_and_dist[0]) + ) # Behave like a float. diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 2d9f3cdb1..3f55d864e 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -280,7 +280,7 @@ def show_change(cur_artist, cur_album, match): # Tracks. pairs = match.mapping.items() - pairs.sort(key=lambda __track_info: __track_info[1].index) + pairs.sort(key=lambda item_and_track_info: item_and_track_info[1].index) # Build up LHS and RHS for track difference display. The `lines` list # contains ``(lhs, rhs, width)`` tuples where `width` is the length (in @@ -441,8 +441,10 @@ def summarize_items(items, singleton): summary_parts.append(items[0].format) else: # Enumerate all the formats by decreasing frequencies: - for fmt, count in sorted(format_counts.items(), - key=lambda f_c: (-f_c[1], f_c[0])): + for fmt, count in sorted( + format_counts.items(), + key=lambda fmt_and_count: (-fmt_and_count[1], fmt_and_count[0]) + ): summary_parts.append('{0} {1}'.format(fmt, count)) if items: