method_call_string
- plasmapy.utils.code_repr.method_call_string(
- cls,
- method: str,
- *,
- args_to_cls: Any | None = None,
- kwargs_to_cls: dict[str, Any] | None = None,
- args_to_method: Any | None = None,
- kwargs_to_method: dict[str, Any] | None = None,
- max_items: int = 12,
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 representationmethod (
str
) – The name of the method in classcls
args_to_cls (
tuple
,list
, or anyobject
; keyword-only, optional) – Atuple
orlist
containing positional arguments, or any other type ofobject
if there is only one positional argument, to be used during instantiation ofcls
.kwargs_to_cls (
dict
, keyword-only optional) – Adict
containing the keyword arguments to be used during instantiation ofcls
.args_to_method (
tuple
,list
, or anyobject
; keyword-only, optional) – Atuple
orlist
containing the positional arguments to be used in the method call, or any otherobject
if there is only one positional argument.kwargs_to_method (
dict
, keyword-only, optional) – Adict
containing the keyword arguments to be used during the method call.max_items (int, keyword-only, default: 12) – The maximum number of items to include in a
ndarray
orQuantity
; additional items will be truncated with an ellipsis.
- Returns:
Approximation of a command to instantiate
cls
withargs_to_cls
as positional arguments andkwargs_to_cls
as keyword arguments, and then call a method of the resulting instance withargs_to_method
as positional arguments andkwargs_to_method
as keyword arguments.- Return type:
See also
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
forastropy.units
andnp
fornumpy
.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)'