diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2015-07-11 19:52:45 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2015-07-11 19:54:54 +0200 |
commit | 63edb526bc4efd8cf7abe260f2787f55d2953e39 (patch) | |
tree | cf7e897d10f8850bb4d9392dbc41ba6a09df0e1c | |
parent | 6d190fa33f8a0cd625d3ce13d6de29bd5b72e742 (diff) | |
download | factory-boy-63edb526bc4efd8cf7abe260f2787f55d2953e39.tar factory-boy-63edb526bc4efd8cf7abe260f2787f55d2953e39.tar.gz |
Improve debug logging efficiency (Closes #155).
As suggested by @adamchainz, use lazy computation of args/kwargs pprint
to only perform complex computation when running with debug.
-rw-r--r-- | factory/utils.py | 32 | ||||
-rw-r--r-- | tests/test_utils.py | 14 |
2 files changed, 30 insertions, 16 deletions
diff --git a/factory/utils.py b/factory/utils.py index 6ecf9a7..806b1ec 100644 --- a/factory/utils.py +++ b/factory/utils.py @@ -110,15 +110,29 @@ def _safe_repr(obj): return obj_repr.decode('utf-8') -def log_pprint(args=(), kwargs=None): - kwargs = kwargs or {} - return ', '.join( - [_safe_repr(arg) for arg in args] + - [ - '%s=%s' % (key, _safe_repr(value)) - for key, value in kwargs.items() - ] - ) +class log_pprint(object): + """Helper for properly printing args / kwargs passed to an object. + + Since it is only used with factory.debug(), the computation is + performed lazily. + """ + __slots__ = ['args', 'kwargs'] + + def __init__(self, args=(), kwargs=None): + self.args = args + self.kwargs = kwargs or {} + + def __repr__(self): + return repr(str(self)) + + def __str__(self): + return ', '.join( + [_safe_repr(arg) for arg in self.args] + + [ + '%s=%s' % (key, _safe_repr(value)) + for key, value in self.kwargs.items() + ] + ) class ResetableIterator(object): diff --git a/tests/test_utils.py b/tests/test_utils.py index eed7a57..77598e1 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -238,33 +238,33 @@ class ImportObjectTestCase(unittest.TestCase): class LogPPrintTestCase(unittest.TestCase): def test_nothing(self): - txt = utils.log_pprint() + txt = str(utils.log_pprint()) self.assertEqual('', txt) def test_only_args(self): - txt = utils.log_pprint((1, 2, 3)) + txt = str(utils.log_pprint((1, 2, 3))) self.assertEqual('1, 2, 3', txt) def test_only_kwargs(self): - txt = utils.log_pprint(kwargs={'a': 1, 'b': 2}) + txt = str(utils.log_pprint(kwargs={'a': 1, 'b': 2})) self.assertIn(txt, ['a=1, b=2', 'b=2, a=1']) def test_bytes_args(self): - txt = utils.log_pprint((b'\xe1\xe2',)) + txt = str(utils.log_pprint((b'\xe1\xe2',))) expected = "b'\\xe1\\xe2'" if is_python2: expected = expected.lstrip('b') self.assertEqual(expected, txt) def test_text_args(self): - txt = utils.log_pprint(('ŧêßŧ',)) + txt = str(utils.log_pprint(('ŧêßŧ',))) expected = "'ŧêßŧ'" if is_python2: expected = "u'\\u0167\\xea\\xdf\\u0167'" self.assertEqual(expected, txt) def test_bytes_kwargs(self): - txt = utils.log_pprint(kwargs={'x': b'\xe1\xe2', 'y': b'\xe2\xe1'}) + txt = str(utils.log_pprint(kwargs={'x': b'\xe1\xe2', 'y': b'\xe2\xe1'})) expected1 = "x=b'\\xe1\\xe2', y=b'\\xe2\\xe1'" expected2 = "y=b'\\xe2\\xe1', x=b'\\xe1\\xe2'" if is_python2: @@ -273,7 +273,7 @@ class LogPPrintTestCase(unittest.TestCase): self.assertIn(txt, (expected1, expected2)) def test_text_kwargs(self): - txt = utils.log_pprint(kwargs={'x': 'ŧêßŧ', 'y': 'ŧßêŧ'}) + txt = str(utils.log_pprint(kwargs={'x': 'ŧêßŧ', 'y': 'ŧßêŧ'})) expected1 = "x='ŧêßŧ', y='ŧßêŧ'" expected2 = "y='ŧßêŧ', x='ŧêßŧ'" if is_python2: |