repr_string

odl.util.utility.repr_string(outer_string, inner_strings, allow_mixed_seps=True)[source]

Return a pretty string for repr.

The returned string is formatted such that it does not extend beyond the line boundary if avoidable. The line width is taken from NumPy's printing options that can be retrieved with numpy.get_printoptions. They can be temporarily overridden using the npy_printoptions context manager. See Examples for details.

Parameters:
outer_stringstr

Name of the class or function that should be printed outside the parentheses.

inner_stringssequence of sequence of str

Stringifications of the positional and optional arguments. This is usually the return value of signature_string_parts.

allow_mixed_sepsbool, optional

If False and the string does 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 inner_strings 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:
repr_stringstr

Full string that can be returned by a class' __repr__ method.

Examples

Things that fit into one line are printed on one line:

>>> outer_string = 'MyClass'
>>> inner_strings = [('1', "'hello'", 'None'),
...                  ("dtype='float32'",)]
>>> print(repr_string(outer_string, inner_strings))
MyClass(1, 'hello', None, dtype='float32')

Otherwise, if a part of inner_strings fits on a line of its own, it is printed on one line, but separated from the other part with a line break:

>>> outer_string = 'MyClass'
>>> inner_strings = [('2.0', "'this_is_a_very_long_argument_string'"),
...                  ("long_opt_arg='another_quite_long_string'",)]
>>> print(repr_string(outer_string, inner_strings))
MyClass(
    2.0, 'this_is_a_very_long_argument_string',
    long_opt_arg='another_quite_long_string'
)

If those parts are themselves too long, they are broken down into several lines:

>>> outer_string = 'MyClass'
>>> inner_strings = [("'this_is_a_very_long_argument_string'",
...                   "'another_very_long_argument_string'"),
...                  ("long_opt_arg='another_quite_long_string'",
...                   "long_opt2_arg='this_wont_fit_on_one_line_either'")]
>>> print(repr_string(outer_string, inner_strings))
MyClass(
    'this_is_a_very_long_argument_string',
    'another_very_long_argument_string',
    long_opt_arg='another_quite_long_string',
    long_opt2_arg='this_wont_fit_on_one_line_either'
)

The usage of mixed separators to optimally use horizontal space can be disabled by setting allow_mixed_seps=False:

>>> outer_string = 'MyClass'
>>> inner_strings = [('2.0', "'this_is_a_very_long_argument_string'"),
...                  ("long_opt_arg='another_quite_long_string'",)]
>>> print(repr_string(outer_string, inner_strings, allow_mixed_seps=False))
MyClass(
    2.0,
    'this_is_a_very_long_argument_string',
    long_opt_arg='another_quite_long_string'
)

With the npy_printoptions context manager, the available line width can be changed:

>>> outer_string = 'MyClass'
>>> inner_strings = [('1', "'hello'", 'None'),
...                  ("dtype='float32'",)]
>>> with npy_printoptions(linewidth=20):
...     print(repr_string(outer_string, inner_strings))
MyClass(
    1, 'hello',
    None,
    dtype='float32'
)