method_repr_string

odl.util.utility.method_repr_string(inst_str, meth_str, arg_strs=None, allow_mixed_seps=True)[source]

Return a repr string for a method that respects line width.

This function is useful to generate a repr string for a derived class that is created through a method, for instance

functional.translated(x)

as a better way of representing

FunctionalTranslation(functional, x)
Parameters
inst_strstr

Stringification of a class instance.

meth_strstr

Name of the method (not including the '.').

arg_strssequence of str, optional

Stringification of the arguments to the method.

allow_mixed_sepsbool, optional

If False and the argument strings do not fit on one line, use ',\n' to separate all strings. By default, a mixture of ', ' and ',\n' is used to fit as much on one line as possible.

In case some of the arg_strs span multiple lines, it is usually advisable to set allow_mixed_seps to False since the result tends to be more readable that way.

Returns
meth_repr_strstr

Concatenation of all strings in a way that the line width is respected.

Examples

>>> inst_str = 'MyClass'
>>> meth_str = 'empty'
>>> arg_strs = []
>>> print(method_repr_string(inst_str, meth_str, arg_strs))
MyClass.empty()
>>> inst_str = 'MyClass'
>>> meth_str = 'fromfile'
>>> arg_strs = ["'tmpfile.txt'"]
>>> print(method_repr_string(inst_str, meth_str, arg_strs))
MyClass.fromfile('tmpfile.txt')
>>> inst_str = "MyClass('init string')"
>>> meth_str = 'method'
>>> arg_strs = ['2.0']
>>> print(method_repr_string(inst_str, meth_str, arg_strs))
MyClass('init string').method(2.0)
>>> long_inst_str = (
...     "MyClass('long string that will definitely trigger a line break')"
... )
>>> meth_str = 'method'
>>> long_arg1 = "'long argument string that should come on the next line'"
>>> arg2 = 'param1=1'
>>> arg3 = 'param2=2.0'
>>> arg_strs = [long_arg1, arg2, arg3]
>>> print(method_repr_string(long_inst_str, meth_str, arg_strs))
MyClass(
    'long string that will definitely trigger a line break'
).method(
    'long argument string that should come on the next line',
    param1=1, param2=2.0
)
>>> print(method_repr_string(long_inst_str, meth_str, arg_strs,
...                          allow_mixed_seps=False))
MyClass(
    'long string that will definitely trigger a line break'
).method(
    'long argument string that should come on the next line',
    param1=1,
    param2=2.0
)