jacinle.utils.printing#

Classes

PrintToStringContext

A context manager that redirect the print to a string.

Functions

colored(*text[, sep, color])

format_printable_data(data[, float_format, ...])

Format the input data.

indent_text(text[, level, indent_format, ...])

Indent the text by the given level.

kvformat(data[, indent, sep, end, max_key_len])

Format the key-value pairs.

kvprint(data[, indent, sep, end, ...])

Print the key-value pairs.

print2format(print_func)

A helper class to convert a "print" function to a "format" function.

print_filedoc(filename, docstring)

Print the file docstring.

print_to(print_func[, target, rstrip])

Redirect the print to a function.

print_to_string([target])

Create a PrintToStringContext and return the context manager.

stformat(data[, key, indent, max_depth])

Structure format.

stprint(data[, key, indent, file, ...])

Structure print.

suppress_output()

A context manager that suppress the stdout and stderr.

suppress_stderr()

A context manager that suppress the stdout.

suppress_stdout()

A context manager that suppress the stdout.

tabulate_dataclass(dataclass_instance[, ...])

Return a list of (field_name, field_value) pairs for the given dataclass instance.

Class PrintToStringContext

class PrintToStringContext[source]#

Bases: object

A context manager that redirect the print to a string.

Example

>>> with PrintToStringContext() as s:
...     print('hello')
>>> print(s.get())
__init__(target='STDOUT', stream=None, need_lock=True)[source]#

Initialize the context.

Parameters:
  • target – the target to redirect to. Can be ‘STDOUT’, ‘STDERR’.

  • stream – the stream to redirect to. If None, use a new io.StringIO.

  • need_lock – whether to use the lock.

__new__(**kwargs)#
get()[source]#

Get the string.

Return type:

str

Functions

colored(*text, sep=' ', color='unspecified')[source]#
Parameters:
format_printable_data(data, float_format=_DEFAULT_FLOAT_FORMAT, indent=1, indent_format='  ')[source]#

Format the input data. It handles the following types:

  • numpy array: print the shape and dtype.

  • torch tensor: print the shape and dtype.

  • float: print with the given float format.

  • other types: use str() to print.

Parameters:
  • data – the data to be printed.

  • float_format (str) – the float format.

  • indent (int) – the indent level.

  • indent_format (str) – the indent format.

indent_text(text, level=1, indent_format=None, tabsize=None)[source]#

Indent the text by the given level.

Parameters:
  • text (str) – the text to be indented.

  • level – the indent level.

  • indent_format (str | None) – the indent format. If None, use the tabsize.

  • tabsize (int | None) – the tab size. If None, use the default tab size (2).

Returns:

The indented text.

Return type:

str

kvformat(data, indent=0, sep=' : ', end='\n', max_key_len=None)[source]#

Format the key-value pairs. See kvprint() for more details.

kvprint(data, indent=0, sep=' : ', end='\n', max_key_len=None, file=None, float_format=_DEFAULT_FLOAT_FORMAT, need_lock=True)[source]#

Print the key-value pairs.

Parameters:
  • data – the data to be printed.

  • indent (int) – the indent level.

  • sep (str) – the separator between key and value.

  • end (str) – the end format.

  • max_key_len (int | None) – the maximum length of the key. If None, use the maximum length of the keys.

  • file (TextIOBase | None) – the file to print to.

  • float_format (str) – the float format.

  • need_lock (bool) – whether to use the lock.

print2format(print_func)[source]#

A helper class to convert a “print” function to a “format” function.

Parameters:

print_func (Callable)

Return type:

Callable

print_filedoc(filename, docstring)[source]#

Print the file docstring.

Parameters:
  • filename (str) – the filename.

  • docstring (str) – the docstring.

print_to(print_func, target='STDOUT', rstrip=True)[source]#

Redirect the print to a function.

Example

def print_func(s):
    print('print_func: {}'.format(s))

with print_to(print_func):
   print('hello')
Parameters:
  • print_func – the function to redirect to.

  • target – the target to redirect to. Can be ‘STDOUT’, ‘STDERR’.

  • rstrip – whether to remove the trailing newlines.

print_to_string(target='STDOUT')[source]#

Create a PrintToStringContext and return the context manager.

stformat(data, key=None, indent=0, max_depth=100, **kwargs)[source]#

Structure format. See stprint() for more details.

stprint(data, key=None, indent=0, file=None, indent_format='  ', end_format='\n', float_format=_DEFAULT_FLOAT_FORMAT, need_lock=True, sort_key=True, max_depth=100)[source]#

Structure print.

Example

>>> data = dict(a=np.zeros(shape=(10, 10)), b=3)
>>> stprint(data)
dict{
    a: ndarray(10, 10), dtype=float64
    b: 3
}
Parameters:
  • data – data to be print. Currently support Sequnce, Mappings and primitive types.

  • key (str | None) – for recursion calls. Do not use it if you don’t know how it works.

  • indent (int) – indent level.

  • file (TextIOBase | None) – the file to print to.

  • indent_format (str) – the indent format.

  • end_format (str) – the end format.

  • float_format (str) – the float format.

  • need_lock (bool) – whether to use the lock.

  • sort_key (bool) – whether to sort the keys.

  • max_depth (int) – the maximum depth of the recursion.

suppress_output()[source]#

A context manager that suppress the stdout and stderr.

suppress_stderr()[source]#

A context manager that suppress the stdout.

suppress_stdout()[source]#

A context manager that suppress the stdout.

tabulate_dataclass(dataclass_instance, separate_kv=False)[source]#

Return a list of (field_name, field_value) pairs for the given dataclass instance.

Parameters:
  • dataclass_instance – the dataclass instance.

  • separate_kv (bool) – whether to separate the key and value as two lists in the return value.

Returns:

a list of (field_name, field_value) pairs if separate_kv is False, otherwise a tuple of two lists.

Return type:

List[Tuple[str, Any]] | Tuple[List[str], List[Any]]