summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane
diff options
context:
space:
mode:
authorDan Klein <danklein10@gmail.com>2016-04-19 01:44:04 +0300
committerDan Klein <danklein10@gmail.com>2016-04-19 01:44:04 +0300
commit3bafb0394c07ef2abb4ce34c7fb4ec01eb09f2df (patch)
tree3a946134ede4bac619d012539f9a3f39f115ed9d /scripts/automation/trex_control_plane
parent330cf8f7950027efe7b4bbc96a7ee1af3ee0b10a (diff)
All unit tests passes with both python 2 and python 3.
Added more documentation to the ToggleFilter class.
Diffstat (limited to 'scripts/automation/trex_control_plane')
-rw-r--r--scripts/automation/trex_control_plane/common/filters.py54
1 files changed, 51 insertions, 3 deletions
diff --git a/scripts/automation/trex_control_plane/common/filters.py b/scripts/automation/trex_control_plane/common/filters.py
index d2ca7b16..bf04a775 100644
--- a/scripts/automation/trex_control_plane/common/filters.py
+++ b/scripts/automation/trex_control_plane/common/filters.py
@@ -8,12 +8,43 @@ class ToggleFilter(object):
This class provides a "sticky" filter, that works by "toggling" items of the original database on and off.
"""
def __init__(self, db_ref, show_by_default=True):
+ """
+ Instantiate a ToggleFilter object
+
+ :parameters:
+ db_ref : iterable
+ an iterable object (i.e. list, set etc) that would serve as the reference db of the instance.
+ Changes in that object will affect the output of ToggleFilter instance.
+
+ show_by_default: bool
+ decide if by default all the items are "on", i.e. these items will be presented if no other
+ toggling occurred.
+
+ default value : **True**
+
+ """
self._data = db_ref
self._toggle_db = set()
self._filter_method = filter
self.__set_initial_state(show_by_default)
def toggle_item(self, item_key):
+ """
+ Toggle a single item in/out.
+
+ :parameters:
+ item_key :
+ an item the by its value the filter can decide to toggle or not.
+ Example: int, str and so on.
+
+ :return:
+ + **True** if item toggled **into** the filtered items
+ + **False** if item toggled **out from** the filtered items
+
+ :raises:
+ + KeyError, in case if item key is not part of the toggled list and not part of the referenced db.
+
+ """
if item_key in self._toggle_db:
self._toggle_db.remove(item_key)
return False
@@ -24,7 +55,23 @@ class ToggleFilter(object):
raise KeyError("Provided item key isn't a key of the referenced data structure.")
def toggle_items(self, *args):
- return all(map(self.toggle_item, args))
+ """
+ Toggle multiple items in/out with a single call. Each item will be ha.
+
+ :parameters:
+ args : iterable
+ an iterable object containing all item keys to be toggled in/out
+
+ :return:
+ + **True** if all toggled items were toggled **into** the filtered items
+ + **False** if at least one of the items was toggled **out from** the filtered items
+
+ :raises:
+ + KeyError, in case if ont of the item keys was not part of the toggled list and not part of the referenced db.
+
+ """
+ # in python 3, 'map' returns an iterator, so wrapping with 'list' call creates same effect for both python 2 and 3
+ return all(list(map(self.toggle_item, args)))
def filter_items(self):
"""
@@ -67,12 +114,13 @@ class ToggleFilter(object):
def dict_filter(function, iterable):
assert isinstance(iterable, dict)
return {k: v
- for k,v in iterable.iteritems()
+ for k,v in iterable.items()
if function(k)}
@staticmethod
def list_filter(function, iterable):
- return filter(function, iterable)
+ # in python 3, filter returns an iterator, so wrapping with list creates same effect for both python 2 and 3
+ return list(filter(function, iterable))
@staticmethod
def set_filter(function, iterable):