Commits

Joe LaFata  committed 5a9bb04

improve string output of parameters

  • Participants
  • Parent commits d933b2f

Comments (0)

Files changed (2)

File src/tests/test_unitbench.py

         eq_(sum23(), 5)
         eq_(adder(7, 9), 16)
         eq_(adder(1, 1), 2)
-        eq_(len(fp.results), 3)
+        print fp.results
+        eq_(len(fp.results), 5)
+        
+    def test_args_to_str(self):
+        eq_(FunctionProfiler._args_to_str(),
+           "None")
+        eq_(FunctionProfiler._args_to_str(1),
+           "[1]")
+        eq_(FunctionProfiler._args_to_str(1, 2, 3, 4),
+           "[1, 2, 3, 4]")
+        eq_(FunctionProfiler._args_to_str(1, 2, 3, 4, x=5),
+           "[1, 2, 3, 4] {\"x\":5}")
+        eq_(FunctionProfiler._args_to_str(x=5),
+           "{\"x\":5}")
+        eq_(FunctionProfiler._args_to_str(x=5, y=True),
+           "{\"x\":5, \"y\":True}")
+        eq_(FunctionProfiler._args_to_str(2, x=5, y=True),
+           "[2] {\"x\":5, \"y\":True}")
+        eq_(FunctionProfiler._args_to_str('', x=5, y=True),
+           "[\"\"] {\"x\":5, \"y\":True}")
     
 class TestBenchmark(TestCase):
     def test_warmup(self):

File src/unitbench.py

 
 from collections import deque, Iterable
 from functools import partial
-from operator import attrgetter
+from operator import attrgetter, itemgetter
 
 import inspect
 import math
     __slots__ = ["repeats", "warmup", "results"]
     
     @staticmethod
-    def _args_to_str(self, *args, **kwargs):
+    def _to_str(x):
+        if isinstance(x, str):
+            return "\"" + x + "\""
+        if hasattr(x, __name__):
+            return x.__name__
+        return str(x)
+    
+    @staticmethod
+    def _args_to_str(*args, **kwargs):
+        to_str = FunctionProfiler._to_str
         value = ''
         if len(args) + len(kwargs) == 0:
-            value = 'None'
-        elif len(kwargs) == 0:
-            value = '(' + str(args) + ')'
-        elif len(args) == 0:
-            value = '(' + str(kwargs) + ')'
-        else:
-            value = '(' + str(args) + ' ' + str(kwargs) + ')'
+            return 'None'
+        
+        if len(args) > 0:
+            value = '[' + ', '.join(map(to_str, args)) + ']'
+        
+        if len(kwargs) > 0:
+            if len(value) > 0:
+                value += ' '
+            sorted_kwargs = sorted(kwargs.iteritems(), key=itemgetter(0))
+            value += '{' + ', '.join(
+                 map(lambda x: to_str(x[0]) + ':' + to_str(x[1]), sorted_kwargs)) + '}'
         return value
     
+    @staticmethod
+    def _wrap_int(x):
+        def wrapper():
+            return x
+        return wrapper
+    
     def __init__(self, repeats=1, warmup=0):
-        self.repeats = self._wrap_int(repeats)
-        self.warmup = self._wrap_int(warmup)
+        self.repeats = FunctionProfiler._wrap_int(repeats)
+        self.warmup = FunctionProfiler._wrap_int(warmup)
         
         self.results = {}
         
             reporter = ConsoleReporter(group_by=ConsoleReporter.GroupType.FUNCTION)
         results = sorted(self.results, key = lambda x : len(self.results[x]))
         reporter.write_results(results)
-    
-    def _wrap_int(self, x):
-        def wrapper():
-            return x
-        return wrapper
 
 class Benchmark(BenchBase):
     """