From ce9bef1c5be77011ee5fc59c65fc3ce2a7ab14f2 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Thu, 13 Jul 2017 16:42:57 +0100 Subject: [PATCH] Add a new parameter ``suffix`` to function ``get_traits`` Added a new optional parameter ``suffix`` to function ``get_traits``. This new parameter allows filtering the list of traits returned by the ending of the name. Change-Id: Ifadb72a1b8b64971c0377eaef89522fe979c0c87 --- os_traits/__init__.py | 7 +++++-- os_traits/tests/test_os_traits.py | 18 +++++++++++++++++- ...add-suffix-get_traits-d1d91edcf7f65188.yaml | 6 ++++++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/add-suffix-get_traits-d1d91edcf7f65188.yaml diff --git a/os_traits/__init__.py b/os_traits/__init__.py index bb15a1f..55ca9a5 100644 --- a/os_traits/__init__.py +++ b/os_traits/__init__.py @@ -65,18 +65,21 @@ def import_submodules(package, recursive=True): import_submodules(sys.modules.get(__name__)) -def get_traits(prefix=None): +def get_traits(prefix=None, suffix=None): """Returns the trait strings in the os_traits module, optionally filtered - by a supplied prefix. + by a supplied prefix and suffix. :param prefix: Optional string prefix to filter by. e.g. 'HW_' + :param suffix: Optional string suffix to filter by, e.g. 'SSE' """ prefix = prefix or "" + suffix = suffix or "" return [ v for k, v in sys.modules[__name__].__dict__.items() if isinstance(v, six.string_types) and not k.startswith('_') and v.startswith(prefix) and + v.endswith(suffix) and # skip module constants k not in ('CUSTOM_NAMESPACE', 'THIS_NAME', 'THIS_LIB', 'TEST_DIR') ] diff --git a/os_traits/tests/test_os_traits.py b/os_traits/tests/test_os_traits.py index 66f1d3b..668df55 100644 --- a/os_traits/tests/test_os_traits.py +++ b/os_traits/tests/test_os_traits.py @@ -38,7 +38,7 @@ class TestSymbols(base.TestCase): ot.HW_GPU_RESOLUTION_W1920H1080) self.assertEqual(offload.TSO, ot.HW_NIC_OFFLOAD_TSO) - def test_get_traits(self): + def test_get_traits_filter_by_prefix(self): traits = ot.get_traits('HW_CPU') self.assertIn("HW_CPU_X86_SSE42", traits) self.assertIn(ot.HW_CPU_X86_AVX2, traits) @@ -47,6 +47,22 @@ class TestSymbols(base.TestCase): self.assertNotIn('CUSTOM_NAMESPACE', traits) self.assertNotIn('os_traits', traits) + def test_get_traits_filter_by_suffix(self): + traits = ot.get_traits(suffix='SSE42') + self.assertIn("HW_CPU_X86_SSE42", traits) + self.assertEqual(1, len(traits)) + + def test_get_traits_filter_by_prefix_and_suffix(self): + traits = ot.get_traits(prefix='HW_NIC', suffix='RSA') + self.assertIn("HW_NIC_ACCEL_RSA", traits) + self.assertNotIn(ot.HW_NIC_ACCEL_TLS, traits) + self.assertEqual(1, len(traits)) + + traits = ot.get_traits(prefix='HW_NIC', suffix='TX') + self.assertIn("HW_NIC_SRIOV_QOS_TX", traits) + self.assertIn("HW_NIC_OFFLOAD_TX", traits) + self.assertEqual(2, len(traits)) + def test_check_traits(self): traits = set(["HW_CPU_X86_SSE42", "HW_CPU_X86_XOP"]) not_traits = set(["not_trait1", "not_trait2"]) diff --git a/releasenotes/notes/add-suffix-get_traits-d1d91edcf7f65188.yaml b/releasenotes/notes/add-suffix-get_traits-d1d91edcf7f65188.yaml new file mode 100644 index 0000000..db67a73 --- /dev/null +++ b/releasenotes/notes/add-suffix-get_traits-d1d91edcf7f65188.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Added a new optional parameter ``suffix`` to function ``get_traits``. This + new parameter allows filtering the list of traits returned by the ending + of the name.