method_call_string

plasmapy.utils.code_repr.method_call_string(cls, method: str, *, args_to_cls: Optional[Any] = None, kwargs_to_cls: Optional[Dict[str, Any]] = None, args_to_method: Optional[Any] = None, kwargs_to_method: Optional[Dict[str, Any]] = None, max_items: numbers.Integral = 12)str

Approximate the command to instantiate a class, and then call a method in the resulting class instance.

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

  • method (str) – The name of the method in class cls

  • args_to_cls (tuple, list, or any object; keyword-only, 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, keyword-only optional) – A dict containing the keyword arguments to be used during instantiation of cls.

  • args_to_method (tuple, list, or any object; keyword-only, optional) – A tuple or list containing the positional arguments to be used in the method call, or any other object if there is only one positional argument.

  • kwargs_to_method (dict, keyword-only, optional) – A dict containing the keyword arguments to be used during the method call.

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

Returns

Approximation of a command to instantiate cls with args_to_cls as positional arguments and kwargs_to_cls as keyword arguments, and then call a method of the resulting instance with args_to_method as positional arguments and kwargs_to_method as keyword arguments.

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, cls_arg, cls_kwarg=None):
...         pass
...     def method(self, method_arg, method_kwarg=None):
...         return 42
>>> c_args = (1,)
>>> c_kwargs = {'cls_kwarg': 2}
>>> m_args = 3
>>> m_kwargs = {'method_kwarg': 4}
>>> method_call_string(
...     SampleClass,
...     'method',
...     args_to_cls=c_args,
...     kwargs_to_cls=c_kwargs,
...     args_to_method=m_args,
...     kwargs_to_method=m_kwargs)
'SampleClass(1, cls_kwarg=2).method(3, method_kwarg=4)'