From a79ca4883a5a46c82afe909770140e0f9814148e Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Mon, 14 Dec 2015 11:06:48 -0800 Subject: [PATCH] For taskflow patterns don't show taskflow.patterns prefix Showing the full name in flow __str__() isn't expecially helpful and clutters up graphviz usage and similar so if a taskflow built-in pattern is stringified just use the short name (that does not start with taskflow.patterns instead of the full long name). Change-Id: Icd8c26eab1e19d6ecf9c4e8e82e6d6902ce5b6ea --- taskflow/flow.py | 10 ++++++++-- .../tests/unit/patterns/test_graph_flow.py | 16 +++++++++++++--- .../tests/unit/patterns/test_linear_flow.py | 19 +++++++++++++------ .../unit/patterns/test_unordered_flow.py | 16 +++++++++++++--- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/taskflow/flow.py b/taskflow/flow.py index 4d93edf4..a0f6846b 100644 --- a/taskflow/flow.py +++ b/taskflow/flow.py @@ -35,6 +35,10 @@ LINK_REASONS = 'reasons' # This key denotes a callable that will determine if the target is visited. LINK_DECIDER = 'decider' +# Chop off full module names of patterns that are built-in to taskflow... +_CHOP_PAT = "taskflow.patterns." +_CHOP_PAT_LEN = len(_CHOP_PAT) + @six.add_metaclass(abc.ABCMeta) class Flow(object): @@ -108,8 +112,10 @@ class Flow(object): """ def __str__(self): - return "%s: %s(len=%d)" % (reflection.get_class_name(self), - self.name, len(self)) + cls_name = reflection.get_class_name(self) + if cls_name.startswith(_CHOP_PAT): + cls_name = cls_name[_CHOP_PAT_LEN:] + return "%s: %s(len=%d)" % (cls_name, self.name, len(self)) @property def provides(self): diff --git a/taskflow/tests/unit/patterns/test_graph_flow.py b/taskflow/tests/unit/patterns/test_graph_flow.py index a1433d98..7197dc72 100644 --- a/taskflow/tests/unit/patterns/test_graph_flow.py +++ b/taskflow/tests/unit/patterns/test_graph_flow.py @@ -27,6 +27,19 @@ def _task(name, provides=None, requires=None): class GraphFlowTest(test.TestCase): + def test_graph_flow_stringy(self): + f = gf.Flow('test') + expected = 'graph_flow.Flow: test(len=0)' + self.assertEqual(expected, str(f)) + + task1 = _task(name='task1') + task2 = _task(name='task2') + task3 = _task(name='task3') + f = gf.Flow('test') + f.add(task1, task2, task3) + expected = 'graph_flow.Flow: test(len=3)' + self.assertEqual(expected, str(f)) + def test_graph_flow_starts_as_empty(self): f = gf.Flow('test') @@ -37,9 +50,6 @@ class GraphFlowTest(test.TestCase): self.assertEqual(set(), f.requires) self.assertEqual(set(), f.provides) - expected = 'taskflow.patterns.graph_flow.Flow: test(len=0)' - self.assertEqual(expected, str(f)) - def test_graph_flow_add_nothing(self): f = gf.Flow('test') result = f.add() diff --git a/taskflow/tests/unit/patterns/test_linear_flow.py b/taskflow/tests/unit/patterns/test_linear_flow.py index 046c8f31..d96835ec 100644 --- a/taskflow/tests/unit/patterns/test_linear_flow.py +++ b/taskflow/tests/unit/patterns/test_linear_flow.py @@ -26,6 +26,19 @@ def _task(name, provides=None, requires=None): class LinearFlowTest(test.TestCase): + def test_linear_flow_stringy(self): + f = lf.Flow('test') + expected = 'linear_flow.Flow: test(len=0)' + self.assertEqual(expected, str(f)) + + task1 = _task(name='task1') + task2 = _task(name='task2') + task3 = _task(name='task3') + f = lf.Flow('test') + f.add(task1, task2, task3) + expected = 'linear_flow.Flow: test(len=3)' + self.assertEqual(expected, str(f)) + def test_linear_flow_starts_as_empty(self): f = lf.Flow('test') @@ -36,9 +49,6 @@ class LinearFlowTest(test.TestCase): self.assertEqual(set(), f.requires) self.assertEqual(set(), f.provides) - expected = 'taskflow.patterns.linear_flow.Flow: test(len=0)' - self.assertEqual(expected, str(f)) - def test_linear_flow_add_nothing(self): f = lf.Flow('test') result = f.add() @@ -104,9 +114,6 @@ class LinearFlowTest(test.TestCase): (task2, task3, {'invariant': True}) ], list(f.iter_links())) - expected = 'taskflow.patterns.linear_flow.Flow: test(len=3)' - self.assertEqual(expected, str(f)) - def test_linear_flow_with_retry(self): ret = retry.AlwaysRevert(requires=['a'], provides=['b']) f = lf.Flow('test', ret) diff --git a/taskflow/tests/unit/patterns/test_unordered_flow.py b/taskflow/tests/unit/patterns/test_unordered_flow.py index d14c4e70..c5d14d33 100644 --- a/taskflow/tests/unit/patterns/test_unordered_flow.py +++ b/taskflow/tests/unit/patterns/test_unordered_flow.py @@ -26,6 +26,19 @@ def _task(name, provides=None, requires=None): class UnorderedFlowTest(test.TestCase): + def test_unordered_flow_stringy(self): + f = uf.Flow('test') + expected = 'unordered_flow.Flow: test(len=0)' + self.assertEqual(expected, str(f)) + + task1 = _task(name='task1') + task2 = _task(name='task2') + task3 = _task(name='task3') + f = uf.Flow('test') + f.add(task1, task2, task3) + expected = 'unordered_flow.Flow: test(len=3)' + self.assertEqual(expected, str(f)) + def test_unordered_flow_starts_as_empty(self): f = uf.Flow('test') @@ -36,9 +49,6 @@ class UnorderedFlowTest(test.TestCase): self.assertEqual(set(), f.requires) self.assertEqual(set(), f.provides) - expected = 'taskflow.patterns.unordered_flow.Flow: test(len=0)' - self.assertEqual(expected, str(f)) - def test_unordered_flow_add_nothing(self): f = uf.Flow('test') result = f.add()