From 203d9bcb45de2c51a6ec2da0aaf456e4034f7f40 Mon Sep 17 00:00:00 2001 From: phistakis Date: Tue, 8 Dec 2020 10:27:29 +0200 Subject: [PATCH 1/3] easy_repr classs decorator adds a standardaized __repr__ method to cls --- easypy/humanize.py | 14 ++++++++++++++ tests/test_humanize.py | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/easypy/humanize.py b/easypy/humanize.py index b5f8189e..a78df0c1 100644 --- a/easypy/humanize.py +++ b/easypy/humanize.py @@ -10,6 +10,7 @@ from math import ceil from collections import namedtuple from contextlib import contextmanager +from inspect import getargspec from io import StringIO from datetime import datetime, timedelta @@ -702,3 +703,16 @@ def __init__(self, value): def __repr__(self): return str(self.value) + + +def easy_repr(*attributes): + def _decorator(cls): + assert attributes, 'must provide at least one attribute' + + def _nice_repr(self): + attrs = ', '.join('{}: {!r}'.format(attr, getattr(self, attr)) for attr in attributes) + return f'<{self.__class__.__name__}: {attrs}>' + cls.__repr__ = _nice_repr + return cls + return _decorator + diff --git a/tests/test_humanize.py b/tests/test_humanize.py index a7d694d1..be27ad46 100644 --- a/tests/test_humanize.py +++ b/tests/test_humanize.py @@ -1,4 +1,4 @@ -from easypy.humanize import from_hexdump, hexdump, IndentableTextBuffer, format_table +from easypy.humanize import from_hexdump, hexdump, IndentableTextBuffer, format_table, easy_repr _SAMPLE_DATA = b'J\x9c\xe8Z!\xc2\xe6\x8b\xa0\x01\xcb\xc3.x]\x11\x9bsC\x1c\xb2\xcd\xb3\x9eM\xf7\x13`\xc8\xce\xf8g1H&\xe2\x9b' \ @@ -88,3 +88,40 @@ def test_format_table_without_titles(): "{'x': 'x'}|b'bytes'|string\n") assert output == format_table(table, titles=False) + + +def test_easy_repr(): + @easy_repr('a', 'b', 'c') + class Class1: + def __init__(self, a, b, c, d): + self.a = a + self.b = b + self.c = c + self.d = d + a = Class1('a', 'b', 1, 2) + assert repr(a) == "" + + # change order + @easy_repr('c', 'a', 'd') + class Class2: + def __init__(self, a, b, c, d): + self.a = a + self.b = b + self.c = c + self.d = d + a = Class2('a', 'b', 1, 2) + assert repr(a) == "" + + try: + @easy_repr() + class Class3: + def __init__(self, a, b, c, d): + self.a = a + self.b = b + self.c = c + self.d = d + except AssertionError: + pass + else: + assert False, 'easy_repr with no attributes should not be allowed' + From 9a79de194892bcc5f011b64b3831204cd88566f8 Mon Sep 17 00:00:00 2001 From: Ofer Koren Date: Tue, 8 Dec 2020 11:47:24 +0200 Subject: [PATCH 2/3] minor cleanups --- easypy/humanize.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/easypy/humanize.py b/easypy/humanize.py index a78df0c1..c7ea2792 100644 --- a/easypy/humanize.py +++ b/easypy/humanize.py @@ -706,12 +706,16 @@ def __repr__(self): def easy_repr(*attributes): + """ + Create a simple __repr__ function for the decorated class. + """ + def _decorator(cls): assert attributes, 'must provide at least one attribute' def _nice_repr(self): - attrs = ', '.join('{}: {!r}'.format(attr, getattr(self, attr)) for attr in attributes) - return f'<{self.__class__.__name__}: {attrs}>' + attrs = ', '.join('{}={!r}'.format(attr, getattr(self, attr)) for attr in attributes) + return '<{self.__class__.__name__}: {attrs}>'.format(self=self, attrs=attrs) cls.__repr__ = _nice_repr return cls return _decorator From 39e3468ca8b05a63d82dc986693a62dbebb4590d Mon Sep 17 00:00:00 2001 From: Ofer Koren Date: Tue, 8 Dec 2020 11:52:25 +0200 Subject: [PATCH 3/3] Update test_humanize.py --- tests/test_humanize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_humanize.py b/tests/test_humanize.py index be27ad46..ee1ce482 100644 --- a/tests/test_humanize.py +++ b/tests/test_humanize.py @@ -99,7 +99,7 @@ def __init__(self, a, b, c, d): self.c = c self.d = d a = Class1('a', 'b', 1, 2) - assert repr(a) == "" + assert repr(a) == "" # change order @easy_repr('c', 'a', 'd') @@ -110,7 +110,7 @@ def __init__(self, a, b, c, d): self.c = c self.d = d a = Class2('a', 'b', 1, 2) - assert repr(a) == "" + assert repr(a) == "" try: @easy_repr()