Utils
- class todd.utils.BaseHandler[source]
-
Utility class for working with collections.
- abstractmethod classmethod map(f, *objs)[source]
Apply a function to the given objects.
All the objects must be of the same shape.
- Parameters:
f (CallableProtocol[Any, Any])
objs (T)
- Return type:
T
- class todd.utils.CallableProtocol[source]
Bases:
Protocol[T_contra,T_co]- __init__(*args, **kwargs)
- class todd.utils.EnvRegistry[source]
Bases:
Registry- data = {'cuda_home': <function cuda_home>, 'git_commit_id': <function git_commit_id>, 'git_status': <function git_status>, 'nvidia_smi': <function nvidia_smi>, 'opencv_version': <function opencv_version>, 'platform': <function platform>, 'python_version': <function python_version>, 'pytorch_version': <function pytorch_version>, 'todd_version': <function todd_version>, 'torchvision_version': <function torchvision_version>}
- class todd.utils.HandlerRegistry[source]
Bases:
Registry- data = {'MappingHandler': <class 'todd.utils.nested_collection_utils.MappingHandler'>, 'SequenceHandler': <class 'todd.utils.nested_collection_utils.SequenceHandler'>, 'SetHandler': <class 'todd.utils.nested_collection_utils.SetHandler'>}
- class todd.utils.Keys[source]
Bases:
NamedTupleKeys(missing, unexpected)
- static __new__(_cls, missing, unexpected)
Create new instance of Keys(missing, unexpected)
- class todd.utils.MappingHandler[source]
Bases:
BaseHandler[Mapping[KT,VT]]- classmethod map(f, *objs)[source]
- Parameters:
f (CallableProtocol[VT, T_co])
objs (Mapping[KT, VT])
- Return type:
dict[KT, T_co]
- class todd.utils.NestedCollectionUtils[source]
Bases:
object- elements(obj)[source]
Get the elements of the given object.
- Parameters:
obj (Any) – The object to get the elements from.
- Returns:
Elements of the given object.
- Return type:
Examples
>>> utils = NestedCollectionUtils() >>> list(utils.elements([])) [] >>> list(utils.elements({1: 'a', 2: 'b'})) ['a', 'b'] >>> list(utils.elements({1, 2, 3})) [1, 2, 3] >>> list(utils.elements(('a', 'b', 'c'))) ['a', 'b', 'c'] >>> utils.elements([[1, 2], [3, [4, 5]]]) [1, 2, 3, 4, 5] >>> utils.elements([1, {2: 'a'}, (3, 4)]) [1, 'a', 3, 4]
- get_handler(*objs)[source]
Find a utility class for all the given object.
- Parameters:
objs (Any) – The objects to check.
- Returns:
The utility class or
Noneif none of the utility classes is applicable.- Return type:
type[BaseHandler[Any]] | None
Examples
>>> utils = NestedCollectionUtils() >>> utils.get_handler([]) <class '...SequenceHandler'> >>> utils.get_handler(tuple()) <class '...SequenceHandler'> >>> utils.get_handler(dict()) <class '...MappingHandler'> >>> utils.get_handler(set()) <class '...SetHandler'> >>> utils.get_handler('') >>> utils.get_handler(123) >>> utils.get_handler(None)
- index(obj, indices)[source]
Index the given object with the given indices.
- Parameters:
- Returns:
The result of indexing the object with the indices.
- Return type:
Examples
>>> utils = NestedCollectionUtils() >>> utils.index([1, 2, 3], 0) 1 >>> utils.index({1: 'a', 2: 'b', 3: 'c'}, 1) 'a' >>> utils.index([[1, 2], [3, 4], [5, 6]], [0, 1]) 2
- is_atomic_collection(obj)[source]
Check if the given object is atomic.
An object is considered atomic if all its elements are not collections.
- Parameters:
obj (Any) – The object to check.
- Returns:
- Return type:
Examples
>>> utils = NestedCollectionUtils() >>> utils.is_atomic_collection([]) True >>> utils.is_atomic_collection({1: 'a', 2: 'b'}) True >>> utils.is_atomic_collection({1, 2, 3}) True >>> utils.is_atomic_collection(('a', 'b', 'c')) True >>> utils.is_atomic_collection([1, [2, 3], [4, [5, 6]]]) False >>> utils.is_atomic_collection({1: [2, 3], 4: [5, 6]}) False
- map(f, *objs)[source]
Recursively apply a function to the given objects.
- Parameters:
f (CallableProtocol[Any, Any]) – The function to apply.
objs (Any) – The objects to apply the function to.
- Returns:
The result of applying the function to the objects.
- Return type:
Examples
>>> utils = NestedCollectionUtils() >>> utils.map(lambda x: x + 1, [1, 2, 3]) (2, 3, 4) >>> result = utils.map( ... lambda x: x.upper(), ... {'a': 'apple', 'b': 'banana'}, ... ) >>> dict(sorted(result.items())) {'a': 'APPLE', 'b': 'BANANA'} >>> utils.map(lambda x: x * 2, set([1, 2, 3])) {2, 4, 6} >>> utils.map(lambda x: x.lower(), ('HELLO', 'WORLD')) ('hello', 'world')
- reduce(f, obj)[source]
Apply a function to the collection and returns a single value.
- Parameters:
- Returns:
The result of applying the function to the collection.
- Return type:
Examples
>>> utils = NestedCollectionUtils() >>> utils.reduce(sum, []) 0 >>> utils.reduce(sum, [1]) 1 >>> utils.reduce(sum, [1, 2, 3, 4]) 10 >>> utils.reduce(sum, [[1, 2], [3, 4], [5, 6]]) 21
- class todd.utils.NestedTensorCollectionUtils[source]
Bases:
NestedCollectionUtils
- class todd.utils.SequenceHandler[source]
Bases:
BaseHandler[Sequence[T]]- classmethod map(f, *objs)[source]
- Parameters:
f (CallableProtocol[T, T_co])
objs (Sequence[T])
- Return type:
tuple[T_co, …]
- class todd.utils.SetHandler[source]
Bases:
BaseHandler[Set[T]]- classmethod map(f, *objs)[source]
- Parameters:
f (CallableProtocol[T, T_co])
objs (Set[T])
- Return type:
set[T_co]
- class todd.utils.StateDictConverter[source]
Bases:
object- __init__(*args, module=None, **kwargs)[source]
- Parameters:
module (Module | None)
- Return type:
None
- class todd.utils.StateDictMixin[source]
Bases:
object
- class todd.utils.StateMachine[source]
Bases:
Generic[T]A class to represent a state that can be transited to another state.
Examples
>>> class Enum(enum.Enum): ... A = 1 ... B = 2 ... C = 3 >>> state = StateMachine(Enum.A) >>> state.state <Enum.A: 1> >>> state.transit({Enum.A: Enum.B}) >>> state.state <Enum.B: 2> >>> state.transit({Enum.A: Enum.C}) Traceback (most recent call last): ... RuntimeError: Enum.B is not in {<Enum.A: 1>: <Enum.C: 3>}.
- property state: T
- class todd.utils.StoreMeta[source]
Bases:
NonInstantiableMetaStores for global variables.
Stores provide an interface to access global variables:
>>> class CustomStore(metaclass=StoreMeta): ... VARIABLE: int >>> CustomStore.VARIABLE 0 >>> CustomStore.VARIABLE = 1 >>> CustomStore.VARIABLE 1
Variables can have explicit default values:
>>> class DefaultStore(metaclass=StoreMeta): ... DEFAULT: float = 0.625 >>> DefaultStore.DEFAULT 0.625
Non-empty environment variables are read-only. For string variables, their values are read directly from the environment. Other environment variables are evaluated and should be of the corresponding type. Default values are ignored.
>>> os.environ['ENV_INT'] = '2' >>> os.environ['ENV_STR'] = 'hello world!' >>> os.environ['ENV_DICT'] = 'dict(a=1)' >>> class EnvStore(metaclass=StoreMeta): ... ENV_INT: int = 1 ... ENV_STR: str ... ENV_DICT: dict >>> EnvStore.ENV_INT 2 >>> EnvStore.ENV_STR 'hello world!' >>> EnvStore.ENV_DICT {'a': 1}
Assignments to those variables will not trigger exceptions, but will not take effect:
>>> EnvStore.ENV_INT = 3 >>> EnvStore.ENV_INT 2
- todd.utils.transfer_state_dict(target, source)[source]
- Parameters:
target (Module | StateDictMixin)
source (Module | StateDictMixin)
- Return type:
None