Commit 2cc840c2 authored by Samuel GAIST's avatar Samuel GAIST

[inputs] Cleanup "get first item" implementation

Python provides better tools that exceptions for that.
parent 548cd43a
......@@ -50,6 +50,16 @@ import six
# ----------------------------------------------------------
def first(iterable, default=None):
"""Get the first item of a list or default
"""
return next(iter(iterable), default)
# ----------------------------------------------------------
class Input(object):
"""Represents an input of a processing block that receive data from a
(legacy) data source
......@@ -217,10 +227,7 @@ class InputGroup:
def __getitem__(self, index):
if isinstance(index, six.string_types):
try:
return [x for x in self._inputs if x.name == index][0]
except Exception: # nosec
pass
return first([x for x in self._inputs if x.name == index])
elif isinstance(index, int):
if index < len(self._inputs):
return self._inputs[index]
......@@ -378,13 +385,9 @@ class InputList:
def __getitem__(self, index):
if isinstance(index, six.string_types):
try:
return [
k for k in map(lambda x: x[index], self._groups) if k is not None
][0]
except Exception: # nosec
pass
return first(
[k for k in map(lambda x: x[index], self._groups) if k is not None]
)
elif isinstance(index, int):
for group in self._groups:
if index < len(group):
......@@ -411,10 +414,7 @@ class InputList:
:param str input_name: Name of the input for which the group should
be search for.
"""
try:
return [k for k in self._groups if k[input_name] is not None][0]
except Exception:
return None
return first([k for k in self._groups if k[input_name] is not None])
def hasMoreData(self):
"""Indicates if there is more data to process in any group"""
......@@ -424,10 +424,7 @@ class InputList:
"""Returns the group matching the name or index passed as parameter"""
if isinstance(name_or_index, six.string_types):
try:
return [x for x in self._groups if x.channel == name_or_index][0]
except Exception:
return None
return first([x for x in self._groups if x.channel == name_or_index])
elif isinstance(name_or_index, int):
return self._groups[name_or_index]
else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment