jacinle#

The Jacinle library.

This main library contains a set of useful utility functions and classes for general Python scripting.

There are a few automatically imported submodules that can be accessed by jacinle.<submodule>.

Command Line Tools

JacArgumentParser

A customized argument parser.

yes_or_no(question[, default])

Ask a yes/no question via input() and return their answer.

maybe_mkdir(dirname)

Make a directory if it does not exist.

git_guard([force])

A utility function to guard the current git repo.

Logging

get_logger([name, formatter])

Get logger with given name.

set_logger_output_file(fout[, mode])

set the output file for all loggers.

Configuration

See jacinle.config.environ_v2 for more details.

configs

The global configuration dictionary.

def_configs()

A context manager to enable configuration definition mode.

def_configs_func(func)

A decorator to enable configuration definition mode when calling a function.

set_configs()

A context manager to enable configuration setting mode.

set_configs_func(func)

A decorator to enable configuration setting mode when calling a function.

jac_getenv(name[, default, type, prefix])

Get the environment variable with the given name.

jac_is_verbose([default, prefix])

Return if the verbose mode is enabled.

jac_is_debug([default, prefix])

Return if the debug mode is enabled.

Utilities (Core)

EmptyContext

An empty context manager that does nothing.

KeyboardInterruptContext

A context manager that catches KeyboardInterrupt and does nothing.

JacEnum

A customized enumeration class, adding helper functions for string-based argument parsing.

Clock

A clock that can be used to measure the time.

deprecated(func)

A helper decorator to mark a function as deprecated.

load_module(module_name)

Import a module by its module name (e.g., jacinle.utils.imp).

load_module_filename(module_filename)

Import a module by its filename (e.g., /Users/jiayuan/Projects/Jacinle/jacinle/utils/imp.py).

load_source(filename[, name])

Load a source file as a module.

UNSET

A special object to indicate that a value is not set.

gofor(v)

A go-style for loop for dict, list, tuple, set, etc.

run_once(func)

A decorator to run a function only once.

try_run(lambda_)

A function that tries to run a function, and returns None if it fails (without raising exceptions).

map_exec(func, *iterables)

Execute a function on each element of the iterables, and return the results.

filter_exec(func, iterable)

Execute a filter function on each element of the iterable, and return the results.

first(iterable[, default])

Get the first element of an iterable.

first_n(iterable[, n])

Get the first n elements of an iterable.

stmap(func, iterable)

A map function that recursively follows the structure of the iterable.

method2func(method_name)

Convert a method name to a function that calls the method.

map_exec_method(method_name, iterable)

Execute a method on each element of the iterable, and return the results.

decorator_with_optional_args([func, is_method])

Make a decorator that can be used with or without arguments.

cond_with(with_statement, cond)

A context manager that runs a with statement only if the condition is true.

cond_with_group(cond, *with_statement)

A context manager that runs a group of with statements only if the condition is true.

merge_iterable(v1, v2)

Merge two iterables into a single iterable.

dict_deep_update(a, b)

Update a dictionary recursively.

dict_deep_kv(d[, sort, sep, allow_dict])

Get a flattened dictionary with keys as the path to the value.

dict_deep_keys(d[, sort, sep, allow_dict])

Get the keys of a flattened dictionary.

assert_instance(ins, clz[, msg])

Assert that an instance is of a certain type.

assert_none(ins[, msg])

Assert that the input is None.

assert_notnone(ins[, msg, name])

Assert that the input is not None.

notnone_property(fget)

A property that raises an error if the value is None.

synchronized([mutex])

A decorator that synchronizes the execution of a function.

timeout(timeout[, fps])

A decorator that raises a TimeoutError if the execution time of the function exceeds the timeout.

make_dummy_func([message])

Make a dummy function that raises an error when called.

repr_from_str(self)

A helper function to generate the repr string from the __str__ method.

class_name(instance_or_class)

Get the class name of an instance or a class object.

func_name(func)

Get a full name of a function, including the module name.

method_name(method)

Get a full name of a method, including the module name and the class name.

class_name_of_method(method)

Get the class name of a method.

colored(*text[, sep, color])

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

