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.

Parameters:
  • obj – object to display

  • with_addr – include the memory address of obj

  • sort_attrs – sort the attributes into alphabetical order?

  • joiner – string with which to join the elements

Returns:

repr()-style representation

Return type:

string

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 using pprint.pformat() and the object’s __dict__ attribute.

Parameters:
Returns:

str()-style representation

Return type:

string

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__()).

Parameters:
  • obj – object to display

  • attributes – list of tuples, each (attr_name, init_param_name).

  • with_addr – include the memory address of obj

  • joiner – string with which to join the elements

Returns:

repr()-style representation

Return type:

string

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.

Parameters:
  • obj – object to display

  • attrnames – list of attribute names

  • with_addr – include the memory address of obj

  • joiner – string with which to join the elements

Returns:

repr()-style representation

Return type:

string

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"])
Parameters:
  • obj – object to display

  • attrlist – iterable of attribute names

  • joiner – string with which to join the elements

Returns:

repr()-style representation

Return type:

string

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.

Parameters:
  • obj – object to display

  • elements – list of object attribute=value strings

  • with_addr – include the memory address of obj

  • joiner – string with which to join the elements

Returns:

repr()-style representation

Return type:

string

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 a repr() representation assuming that parameters to the constructor have the same names.

Parameters:
  • obj – object to display

  • attrnames – names of attributes to include

  • with_addr – include the memory address of obj

  • joiner – string with which to join the elements

Returns:

repr()-style representation

Return type:

string