cardinal_pythonlib.reprfunc
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Functions to assist making repr() methods for Python objects.
- cardinal_pythonlib.reprfunc.auto_repr(obj: Any, with_addr: bool = False, sort_attrs: bool = True, joiner: str = ', ') str [source]
Convenience function for
__repr__()
. Works its way through the object’s__dict__
and reports accordingly.
- cardinal_pythonlib.reprfunc.auto_str(obj: Any, indent: int = 4, width: int = 80, depth: int | None = None, compact: bool = False) str [source]
Make a pretty
str()
representation usingpprint.pformat()
and the object’s__dict__
attribute.
- cardinal_pythonlib.reprfunc.mapped_repr(obj: Any, attributes: List[Tuple[str, str]], with_addr: bool = False, joiner: str = ', ') str [source]
Convenience function for
__repr__()
. Takes attribute names and corresponding initialization parameter names (parameters to__init__()
).
- cardinal_pythonlib.reprfunc.mapped_repr_stripping_underscores(obj: Any, attrnames: List[str], with_addr: bool = False, joiner: str = ', ') str [source]
Convenience function for
__repr__()
. Here, you pass a list of internal attributes, and it assumes that the__init__()
parameter names have the leading underscore dropped.
- cardinal_pythonlib.reprfunc.ordered_repr(obj: object, attrlist: Iterable[str], joiner: str = ', ') str [source]
Shortcut to make
repr()
functions ordered. Define your__repr__()
like this:def __repr__(self): return ordered_repr(self, ["field1", "field2", "field3"])
- cardinal_pythonlib.reprfunc.repr_result(obj: Any, elements: List[str], with_addr: bool = False, joiner: str = ', ') str [source]
Internal function to make a
repr()
-style representation of an object.
- cardinal_pythonlib.reprfunc.simple_repr(obj: Any, attrnames: List[str], with_addr: bool = False, joiner: str = ', ') str [source]
Convenience function for
__repr__()
. Works its way through a list of attribute names, and creates arepr()
representation assuming that parameters to the constructor have the same names.