attribute_call_string

plasmapy.utils.code_repr.attribute_call_string(
cls,
attr: str,
args_to_cls: tuple | Any | None = None,
kwargs_to_cls: dict[str, Any] | None = None,
max_items: int = 12,
) str[source]

Approximate the command to instantiate a class, and access an attribute of the resulting class instance.

Parameters:
  • cls (class) – The class to be used in the string representation.

  • attr (str) – The name of the attribute of class cls.

  • args_to_cls (tuple, list, or any object; optional) – A tuple or list containing positional arguments, or any other type of object if there is only one positional argument, to be used during instantiation of cls

  • kwargs_to_cls (dict, optional) – A dict containing the keyword arguments to be used during instantiation of cls.

  • max_items (int, default: 12) – The maximum number of items to include in a ndarray or Quantity; additional items will be truncated with an ellipsis.

Returns:

Approximation of a command to instantiate cls with args_to_cls as positional arguments and kwargs_to_cls as keyword arguments, and then access the attribute attr.

Return type:

str

Notes

This function will generally provide an exact call string for most common types of simple positional and keyword arguments. When dealing with types that are not accounted for, this function will fall back on repr.

This function assumes aliases of u for astropy.units and np for numpy.

Examples

>>> class SampleClass:
...     def __init__(self, arg1, kwarg1=None):
...         pass
...
...     @property
...     def attribute(self):
...         return 42
>>> args_to_cls = (1, 2)
>>> kwargs_to_cls = {"kwarg1": 2}
>>> attribute_call_string(SampleClass, "attribute", args_to_cls, kwargs_to_cls)
'SampleClass(1, 2, kwarg1=2).attribute'