From 2233f60f5829cf3b651403f8fc77c28b907b60e9 Mon Sep 17 00:00:00 2001 From: Nils Philippsen Date: Sep 22 2016 14:08:52 +0000 Subject: implement BaseMessage.__repr__ Take advantage of the information contained in signature/parameter objects to represent positional and keyword arguments properly. Fall back to using the funcsigs module on Python 2. --- diff --git a/requirements.txt b/requirements.txt index 1959443..d0fc5f7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ Flask-Script Flask-SQLAlchemy Flask-Migrate python-fedora +funcsigs # Python2 only diff --git a/rida/messaging.py b/rida/messaging.py index fa45ee9..f07dc30 100644 --- a/rida/messaging.py +++ b/rida/messaging.py @@ -25,6 +25,11 @@ """Generic messaging functions.""" import re +try: + from inspect import signature +except ImportError: + from funcsigs import signature + from rida import logger @@ -36,6 +41,17 @@ class BaseMessage(object): """ self.msg_id = msg_id + def __repr__(self): + init_sig = signature(self.__init__) + + args_strs = ( + "{}={!r}".format(name, getattr(self, name)) + if param.default != param.empty + else repr(getattr(self, name)) + for name, param in init_sig.parameters.items()) + + return "{}({})".format(type(self).__name__, ', '.join(args_strs)) + @staticmethod def from_fedmsg(topic, msg): """