jacinle.utils package¶
Submodules¶
jacinle.utils.argument module¶
-
jacinle.utils.argument.get_2dshape(x: Union[int, Sequence[int], None], default: Tuple[int, int] = None, type: type = <class 'int'>) → Tuple[int, int][source]¶ Convert a value or a tuple to a tuple of length 2.
Parameters: - x – a value of type type, or a tuple of length 2. If the input is a single value, it will be duplicated to a tuple of length 2.
- default – default value.
- type – expected type of the element.
Returns: a tuple of length 2.
-
jacinle.utils.argument.get_3dshape(x: Union[int, Sequence[int], None], default: Tuple[int, int, int] = None, type: type = <class 'int'>) → Tuple[int, int, int][source]¶ Convert a value or a tuple to a tuple of length 3.
Parameters: - x – a value of type type, or a tuple of length 3. If the input is a single value, it will be duplicated to a tuple of length 3.
- default – default value.
- type – expected type of the element.
Returns: a tuple of length 3.
-
jacinle.utils.argument.get_4dshape(x: Union[int, Sequence[int], None], default: Tuple[int, int, int, int] = None, type: type = <class 'int'>) → Tuple[int, int, int, int][source]¶ Convert a value or a tuple to a tuple of length 4.
Parameters: - x – a value of type type, or a tuple of length 4. If there is only one value, it will return (1, x, x, 1). If there are two values, it will return (1, x[0], x[1], 1).
- default – default value.
- type – expected type of the element.
Returns: a tuple of length 4.
-
jacinle.utils.argument.astuple(arr_like: Any) → Tuple[source]¶ Convert a sequence or a single value to a tuple. This method differ from the system method tuple in that a single value (incl. int, string, bytes) will be converted to a tuple of length 1.
Parameters: arr_like – a sequence or a single value. Returns: a tuple.
-
jacinle.utils.argument.asshape(arr_like: Union[int, Sequence[int], None]) → Optional[Tuple[int, ...]][source]¶ Convert a sequence or a single value to a tuple of integers. It will return None if the input is None.
Parameters: arr_like – a sequence or a single value. Returns: a tuple of integers.
-
jacinle.utils.argument.canonize_args_list(args: Tuple[Any], *, allow_empty: bool = False, cvt: Optional[Callable[[Any], Any]] = None) → Tuple[Any][source]¶ Convert the argument list to a tuple of values. This is useful to make unified interface for shape-related operations.
Example
def foo(*args): args = canonize_args_list(args, allow_empty=True) print(args) foo(1, 2, 3) # (1, 2, 3) foo((1, 2, 3)) # (1, 2, 3) foo(1) # (1,) foo() # ()
Parameters: - args – the argument list.
- allow_empty – whether to allow empty argument list.
- cvt – a function to be applied to each element.
jacinle.utils.cache module¶
-
class
jacinle.utils.cache.cached_property(fget)[source]¶ Bases:
objectA decorator that converts a function into a cached property. Similar to
@property, but the function result is cached.
-
jacinle.utils.cache.cached_result(func)[source]¶ A decorator that caches the result of a function. Note that this decorator does not support any arguments to the function.
-
jacinle.utils.cache.fs_cached_result(filename: str, force_update: bool = False, verbose: bool = False) → Callable[[Callable], Callable][source]¶ A decorator that caches the result of a function into a file. Note that this decorator does not take care of any arguments to the function.
Parameters: - filename – the filename to store the result.
- force_update – if True, the result will be updated even if the file exists.
- verbose – if True, the filenames will be printed to the console.
Returns: a decorator function.
jacinle.utils.container module¶
-
class
jacinle.utils.container.G[source]¶ Bases:
dictA simple container that wraps a dict and provides attribute access to the dict.
Example
>>> g = G() >>> g.a = 1 >>> g['b'] = 2
-
format(sep=': ', end='\n')[source]¶ Format the dict as a string using
jacinle.utils.printing.kvformat().
-
print(sep=': ', end='\n', file=None)[source]¶ Print the dict using
jacinle.utils.printing.kvprint().
-
-
jacinle.utils.container.g= {}¶ A simple global dict-like object.
-
class
jacinle.utils.container.GView(dict_=None)[source]¶ Bases:
objectA simple container that wraps a dict and provides attribute access to the dict. In contrast to
G, this class wraps around an existing dict.-
format(sep=': ', end='\n')[source]¶ Format the dict as a string using
jacinle.utils.printing.kvformat().
-
print(sep=': ', end='\n', file=None)[source]¶ Print the dict using
jacinle.utils.printing.kvprint().
-
-
class
jacinle.utils.container.SlotAttrObject(**kwargs)[source]¶ Bases:
objectCreate a object that allows only a fixed set of attributes to be set.
jacinle.utils.context module¶
-
class
jacinle.utils.context.EmptyContext[source]¶ Bases:
objectAn empty context manager that does nothing.
jacinle.utils.debug module¶
-
jacinle.utils.debug.unhook_exception_ipdb()[source]¶ Remove the hook to ipdb when an exception is raised.
-
jacinle.utils.debug.exception_hook(enable: bool = True)[source]¶ A context manager to temporarily enable the ipdb exception hook.
-
jacinle.utils.debug.decorate_exception_hook(func: Callable) → Callable[source]¶ A decorator to decorate the exception hook when calling a function.
-
jacinle.utils.debug.timeout_ipdb(locals_, timeout: float = 3)[source]¶ A context manager that enters ipdb when timeout. This is useful when you want to debug a function that is stuck in a loop.
Example
with timeout_ipdb(locals(), timeout=3): while True: pass
Parameters: - locals – the locals() of the function.
- timeout – the timeout in seconds.
-
jacinle.utils.debug.log_function(init_function: Optional[Callable] = None, *, verbose: bool = True)[source]¶ A decorator to log the function call. This is useful to debug the function call stack. The call stack will be formated with indentations.
Parameters: - init_function – the function to be wrapped. If not specified, this function will return a decorator.
- verbose – whether to print detailed log. By default, only the function name is printed.
Example
@log_function def foo(): bar() @log_function def bar(): pass foo()
- Output:
Entering: foo Entering: bar Exiting: bar Return: None Exiting: foo Return: None
-
jacinle.utils.debug.profile(field='tottime', top_k=10)[source]¶ A context manager to profile the code in the context. After profiling is done, the top k functions will be printed.
Parameters: - field – the field to sort the profile result. See cProfile.Profile.print_stats for more details.
- top_k – the number of top functions to print.
Example
with profile(): foo()
jacinle.utils.defaults module¶
-
class
jacinle.utils.defaults.FileOptions(__file__, **init_kwargs)[source]¶ Bases:
objectA class that stores options in a single file.
Example
# file: my_module.py options = FileOptions(__file__, number_to_add=1) def my_func(x: int) -> int: return x + options.number_to_add # file: my_script.py import my_module my_module.options.set(number_to_add=2) my_module.my_func(1) # returns 3
-
jacinle.utils.defaults.ARGDEF= <object object>¶ A special value to indicate that the default value of an argument will be determined in a deferred manner. See
default_args().
-
jacinle.utils.defaults.default_args(func)[source]¶ A helper function handles the case of “fall-through” default arguments. Suppose we have two functions:
fandg, andfcallsg.ghas a default argumentx, e.g.,x=1. In many cases, we do not want to specify the default value ofxinf. One way to do this is to useNoneas the default value ofxinf, and then check ifxisNoneing. However this does not handle cases wherexcan beNonein other cases. It also requires additional checks ing. With this decorator, we can simply writex=ARGDEFinf, and thenxwill be set to1ing.Example
def f(x=ARGDEF): g(x) @default_args def g(x=1): print(x) f() # prints 1 f(2) # prints 2
jacinle.utils.deprecated module¶
jacinle.utils.enum module¶
jacinle.utils.env module¶
-
jacinle.utils.env.jac_getenv(name: str, default: Any = None, type: Union[str, type, None] = None, prefix: str = None) → Any[source]¶ Get the environment variable with the given name.
Parameters: - name – the name of the environment variable.
- default – the default value if the environment variable is not set.
- type – the type of the environment variable. If not given, the type of the default value will be used.
It supports a special type, denoted by a string
'bool', indicating that the value will be converted to a boolean value (‘true’/’false’, ‘yes’/’no’, ‘1’/’0’). - prefix – the prefix of the environment variable. If not given, the prefix will be
JAC_.
Returns: the value of the environment variable.
jacinle.utils.exception module¶
jacinle.utils.filelock module¶
A platform independent file lock that supports the with-statement.
Original License:
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
-
exception
jacinle.utils.filelock.FileLockTimeout(lock_file)[source]¶ Bases:
TimeoutErrorRaised when the lock could not be acquired in timeout seconds.
-
lock_file= None¶ The path of the file lock.
-
-
class
jacinle.utils.filelock.BaseFileLock(lock_file, timeout=-1)[source]¶ Bases:
objectImplements the base class of a file lock.
-
acquire(timeout=None, poll_intervall=0.05)[source]¶ Acquires the file lock or fails with a
Timeouterror.Parameters: - timeout (float) – the maximum time waited for the file lock.
If
timeout <= 0, there is no timeout and this method will block until the lock could be acquired. Iftimeoutis None, the defaulttimeoutis used. - poll_intervall (float) – we check once in poll_intervall seconds if we can acquire the file lock.
# You can use this method in the context manager (recommended) with lock.acquire(): pass # Or you use an equal try-finally construct: lock.acquire() try: pass finally: lock.release()
- timeout (float) – the maximum time waited for the file lock.
If
-
is_locked¶ True, if the object holds the file lock.
-
lock_file¶ The path to the lock file.
-
release(force=False)[source]¶ Releases the file lock.
Please note, that the lock is only completly released, if the lock counter is 0.
Also note, that the lock file itself is not automatically deleted.
Parameters: force (bool) – If true, the lock counter is ignored and the lock is released in every case.
-
timeout¶ You can set a default timeout for the filelock. It will be used as fallback value in the acquire method, if no timeout value (None) is given.
If you want to disable the timeout, set it to a negative value.
A timeout of 0 means, that there is exactly one attempt to acquire the file lock.
-
-
class
jacinle.utils.filelock.WindowsFileLock(lock_file, timeout=-1)[source]¶ Bases:
jacinle.utils.filelock.BaseFileLockUses the
msvcrt.locking()function to hard lock the lock file on windows systems.
-
class
jacinle.utils.filelock.UnixFileLock(lock_file, timeout=-1)[source]¶ Bases:
jacinle.utils.filelock.BaseFileLockUses the
fcntl.flock()to hard lock the lock file on unix systems.
-
class
jacinle.utils.filelock.SoftFileLock(lock_file, timeout=-1)[source]¶ Bases:
jacinle.utils.filelock.BaseFileLockSimply watches the existence of the lock file.
-
jacinle.utils.filelock.FileLock¶ Alias for the lock, which should be used for the current platform. On Windows, this is an alias for
WindowsFileLock, on Unix forUnixFileLockand otherwise forSoftFileLock.alias of
jacinle.utils.filelock.UnixFileLock
jacinle.utils.imp module¶
-
jacinle.utils.imp.load_module(module_name: str) → Any[source]¶ Import a module by its module name (e.g., jacinle.utils.imp).
Parameters: module_name – the name of the module. Returns: the imported module.
-
jacinle.utils.imp.load_module_filename(module_filename: str) → Any[source]¶ Import a module by its filename (e.g., /Users/jiayuan/Projects/Jacinle/jacinle/utils/imp.py).
Parameters: module_filename – the filename of the module. Returns: the imported module.
-
jacinle.utils.imp.load_source(filename: str, name: Optional[str] = None) → Any[source]¶ Load a source file as a module.
Parameters: - filename – the filename of the source file.
- name – the name of the module.
Returns: the loaded module.
-
jacinle.utils.imp.tuple_to_classname(t: Tuple[str, str]) → str[source]¶ Convert a module-class tuple to a string.
Parameters: t – the module-class tuple. E.g., (‘jacinle.utils.imp’, ‘load_module’). Returns: the string representation of the module-class tuple. E.g., ‘jacinle.utils.imp.load_module’.
-
jacinle.utils.imp.classname_to_tuple(classname: str) → Tuple[str, str][source]¶ Convert a string to a module-class tuple.
Parameters: classname – the string representation of the module-class tuple. E.g., ‘jacinle.utils.imp.load_module’. Returns: the module-class tuple. E.g., (‘jacinle.utils.imp’, ‘load_module’).
-
jacinle.utils.imp.load_class(classname: Union[str, Tuple[str, str]], exit_on_error: bool = True)[source]¶ Load a class by its classname (including module).
Parameters: - classname – the classname of the class.
- exit_on_error – whether to exit the program when the class cannot be loaded.
Returns: the loaded class.
jacinle.utils.init module¶
jacinle.utils.inspect module¶
-
jacinle.utils.inspect.class_name(instance_or_class: Any) → str[source]¶ Get the class name of an instance or a class object.
Parameters: instance_or_class – an instance or a class object. Returns: the class name of the instance or the class object.
-
jacinle.utils.inspect.func_name(func: Callable) → str[source]¶ Get a full name of a function, including the module name.
Parameters: func – a function. Returns: the full name of the function.
-
jacinle.utils.inspect.method_name(method: Callable) → str[source]¶ Get a full name of a method, including the module name and the class name.
jacinle.utils.matching module¶
Functions to match names using glob patterns.
-
class
jacinle.utils.matching.NameMatcher(rules: Union[Iterable[Tuple[str, Any]], Dict[str, Any], None] = None)[source]¶ Bases:
objectA name matcher based on a set of glob patterns.
The rule set is a list of (pattern, value) pairs. The pattern is a glob pattern, and the value is the value to be returned when the pattern matches.
Example
matcher = NameMatcher({'*.jpg': 'image', '*.png': 'image', '*.txt': 'text'}) with matcher: matcher.match('a.jpg') # 'image' matcher.match('a.png') # 'image' matched, unused = matcher.get_last_stat() # Return a tuple of (matched values, unmatched patterns). print(matched) # [('a.jpg', '*.jpg', 'image'), ('a.png', '*.png', 'image')] print(unused) # {'*.txt'}
-
append_rule(rule: Tuple[str, Any])[source]¶ Append a rule to the rule set. The rule is a (pattern, value) pair.
Parameters: rule – the rule to be appended.
-
end()[source]¶ End a matching session, which returns a tuple of (matched values, unmatched patterns). See the docstring of
NameMatcherfor more details.
-
insert_rule(index: int, rule: Tuple[str, Any])[source]¶ Insert a rule to the rule set at a given position (priority). The rule is a (pattern, value) pair.
-
match(k: str) → Optional[Any][source]¶ Match a name against the rule set. Return the value if matched, otherwise return None.
Parameters: k – the name to be matched. Returns: The value if matched, otherwise None.
-
pop_rule(index=None)[source]¶ Pop a rule from the rule set.
Parameters: index – the index of the rule to be popped. If None, the last rule will be popped.
-
rules¶ Get the rules.
-
-
class
jacinle.utils.matching.IENameMatcher(include: Optional[Iterable[str]], exclude: Optional[Iterable[str]])[source]¶ Bases:
objectA name matcher based on two sets of glob patterns: one for inclusion and one for exclusion.
- When
includeis None,excludeis not None, the matcher will match all names that are not excluded. - When
includeis not None,excludeis None, the matcher will match all names that are included. - When
includeis not None,excludeis not None, the matcher will match all names that are included and not excluded. - The
excluderule set has higher priority than theincluderule set.
- When
Example
matcher = IENameMatcher(include=['*.jpg', '*.png'], exclude=['*.bak.png']) with matcher: matcher.match('a.jpg') # True matcher.match('a.png') # True matcher.match('a.bak.png') # False matcher.match('a.txt') # False matcher.match('a.bak.txt') # False stat_type, things = matcher.get_last_stat() print(stat_type) # 'exclude' # Everything that has been rejected. print(things) # ['a.bak.png', 'a.txt', 'a.bak.txt']
-
end()[source]¶ End a matching session, which returns a tuple of
(stat_type, things)See the docstring ofIENameMatcher.
- When
jacinle.utils.meta module¶
-
jacinle.utils.meta.gofor(v: Iterable[Any]) → Iterable[Tuple[Any, Any]][source]¶ A go-style for loop for dict, list, tuple, set, etc.
- dict: for key, value in gofor(dict):
- list, tuple, set: for index, value in gofor(list):
Parameters: v – the iterable object.
-
jacinle.utils.meta.try_run(lambda_)[source]¶ A function that tries to run a function, and returns None if it fails (without raising exceptions).
-
jacinle.utils.meta.map_exec(func, *iterables) → List[Any][source]¶ Execute a function on each element of the iterables, and return the results.
-
jacinle.utils.meta.filter_exec(func, iterable: Iterable[Any]) → List[Any][source]¶ Execute a filter function on each element of the iterable, and return the results.
-
jacinle.utils.meta.first(iterable: Iterable[Any], default: Any = None) → Any[source]¶ Get the first element of an iterable. If the iterable is empty, return the default value.
Parameters: - iterable – the iterable object.
- default – the default value.
Returns: the first element of the iterable, or the default value.
-
jacinle.utils.meta.first_n(iterable: Iterable[T_co], n: int = 10) → Optional[List[Any]][source]¶ Get the first n elements of an iterable. If the iterable has less than n elements, return None.
Parameters: - iterable – the iterable object.
- n – the number of elements to get.
Returns: the first n elements of the iterable, or None.
-
jacinle.utils.meta.stmap(func, iterable: Iterable[Any]) → Iterable[Any][source]¶ A map function that recursively follows the structure of the iterable.
- list, tuple: return
[func(v) for v in iterable] - set: return
{func(v) for v in iterable} - dict: return
{k: func(v) for k, v in iterable.items()}
Parameters: - func – the function to be applied.
- iterable – the iterable object.
Returns: the mapped iterable.
- list, tuple: return
-
jacinle.utils.meta.method2func(method_name: str) → Callable[source]¶ Convert a method name to a function that calls the method. Equivalent to
lambda x: x.method_name().Parameters: method_name – the method name.
-
jacinle.utils.meta.map_exec_method(method_name, iterable: Iterable[Any]) → List[Any][source]¶ Execute a method on each element of the iterable, and return the results.
Parameters: - method_name – the method name.
- iterable – the iterable object.
-
jacinle.utils.meta.decorator_with_optional_args(func=None, *, is_method=False)[source]¶ Make a decorator that can be used with or without arguments.
Parameters: - func – the function to be decorated.
- is_method – whether the function is a method.
Example
@decorator_with_optional_args def my_decorator(func=None, *, a=1, b=2): def wrapper(func): @functools.wraps(func) def new_func(*args, **kwargs): print(f'Calling {func.__name__} with a={a}, b={b}') return func(*args, **kwargs) return new_func return wrapper @my_decorator def func1(): pass # Calling func1 with a=1, b=2 @my_decorator(a=2) def func2(): pass # Calling func2 with a=2, b=2
-
jacinle.utils.meta.cond_with(with_statement, cond: bool)[source]¶ A context manager that runs a with statement only if the condition is true.
-
jacinle.utils.meta.cond_with_group(cond: bool, *with_statement)[source]¶ A context manager that runs a group of with statements only if the condition is true.
-
jacinle.utils.meta.merge_iterable(v1, v2)[source]¶ Merge two iterables into a single iterable.
- list, tuple: return
v1 + v2 - set: return
v1 | v2 - dict: return
{**v1, **v2}
Parameters: - v1 – the first iterable.
- v2 – the second iterable.
Returns: the merged iterable.
- list, tuple: return
-
jacinle.utils.meta.dict_deep_update(a: Dict[Any, Any], b: Dict[Any, Any])[source]¶ Update a dictionary recursively.
Parameters: - a – the dictionary to be updated.
- b – the dictionary to update from.
-
jacinle.utils.meta.dict_deep_kv(d: Dict[Any, Any], sort: bool = True, sep='.', allow_dict: bool = False) → Dict[str, Any][source]¶ Get a flattened dictionary with keys as the path to the value.
Example
>>> d = {'a': 1, 'b': {'c': 2, 'd': 3}} >>> dict_deep_kv(d) {'a': 1, 'b.c': 2, 'b.d': 3}
Parameters: - d – the dictionary.
- sort – whether to sort the keys.
- sep – the separator between keys.
- allow_dict – whether to allow dictionary values.
-
jacinle.utils.meta.dict_deep_keys(d: Dict[Any, Any], sort: bool = True, sep='.', allow_dict: bool = False) → List[str][source]¶ Get the keys of a flattened dictionary.
Parameters: - d – the dictionary.
- sort – whether to sort the keys.
- sep – the separator between keys.
- allow_dict – whether to allow dictionary values.
Returns: a list of keys.
See also
-
jacinle.utils.meta.assert_instance(ins: Any, clz: Union[type, Tuple[type, ...]], msg: str = None)[source]¶ Assert that an instance is of a certain type.
-
jacinle.utils.meta.assert_notnone(ins: Any, msg: str = None, name: str = None)[source]¶ Assert that the input is not None.
Parameters: - ins – the input.
- msg – the error message. If not specified, a default message
{name} is Nonewill be used. - name – the name of the input.
-
class
jacinle.utils.meta.notnone_property(fget)[source]¶ Bases:
objectA property that raises an error if the value is None.
-
jacinle.utils.meta.synchronized(mutex=None)[source]¶ A decorator that synchronizes the execution of a function.
Parameters: mutex – the mutex to use. If not specified, a new threading mutex will be created.
-
jacinle.utils.meta.timeout(timeout: float, fps: Optional[float] = None)[source]¶ A decorator that raises a TimeoutError if the execution time of the function exceeds the timeout.
Parameters: Example
import time from jacinle.utils.meta import timeout for _ in timeout(5.1): print('hello') time.sleep(1)
-
class
jacinle.utils.meta.Clock(tick: Optional[float] = None)[source]¶ Bases:
objectA clock that can be used to measure the time.
jacinle.utils.meter module¶
-
class
jacinle.utils.meter.AverageMeter[source]¶ Bases:
objectComputes and stores the average and current value
-
avg= 0¶
-
count= 0¶
-
sum= 0¶
-
tot_count= 0¶
-
val= 0¶
-
jacinle.utils.naming module¶
jacinle.utils.network module¶
-
jacinle.utils.network.get_local_addr() → str[source]¶ Get the local IP address of the machine.
Returns: the local IP address.
jacinle.utils.numeric module¶
-
jacinle.utils.numeric.safe_sum(*values)[source]¶ A safe sum function that uses the first value as the initial value. It can be used as a replacement of
sum().
-
jacinle.utils.numeric.mean(values, default=0)[source]¶ A mean function that returns the default value when the input is empty.
-
jacinle.utils.numeric.std(values, default=0)[source]¶ A standard deviation function that returns the default value when the input is empty.
-
jacinle.utils.numeric.rms(values, default=0)[source]¶ A root mean square function that returns the default value when the input is empty.
jacinle.utils.printing module¶
-
jacinle.utils.printing.indent_text(text: str, level=1, indent_format: Optional[str] = None, tabsize: Optional[int] = None) → str[source]¶ Indent the text by the given level.
Parameters: - text – the text to be indented.
- level – the indent level.
- indent_format – the indent format. If None, use the tabsize.
- tabsize – the tab size. If None, use the default tab size (2).
Returns: The indented text.
-
jacinle.utils.printing.stprint(data, key: Optional[str] = None, indent: int = 0, file: Optional[io.TextIOBase] = None, indent_format: str = ' ', end_format: str = '\n', float_format: str = '{:.6f}', need_lock: bool = True, sort_key: bool = True, max_depth: int = 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 – for recursion calls. Do not use it if you don’t know how it works.
- indent – indent level.
- file – the file to print to.
- indent_format – the indent format.
- end_format – the end format.
- float_format – the float format.
- need_lock – whether to use the lock.
- sort_key – whether to sort the keys.
- max_depth – the maximum depth of the recursion.
-
jacinle.utils.printing.stformat(data, key=None, indent=0, max_depth=100, **kwargs)[source]¶ Structure format. See
stprint()for more details.
-
jacinle.utils.printing.kvprint(data, indent: int = 0, sep: str = ' : ', end: str = '\n', max_key_len: Optional[int] = None, file: Optional[io.TextIOBase] = None, float_format: str = '{:.6f}', need_lock: bool = True)[source]¶ Print the key-value pairs.
Parameters: - data – the data to be printed.
- indent – the indent level.
- sep – the separator between key and value.
- end – the end format.
- max_key_len – the maximum length of the key. If None, use the maximum length of the keys.
- file – the file to print to.
- float_format – the float format.
- need_lock – whether to use the lock.
-
jacinle.utils.printing.kvformat(data, indent=0, sep=' : ', end='\n', max_key_len=None)[source]¶ Format the key-value pairs. See
kvprint()for more details.
-
class
jacinle.utils.printing.PrintToStringContext(target='STDOUT', stream=None, need_lock=True)[source]¶ Bases:
objectA context manager that redirect the print to a string.
Example
>>> with PrintToStringContext() as s: ... print('hello') >>> print(s.get())
-
jacinle.utils.printing.print_to_string(target='STDOUT')[source]¶ Create a
PrintToStringContextand return the context manager.
-
jacinle.utils.printing.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.
jacinle.utils.registry module¶
-
class
jacinle.utils.registry.CallbackRegistry[source]¶ Bases:
jacinle.utils.registry.RegistryA callable manager utils.
If there exists a super callback, it will block all callbacks. A super callback will receive the called name as its first argument.
Then the dispatcher will try to call the callback by name. If such name does not exists, a fallback callback will be called.
The fallback callback will also receive the called name as its first argument.
Example
>>> registry = CallbackRegistry() >>> callback_func = print >>> registry.register('name', callback_func) # register a callback. >>> registry.dispatch('name', 'arg1', 'arg2', kwarg1='kwarg1') # dispatch.
-
fallback_callback¶
-
super_callback¶
-
-
class
jacinle.utils.registry.SimpleEventRegistry(allowed_events=None)[source]¶ Bases:
object-
allowed_events¶
-
-
class
jacinle.utils.registry.EventRegistry[source]¶ Bases:
jacinle.utils.registry.Registry-
DEF_PRIORITY= 10¶
-
jacinle.utils.tqdm module¶
-
jacinle.utils.tqdm.get_current_tqdm()[source]¶ Get the current tqdm instance. Only tqdms created by
tqdm()will be returned.
-
jacinle.utils.tqdm.tqdm(iterable, **kwargs)[source]¶ Wrapped tqdm, where default kwargs will be load, and support for i in tqdm(10) usage.
-
jacinle.utils.tqdm.tqdm_gofor(iterable, **kwargs)[source]¶ Create a tqdm progress bar for the given iterable, and use it as the progress bar for
jacinle.utils.meta.gofor().
jacinle.utils.uid module¶
jacinle.utils.value_scheduler module¶
-
class
jacinle.utils.value_scheduler.ValueScheduler[source]¶ Bases:
objectEssentially, define y = f(x), where x is a discrete, bounded variable.
-
class
jacinle.utils.value_scheduler.MonotonicSchedulerBase(begin, begin_value, end, end_value)[source]¶