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 classclsargs_to_cls (
tuple,list, or anyobject; keyword-only, optional) β Atupleorlistcontaining positional arguments, or any other type ofobjectif there is only one positional argument, to be used during instantiation ofcls.kwargs_to_cls (
dict, keyword-only optional) β Adictcontaining the keyword arguments to be used during instantiation ofcls.args_to_method (
tuple,list, or anyobject; keyword-only, optional) β Atupleorlistcontaining the positional arguments to be used in the method call, or any otherobjectif there is only one positional argument.kwargs_to_method (
dict, keyword-only, optional) β Adictcontaining 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
ndarrayorQuantity; additional items will be truncated with an ellipsis.
- Returns:
Approximation of a command to instantiate
clswithargs_to_clsas positional arguments andkwargs_to_clsas keyword arguments, and then call a method of the resulting instance withargs_to_methodas positional arguments andkwargs_to_methodas 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
uforastropy.unitsandnpfornumpy.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)'