Indent the text by the given level.

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

Structure print.

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

Structure format.

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

Print the key-value pairs.

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

Format the key-value pairs.

print_to_string([target])

Create a PrintToStringContext and return the context manager.

print_to(print_func[, target, rstrip])

Redirect the print to a function.

suppress_stdout()

A context manager that suppress the stdout.

suppress_stderr()

A context manager that suppress the stdout.

suppress_output()

A context manager that suppress the stdout and stderr.

tabulate_dataclass(dataclass_instance[, ...])

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

print_filedoc(filename, docstring)

Print the file docstring.

Utilities (IO)

load(filename, **kwargs)

Load a file with automatic file type detection.

dump(filename, obj, **kwargs)

Dump a file with automatic file type detection.

load_pkl(fd_or_filename, **kwargs)

Load a pickle file.

dump_pkl(fd_or_filename, obj, **kwargs)

Dump a pickle file.

lsdir(dirname[, pattern, return_type, sort])

List all files in a directory.

mkdir(path)

Create a directory if it does not exist without raising errors when the directory already exists.

Utilities (Cache)

cached_property(fget)

A decorator that converts a function into a cached property.

cached_result(func)

A decorator that caches the result of a function.

fs_cached_result(filename[, force_update, ...])

A decorator that caches the result of a function into a file.

Utilities (TQDM)

get_current_tqdm()

Get the current tqdm instance.

tqdm(iterable, **kwargs)

Wrapped tqdm, where default kwargs will be load, and support for i in tqdm(10) usage.

tqdm_pbar(**kwargs)

Create a tqdm progress bar with the given kwargs.

tqdm_gofor(iterable, **kwargs)

Create a tqdm progress bar for the given iterable, and use it as the progress bar for jacinle.utils.meta.gofor().

tqdm_zip(*iterable, **kwargs)

Create a tqdm progress bar for a zip of the given iterables, and use it as the progress bar.

TQDMPool

Utilities (Math)

GroupMeters

safe_sum(*values)

A safe sum function that uses the first value as the initial value.

mean(values[, default])

A mean function that returns the default value when the input is empty.

std(values[, default])

A standard deviation function that returns the default value when the input is empty.

rms(values[, default])

A root mean square function that returns the default value when the input is empty.

prod(values[, default])

A product function that returns the default value when the input is empty.

divup(n, d)

Divide n by d and round up.

reset_global_seed([seed, verbose])

Reset the global seed for all random number generators.

seed(seed)

with_seed([seed, verbose])

A context manager that sets the global seed to the given value, and restores it after the context.

Utilities (Container)

g

A simple global dict-like object.

G

A simple container that wraps a dict and provides attribute access to the dict.

GView

A simple container that wraps a dict and provides attribute access to the dict.

SlotAttrObject

Create a object that allows only a fixed set of attributes to be set.

OrderedSet

A set that keeps the order of the elements.

Utilities (Defaults)

See jacinle.utils.defaults for more details.

defaults_manager

Defaults manager can be used to create program or thread-level registries.

wrap_custom_as_default

gen_get_default([default_getter])

gen_set_default(cls)

option_context(name[, is_local])

FileOptions

A class that stores options in a single file.

default_args(func)

A helper function handles the case of "fall-through" default arguments.

ARGDEF

A special value to indicate that the default value of an argument will be determined in a deferred manner.

Utilities (Exception and Debugging)

hook_exception_ipdb()

Add a hook to ipdb when an exception is raised.

exception_hook([enable])

A context manager to temporarily enable the ipdb exception hook.

timeout_ipdb(locals_[, timeout])

A context manager that enters ipdb when timeout.

log_function([init_function, verbose, ...])

A decorator to log the function call.

profile([field, top_k])

A context manager to profile the code in the context.

time([name])

A context manager to time the code in the context.

format_exc(ei)

Format an exception info tuple into a string.

Utilities (Network and Misc)

get_local_addr()

Get the local IP address of the machine.

gen_time_string()

Generate a time string with format: %Y%m%d-%H%M%S-%f.

gen_uuid4()

Generate a UUID4 string.

Submodules