Generate a ``__str__`` method for a `.Transform` subclass. After :: class T: __str__ = _make_str_method("attr", key="other") ``str(T(...))`` will be .. code-block:: text {type(T).__name__}( {self.attr}, key={self.other})
(*args, **kwargs)
| 52 | |
| 53 | |
| 54 | def _make_str_method(*args, **kwargs): |
| 55 | """ |
| 56 | Generate a ``__str__`` method for a `.Transform` subclass. |
| 57 | |
| 58 | After :: |
| 59 | |
| 60 | class T: |
| 61 | __str__ = _make_str_method("attr", key="other") |
| 62 | |
| 63 | ``str(T(...))`` will be |
| 64 | |
| 65 | .. code-block:: text |
| 66 | |
| 67 | {type(T).__name__}( |
| 68 | {self.attr}, |
| 69 | key={self.other}) |
| 70 | """ |
| 71 | indent = functools.partial(textwrap.indent, prefix=" " * 4) |
| 72 | def strrepr(x): return repr(x) if isinstance(x, str) else str(x) |
| 73 | return lambda self: ( |
| 74 | type(self).__name__ + "(" |
| 75 | + ",".join([*(indent("\n" + strrepr(getattr(self, arg))) |
| 76 | for arg in args), |
| 77 | *(indent("\n" + k + "=" + strrepr(getattr(self, arg))) |
| 78 | for k, arg in kwargs.items())]) |
| 79 | + ")") |
| 80 | |
| 81 | |
| 82 | class TransformNode: |
no test coverage detected
searching dependent graphs…