From: Naoki Kambe Date: Fri, 14 Dec 2012 08:34:04 +0000 (+0900) Subject: [2225_statistics] add a helper method _concat and its test X-Git-Tag: bind10-1.1.0beta1-release~72^2~40^2~27 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=652859d50201a5fd7d7ec3bc4266ead82fc86c4b;p=thirdparty%2Fkea.git [2225_statistics] add a helper method _concat and its test And replace it with '/'.join(args) Due to the review comment. --- diff --git a/src/lib/python/isc/statistics/counter.py b/src/lib/python/isc/statistics/counter.py index 81350978ad..acce8d8a68 100644 --- a/src/lib/python/isc/statistics/counter.py +++ b/src/lib/python/isc/statistics/counter.py @@ -134,6 +134,11 @@ def _stop_timer(start_time, element, spec, identifier): delta.microseconds * 1E-6, 6) _set_counter(element, spec, identifier, sec) +def _concat(*args, sep='/'): + """Concatenates words in args with a separator('/') + """ + return sep.join(args) + class _Statistics(): """Statistics data set""" # default statistics data @@ -291,7 +296,7 @@ class Counters(): caller. isc.cc.data.DataNotFoundError is raised when incrementing the counter of the item undefined in the spec file.""" - identifier = '/'.join(args) + identifier = _concat(*args) step = 1 with self._rlock: if self._disabled: return @@ -305,7 +310,7 @@ class Counters(): caller. isc.cc.data.DataNotFoundError is raised when decrementing the counter of the item undefined in the spec file.""" - identifier = '/'.join(args) + identifier = _concat(*args) step = -1 with self._rlock: if self._disabled: return @@ -317,13 +322,13 @@ class Counters(): """A getter method for counters. It returns the current number of the specified counter. isc.cc.data.DataNotFoundError is raised when the counter doesn't have a number yet.""" - identifier = '/'.join(args) + identifier = _concat(*args) return _get_counter(self._statistics._data, identifier) def start(self, *args): """Sets the value returned from _start_timer() as a value of the identifier in the self._start_time which is dict-type""" - identifier = '/'.join(args) + identifier = _concat(*args) with self._rlock: if self._disabled: return isc.cc.data.set(self._start_time, identifier, _start_timer()) @@ -336,7 +341,7 @@ class Counters(): timer which has never been started, it raises and does nothing. But in case of stopping the time which isn't defined in the spec file, it raises DataNotFoundError""" - identifier = '/'.join(args) + identifier = _concat(*args) with self._rlock: if self._disabled: return try: @@ -355,7 +360,7 @@ class Counters(): # delete the started timer del isc.cc.data.find( self._start_time, - '/'.join(identifier.split('/')[0:-1]))\ + _concat(*identifier.split('/')[0:-1]))\ [identifier.split('/')[-1]] def dump_statistics(self): diff --git a/src/lib/python/isc/statistics/tests/counter_test.py b/src/lib/python/isc/statistics/tests/counter_test.py index d454ceb22f..1a5c806d1b 100644 --- a/src/lib/python/isc/statistics/tests/counter_test.py +++ b/src/lib/python/isc/statistics/tests/counter_test.py @@ -142,6 +142,22 @@ class TestBasicMethods(unittest.TestCase): counter._get_counter(self.counters._statistics._data, timer_name), 0) + def test_concat(self): + # only strings + a = ( 'a','b','c','d' ) + self.assertEqual('a/b/c/d', counter._concat(*a)) + self.assertEqual('aTbTcTd', counter._concat(*a, sep='T')) + self.assertEqual('a\\b\\c\\d', counter._concat(*a, sep='\\')) + # mixed with other types + b = a + (1,) + self.assertRaises(TypeError, counter._concat, *b) + b = a + (1.1,) + self.assertRaises(TypeError, counter._concat, *b) + b = a + ([],) + self.assertRaises(TypeError, counter._concat, *b) + b = a + ({},) + self.assertRaises(TypeError, counter._concat, *b) + class BaseTestCounters(): def setUp(self):