flake8: fix PEP8 warnings about lambda
Fix the 'lambda to def' warnings
This commit is contained in:
parent
4bf7a568d1
commit
34ada2784a
@ -25,7 +25,9 @@ class Scanner(object):
|
||||
sink = gst.element_factory_make('fakesink')
|
||||
|
||||
audio_caps = gst.Caps(b'audio/x-raw-int; audio/x-raw-float')
|
||||
pad_added = lambda src, pad: pad.link(sink.get_pad('sink'))
|
||||
|
||||
def pad_added(src, pad):
|
||||
return pad.link(sink.get_pad('sink'))
|
||||
|
||||
self._uribin = gst.element_factory_make('uridecodebin')
|
||||
self._uribin.set_property('caps', audio_caps)
|
||||
|
||||
@ -113,7 +113,9 @@ class Backends(list):
|
||||
self.with_playlists = collections.OrderedDict()
|
||||
|
||||
backends_by_scheme = {}
|
||||
name = lambda b: b.actor_ref.actor_class.__name__
|
||||
|
||||
def name(b):
|
||||
return b.actor_ref.actor_class.__name__
|
||||
|
||||
for b in backends:
|
||||
has_library = b.has_library().get()
|
||||
|
||||
@ -21,37 +21,52 @@ def find_exact(tracks, query=None, uris=None):
|
||||
else:
|
||||
q = value.strip()
|
||||
|
||||
uri_filter = lambda t: q == t.uri
|
||||
track_name_filter = lambda t: q == t.name
|
||||
album_filter = lambda t: q == getattr(
|
||||
getattr(t, 'album', None), 'name', None)
|
||||
artist_filter = lambda t: filter(
|
||||
lambda a: q == a.name, t.artists)
|
||||
albumartist_filter = lambda t: any([
|
||||
q == a.name
|
||||
for a in getattr(t.album, 'artists', [])])
|
||||
composer_filter = lambda t: any([
|
||||
q == a.name
|
||||
for a in getattr(t, 'composers', [])])
|
||||
performer_filter = lambda t: any([
|
||||
q == a.name
|
||||
for a in getattr(t, 'performers', [])])
|
||||
track_no_filter = lambda t: q == t.track_no
|
||||
genre_filter = lambda t: t.genre and q == t.genre
|
||||
date_filter = lambda t: q == t.date
|
||||
comment_filter = lambda t: q == t.comment
|
||||
any_filter = lambda t: (
|
||||
uri_filter(t) or
|
||||
track_name_filter(t) or
|
||||
album_filter(t) or
|
||||
artist_filter(t) or
|
||||
albumartist_filter(t) or
|
||||
composer_filter(t) or
|
||||
performer_filter(t) or
|
||||
track_no_filter(t) or
|
||||
genre_filter(t) or
|
||||
date_filter(t) or
|
||||
comment_filter(t))
|
||||
def uri_filter(t):
|
||||
return q == t.uri
|
||||
|
||||
def track_name_filter(t):
|
||||
return q == t.name
|
||||
|
||||
def album_filter(t):
|
||||
return q == getattr(getattr(t, 'album', None), 'name', None)
|
||||
|
||||
def artist_filter(t):
|
||||
return filter(lambda a: q == a.name, t.artists)
|
||||
|
||||
def albumartist_filter(t):
|
||||
return any([q == a.name for a in getattr(t.album,
|
||||
'artists', [])])
|
||||
|
||||
def composer_filter(t):
|
||||
return any([q == a.name for a in getattr(t, 'composers', [])])
|
||||
|
||||
def performer_filter(t):
|
||||
return any([q == a.name for a in getattr(t, 'performers', [])])
|
||||
|
||||
def track_no_filter(t):
|
||||
return q == t.track_no
|
||||
|
||||
def genre_filter(t):
|
||||
return (t.genre and q == t.genre)
|
||||
|
||||
def date_filter(t):
|
||||
return q == t.date
|
||||
|
||||
def comment_filter(t):
|
||||
return q == t.comment
|
||||
|
||||
def any_filter(t):
|
||||
return (uri_filter(t) or
|
||||
track_name_filter(t) or
|
||||
album_filter(t) or
|
||||
artist_filter(t) or
|
||||
albumartist_filter(t) or
|
||||
composer_filter(t) or
|
||||
performer_filter(t) or
|
||||
track_no_filter(t) or
|
||||
genre_filter(t) or
|
||||
date_filter(t) or
|
||||
comment_filter(t))
|
||||
|
||||
if field == 'uri':
|
||||
tracks = filter(uri_filter, tracks)
|
||||
@ -102,38 +117,56 @@ def search(tracks, query=None, uris=None):
|
||||
else:
|
||||
q = value.strip().lower()
|
||||
|
||||
uri_filter = lambda t: bool(t.uri and q in t.uri.lower())
|
||||
track_name_filter = lambda t: bool(t.name and q in t.name.lower())
|
||||
album_filter = lambda t: bool(
|
||||
t.album and t.album.name and q in t.album.name.lower())
|
||||
artist_filter = lambda t: bool(filter(
|
||||
lambda a: bool(a.name and q in a.name.lower()), t.artists))
|
||||
albumartist_filter = lambda t: any([
|
||||
a.name and q in a.name.lower()
|
||||
for a in getattr(t.album, 'artists', [])])
|
||||
composer_filter = lambda t: any([
|
||||
a.name and q in a.name.lower()
|
||||
for a in getattr(t, 'composers', [])])
|
||||
performer_filter = lambda t: any([
|
||||
a.name and q in a.name.lower()
|
||||
for a in getattr(t, 'performers', [])])
|
||||
track_no_filter = lambda t: q == t.track_no
|
||||
genre_filter = lambda t: bool(t.genre and q in t.genre.lower())
|
||||
date_filter = lambda t: bool(t.date and t.date.startswith(q))
|
||||
comment_filter = lambda t: bool(
|
||||
t.comment and q in t.comment.lower())
|
||||
any_filter = lambda t: (
|
||||
uri_filter(t) or
|
||||
track_name_filter(t) or
|
||||
album_filter(t) or
|
||||
artist_filter(t) or
|
||||
albumartist_filter(t) or
|
||||
composer_filter(t) or
|
||||
performer_filter(t) or
|
||||
track_no_filter(t) or
|
||||
genre_filter(t) or
|
||||
date_filter(t) or
|
||||
comment_filter(t))
|
||||
def uri_filter(t):
|
||||
return bool(t.uri and q in t.uri.lower())
|
||||
|
||||
def track_name_filter(t):
|
||||
return bool(t.name and q in t.name.lower())
|
||||
|
||||
def album_filter(t):
|
||||
return bool(t.album and t.album.name
|
||||
and q in t.album.name.lower())
|
||||
|
||||
def artist_filter(t):
|
||||
return bool(filter(lambda a:
|
||||
bool(a.name and q in a.name.lower()), t.artists))
|
||||
|
||||
def albumartist_filter(t):
|
||||
return any([a.name and q in a.name.lower()
|
||||
for a in getattr(t.album, 'artists', [])])
|
||||
|
||||
def composer_filter(t):
|
||||
return any([a.name and q in a.name.lower()
|
||||
for a in getattr(t, 'composers', [])])
|
||||
|
||||
def performer_filter(t):
|
||||
return any([a.name and q in a.name.lower()
|
||||
for a in getattr(t, 'performers', [])])
|
||||
|
||||
def track_no_filter(t):
|
||||
return q == t.track_no
|
||||
|
||||
def genre_filter(t):
|
||||
return bool(t.genre and q in t.genre.lower())
|
||||
|
||||
def date_filter(t):
|
||||
return bool(t.date and t.date.startswith(q))
|
||||
|
||||
def comment_filter(t):
|
||||
return bool(t.comment and q in t.comment.lower())
|
||||
|
||||
def any_filter(t):
|
||||
return (uri_filter(t) or
|
||||
track_name_filter(t) or
|
||||
album_filter(t) or
|
||||
artist_filter(t) or
|
||||
albumartist_filter(t) or
|
||||
composer_filter(t) or
|
||||
performer_filter(t) or
|
||||
track_no_filter(t) or
|
||||
genre_filter(t) or
|
||||
date_filter(t) or
|
||||
comment_filter(t))
|
||||
|
||||
if field == 'uri':
|
||||
tracks = filter(uri_filter, tracks)
|
||||
|
||||
@ -367,7 +367,9 @@ class Track(ImmutableObject):
|
||||
last_modified = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
get = lambda key: frozenset(kwargs.pop(key, None) or [])
|
||||
def get(key):
|
||||
return frozenset(kwargs.pop(key, None) or [])
|
||||
|
||||
self.__dict__['artists'] = get('artists')
|
||||
self.__dict__['composers'] = get('composers')
|
||||
self.__dict__['performers'] = get('performers')
|
||||
|
||||
@ -3,8 +3,7 @@ application-import-names = mopidy,tests
|
||||
exclude = .git,.tox,build,js
|
||||
# Ignored flake8 warnings:
|
||||
# - E402 module level import not at top of file
|
||||
# - E731 do not assign a lambda expression, use a def
|
||||
ignore = E402,E731
|
||||
ignore = E402
|
||||
|
||||
[wheel]
|
||||
universal = 1
|
||||
|
||||
@ -64,7 +64,8 @@ class TestCommands(unittest.TestCase):
|
||||
pass
|
||||
|
||||
def test_register_second_command_to_same_name_fails(self):
|
||||
func = lambda context: True
|
||||
def func(context):
|
||||
pass
|
||||
|
||||
self.commands.add('foo')(func)
|
||||
with self.assertRaises(Exception):
|
||||
@ -88,7 +89,10 @@ class TestCommands(unittest.TestCase):
|
||||
|
||||
def test_function_has_required_and_optional_args_succeeds(self):
|
||||
sentinel = object()
|
||||
func = lambda context, required, optional=None: sentinel
|
||||
|
||||
def func(context, required, optional=None):
|
||||
return sentinel
|
||||
|
||||
self.commands.add('bar')(func)
|
||||
self.assertEqual(sentinel, self.commands.call(['bar', 'arg']))
|
||||
self.assertEqual(sentinel, self.commands.call(['bar', 'arg', 'arg']))
|
||||
@ -111,12 +115,16 @@ class TestCommands(unittest.TestCase):
|
||||
|
||||
def test_function_has_required_and_varargs_fails(self):
|
||||
with self.assertRaises(TypeError):
|
||||
func = lambda context, required, *args: True
|
||||
def func(context, required, *args):
|
||||
pass
|
||||
|
||||
self.commands.add('test')(func)
|
||||
|
||||
def test_function_has_optional_and_varargs_fails(self):
|
||||
with self.assertRaises(TypeError):
|
||||
func = lambda context, optional=None, *args: True
|
||||
def func(context, optional=None, *args):
|
||||
pass
|
||||
|
||||
self.commands.add('test')(func)
|
||||
|
||||
def test_function_hash_keywordargs_fails(self):
|
||||
@ -158,7 +166,9 @@ class TestCommands(unittest.TestCase):
|
||||
self.assertEqual('test', self.commands.call(['foo', 'test']))
|
||||
|
||||
def test_call_passes_required_and_optional_argument(self):
|
||||
func = lambda context, required, optional=None: (required, optional)
|
||||
def func(context, required, optional=None):
|
||||
return (required, optional)
|
||||
|
||||
self.commands.add('foo')(func)
|
||||
self.assertEqual(('arg', None), self.commands.call(['foo', 'arg']))
|
||||
self.assertEqual(
|
||||
@ -182,20 +192,29 @@ class TestCommands(unittest.TestCase):
|
||||
|
||||
def test_validator_gets_applied_to_required_arg(self):
|
||||
sentinel = object()
|
||||
func = lambda context, required: required
|
||||
|
||||
def func(context, required):
|
||||
return required
|
||||
|
||||
self.commands.add('test', required=lambda v: sentinel)(func)
|
||||
self.assertEqual(sentinel, self.commands.call(['test', 'foo']))
|
||||
|
||||
def test_validator_gets_applied_to_optional_arg(self):
|
||||
sentinel = object()
|
||||
func = lambda context, optional=None: optional
|
||||
|
||||
def func(context, optional=None):
|
||||
return optional
|
||||
|
||||
self.commands.add('foo', optional=lambda v: sentinel)(func)
|
||||
|
||||
self.assertEqual(sentinel, self.commands.call(['foo', '123']))
|
||||
|
||||
def test_validator_skips_optional_default(self):
|
||||
sentinel = object()
|
||||
func = lambda context, optional=sentinel: optional
|
||||
|
||||
def func(context, optional=sentinel):
|
||||
return optional
|
||||
|
||||
self.commands.add('foo', optional=lambda v: None)(func)
|
||||
|
||||
self.assertEqual(sentinel, self.commands.call(['foo']))
|
||||
@ -203,28 +222,38 @@ class TestCommands(unittest.TestCase):
|
||||
def test_validator_applied_to_non_existent_arg_fails(self):
|
||||
self.commands.add('foo')(lambda context, arg: arg)
|
||||
with self.assertRaises(TypeError):
|
||||
func = lambda context, wrong_arg: wrong_arg
|
||||
def func(context, wrong_arg):
|
||||
return wrong_arg
|
||||
|
||||
self.commands.add('bar', arg=lambda v: v)(func)
|
||||
|
||||
def test_validator_called_context_fails(self):
|
||||
return # TODO: how to handle this
|
||||
with self.assertRaises(TypeError):
|
||||
func = lambda context: True
|
||||
def func(context):
|
||||
pass
|
||||
|
||||
self.commands.add('bar', context=lambda v: v)(func)
|
||||
|
||||
def test_validator_value_error_is_converted(self):
|
||||
def validdate(value):
|
||||
raise ValueError
|
||||
|
||||
func = lambda context, arg: True
|
||||
def func(context, arg):
|
||||
pass
|
||||
|
||||
self.commands.add('bar', arg=validdate)(func)
|
||||
|
||||
with self.assertRaises(exceptions.MpdArgError):
|
||||
self.commands.call(['bar', 'test'])
|
||||
|
||||
def test_auth_required_gets_stored(self):
|
||||
func1 = lambda context: context
|
||||
func2 = lambda context: context
|
||||
def func1(context):
|
||||
pass
|
||||
|
||||
def func2(context):
|
||||
pass
|
||||
|
||||
self.commands.add('foo')(func1)
|
||||
self.commands.add('bar', auth_required=False)(func2)
|
||||
|
||||
@ -232,8 +261,12 @@ class TestCommands(unittest.TestCase):
|
||||
self.assertFalse(self.commands.handlers['bar'].auth_required)
|
||||
|
||||
def test_list_command_gets_stored(self):
|
||||
func1 = lambda context: context
|
||||
func2 = lambda context: context
|
||||
def func1(context):
|
||||
pass
|
||||
|
||||
def func2(context):
|
||||
pass
|
||||
|
||||
self.commands.add('foo')(func1)
|
||||
self.commands.add('bar', list_command=False)(func2)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user