From: Marc Schink Date: Fri, 29 Mar 2024 12:38:23 +0000 (+0100) Subject: bindings/python: More user-friendly class string representation X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=cc71138f09a271566e033eb74975531f61271eb0;p=libsigrok.git bindings/python: More user-friendly class string representation Add a function to generate a more descriptive and user-friendly class string representation using class attributes. Signed-off-by: Marc Schink --- diff --git a/bindings/python/sigrok/core/classes.i b/bindings/python/sigrok/core/classes.i index b0b7fcfa..325983d7 100644 --- a/bindings/python/sigrok/core/classes.i +++ b/bindings/python/sigrok/core/classes.i @@ -81,6 +81,15 @@ typedef guint pyg_flags_type; %include "../../../swig/templates.i" +%pythoncode +{ + def _class_attribute_repr(obj, attributes): + attribute_str = ", ".join( + ["%s=%s" % (key, repr(obj.__getattribute__(key))) for key in attributes] + ) + return "{:s} ({:s})".format(obj.__class__.__name__, attribute_str) +} + /* Map file objects to file descriptors. */ %typecheck(SWIG_TYPECHECK_POINTER) int fd { $1 = (PyObject_AsFileDescriptor($input) != -1);