jacinle.utils.meta#
Module attributes
|
A special object to indicate that a value is not set. |
Classes
A clock that can be used to measure the time. |
Functions
|
Assert that an instance is of a certain type. |
|
Assert that the input is None. |
|
Assert that the input is not None. |
|
A context manager that runs a with statement only if the condition is true. |
|
A context manager that runs a group of with statements only if the condition is true. |
|
Make a decorator that can be used with or without arguments. |
|
Get the keys of a flattened dictionary. |
|
Get a flattened dictionary with keys as the path to the value. |
|
Update a dictionary recursively. |
|
Execute a filter function on each element of the iterable, and return the results. |
|
Get the first element of an iterable. |
|
Get the first n elements of an iterable. |
|
A go-style for loop for dict, list, tuple, set, etc. |
|
Make a dummy function that raises an error when called. |
|
Execute a function on each element of the iterables, and return the results. |
|
Execute a method on each element of the iterable, and return the results. |
|
Merge two iterables into a single iterable. |
|
Convert a method name to a function that calls the method. |
|
A property that raises an error if the value is None. |
|
A helper function to generate the repr string from the __str__ method. |
|
A decorator to run a function only once. |
|
A map function that recursively follows the structure of the iterable. |
|
A decorator that synchronizes the execution of a function. |
|
A decorator that raises a TimeoutError if the execution time of the function exceeds the timeout. |
|
A function that tries to run a function, and returns None if it fails (without raising exceptions). |
Class Clock
- class Clock[source]#
Bases:
object
A clock that can be used to measure the time.
- __init__(tick=None)[source]#
Initialize the clock.
- Parameters:
tick (float | None) – the time (second) for each tick of the clock.
- __new__(**kwargs)#
Functions
- cond_with(with_statement, cond)[source]#
A context manager that runs a with statement only if the condition is true.
- Parameters:
cond (bool)
- cond_with_group(cond, *with_statement)[source]#
A context manager that runs a group of with statements only if the condition is true.
- Parameters:
cond (bool)
- 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
- dict_deep_keys(d, sort=True, sep='.', allow_dict=False)[source]#
Get the keys of a flattened dictionary.
- Parameters:
- Returns:
a list of keys.
- Return type:
See also
- dict_deep_kv(d, sort=True, sep='.', allow_dict=False)[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}
- filter_exec(func, iterable)[source]#
Execute a filter function on each element of the iterable, and return the results.
- first(iterable, default=None)[source]#
Get the first element of an iterable. If the iterable is empty, return the default value.
- first_n(iterable, n=10)[source]#
Get the first n elements of an iterable. If the iterable has less than n elements, return None.
- gofor(v)[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):
- map_exec(func, *iterables)[source]#
Execute a function on each element of the iterables, and return the results.
- map_exec_method(method_name, iterable)[source]#
Execute a method on each element of the iterable, and return the results.
- 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.
- method2func(method_name)[source]#
Convert a method name to a function that calls the method. Equivalent to
lambda x: x.method_name()
.
- repr_from_str(self)[source]#
A helper function to generate the repr string from the __str__ method.
Example
class Foo(object): def __str__(self): return 'Foo' __repr__ = repr_from_str print(Foo()) # Foo print(repr(Foo())) # Foo[Foo]
- stmap(func, iterable)[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()}
- 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.