From 0fa1a9036001e56c96bc38cf5855628a5750aae4 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 3 Nov 2020 16:40:54 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20ColRepr=20to=20alchemist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/alchemist/__init__.py | 1 + royalnet/alchemist/repr.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 royalnet/alchemist/repr.py diff --git a/royalnet/alchemist/__init__.py b/royalnet/alchemist/__init__.py index 8ce214bb..57a80848 100644 --- a/royalnet/alchemist/__init__.py +++ b/royalnet/alchemist/__init__.py @@ -1 +1,2 @@ from .func import * +from .repr import * \ No newline at end of file diff --git a/royalnet/alchemist/repr.py b/royalnet/alchemist/repr.py new file mode 100644 index 00000000..f6688ac0 --- /dev/null +++ b/royalnet/alchemist/repr.py @@ -0,0 +1,12 @@ +class ColRepr: + """A mixin that can be added to a declared class to display all table columns in the __repr__.""" + + def __repr__(self): + columns = [c.name for c in self.__class__.__table__.columns] + args = [f"{c}={repr(self.__getattribute__(c))}" for c in columns] + return f"{self.__class__.__qualname__}({', '.join(args)})" + + +__all__ = ( + "ColRepr", +)