jacinle.utils.printing#
Classes
A context manager that redirect the print to a string. |
Functions
|
|
|
Format the input data. |
|
Indent the text by the given level. |
|
Format the key-value pairs. |
|
Print the key-value pairs. |
|
A helper class to convert a "print" function to a "format" function. |
|
Print the file docstring. |
|
Redirect the print to a function. |
|
Create a |
|
Structure format. |
|
Structure print. |
A context manager that suppress the stdout and stderr. |
|
A context manager that suppress the stdout. |
|
A context manager that suppress the stdout. |
|
|
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)#
Functions
- 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.
- indent_text(text, level=1, indent_format=None, tabsize=None)[source]#
Indent the text by the given level.
- 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.
- 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.
- 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: