[Piglit] [PATCH 1/3] framework/grouptools.py: Add a function for print formatting

baker.dylan.c at gmail.com baker.dylan.c at gmail.com
Fri Nov 20 17:10:01 PST 2015


From: Dylan Baker <baker.dylan.c at gmail.com>

This adds a function to grouptools that replaces grouptools.SEPARATOR
with '/'. This function is meant for use when printing to the console,
since most people find '/' easier to read than whatever is being used
internally to separate a group, and because consistance counts.

The implementation differs from that originally used in console_.py,
this implementation (tested on python 2.7.10) is roughly twice as fast.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---

For those wondering about the 2x performance gain I mention, you can run
this script to see the difference:

"""
from __future__ import absolute_import, division, print_function
import timeit

SEPARATOR = '@'
TESTSTRING = 'foo at bar@boink at foo@john'

print('split-join')
print(timeit.timeit(lambda: '/'.join(TESTSTRING.split(SEPARATOR))))

print('replace')
print(timeit.timeit(lambda: TESTSTRING.replace(SEPARATOR, '/')))
"""

 framework/grouptools.py             | 17 +++++++++++++++++
 framework/summary/console_.py       |  2 +-
 framework/tests/grouptools_tests.py |  7 +++++++
 3 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/framework/grouptools.py b/framework/grouptools.py
index 62d3f9c..780f3a8 100644
--- a/framework/grouptools.py
+++ b/framework/grouptools.py
@@ -30,7 +30,9 @@ posix paths they may not start with a leading '/'.
 """
 
 __all__ = [
+    'SEPARATOR',
     'commonprefix',
+    'format',
     'from_path',
     'groupname',
     'join',
@@ -161,3 +163,18 @@ def from_path(path):
     if '.' == path:
         return ''
     return path
+
+
+def format(name):
+    """Format an internal name for printing.
+
+    It doesn't matter how the name is stored internally, presenting a
+    consistant, clean interface on the command line that doesn't contain any
+    ugly or problematic chractrs is important.
+
+    This replaces SEPARATOR with '/', which is what most devs are used to and
+    want to see.
+
+    """
+    assert isinstance(name, basestring)
+    return name.replace(SEPARATOR, '/')
diff --git a/framework/summary/console_.py b/framework/summary/console_.py
index 03ea239..d219498 100644
--- a/framework/summary/console_.py
+++ b/framework/summary/console_.py
@@ -91,7 +91,7 @@ def _print_result(results, list_):
     """Takes a list of test names to print and prints the name and result."""
     for test in sorted(list_):
         print("{test}: {statuses}".format(
-            test='/'.join(test.split(grouptools.SEPARATOR)),
+            test=grouptools.format(test),
             statuses=' '.join(str(r) for r in results.get_result(test))))
 
 
diff --git a/framework/tests/grouptools_tests.py b/framework/tests/grouptools_tests.py
index acab7ed..3dd0451 100644
--- a/framework/tests/grouptools_tests.py
+++ b/framework/tests/grouptools_tests.py
@@ -119,3 +119,10 @@ def test_join_empty():
 def test_commonprefix_none():
     """grouptools.commonprefix: returns '' when no values are the same"""
     nt.eq_('', grouptools.commonprefix(['foo', 'bar']))
+
+
+ at doc_formatter
+def test_format():
+    """grouptools.format: replaces {seperator} with '/'"""
+    test_str = grouptools.SEPARATOR.join(['foo', 'bar', 'boink'])
+    nt.eq_(grouptools.format(test_str), 'foo/bar/boink')
-- 
2.6.2



More information about the Piglit mailing list