utils ¶
Classes:
-
KwargsNotNone–Remove all None objects from this kwargs dict.
-
LinearRangeLut– -
Singleton–Handy class to inherit to have the SingletonMeta metaclass.
-
cachedproperty–Wrapper for a one-time get property, that will be cached.
-
classproperty–A combination of
classmethodandproperty. -
inject_kwargs_params–Descriptor that injects parameters into functions based on an instance's keyword mapping.
-
inject_self–Descriptor that ensures the wrapped function always has a constructed
self.
Functions:
-
complex_hash–Class decorator that automatically adds a
__hash__method to the target class. -
copy_signature–Utility function to copy the signature of one function to another one.
-
get_subclasses–Get all subclasses of a given type.
KwargsNotNone ¶
Bases: KwargsT
Remove all None objects from this kwargs dict.
Source code in jetpytools/types/utils.py
863 864 865 | |
LinearRangeLut ¶
Singleton ¶
Handy class to inherit to have the SingletonMeta metaclass.
SingletonMeta ¶
Bases: type
Methods:
-
__call__–
__call__ ¶
Source code in jetpytools/types/utils.py
889 890 891 892 893 894 895 896 897 898 899 900 901 | |
cachedproperty ¶
cachedproperty(
fget: Callable[[Any], _R_co],
fset: Callable[[Any, _T_Any], None] | None = None,
fdel: Callable[[Any], None] | None = None,
doc: str | None = None,
)
Bases: property, Generic[_R_co, _T_Any]
Wrapper for a one-time get property, that will be cached.
You shouldn't hold a reference to itself or it will never get garbage collected.
Methods:
-
clear_cache–Clear cached properties of an object instance.
-
deleter– -
getter– -
setter– -
update_cache–Update cached property of an object instance.
Attributes:
Source code in jetpytools/types/utils.py
784 785 786 787 788 789 790 791 792 | |
clear_cache classmethod ¶
Clear cached properties of an object instance.
Parameters:
-
(obj¶object) –The object whose cache should be cleared.
-
(names¶str | Iterable[str] | None, default:None) –Specific property names to clear. If None, all cached properties are cleared.
Source code in jetpytools/types/utils.py
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 | |
deleter ¶
deleter(fdel: Callable[..., None]) -> cachedproperty[_R_co, _T_Any]
Source code in jetpytools/types/utils.py
780 | |
getter ¶
getter(fget: Callable[..., _R_co]) -> cachedproperty[_R_co, _T_Any]
Source code in jetpytools/types/utils.py
776 | |
setter ¶
setter(fset: Callable[[Any, _T_Any], None]) -> cachedproperty[_R_co, _T_Any]
Source code in jetpytools/types/utils.py
778 | |
update_cache classmethod ¶
Update cached property of an object instance.
Parameters:
-
(obj¶object) –The object whose cache should be updated.
-
(name¶str) –Property name to update.
-
(value¶Any) –The value to assign.
Source code in jetpytools/types/utils.py
847 848 849 850 851 852 853 854 855 856 857 | |
classproperty ¶
classproperty(
fget: Callable[[type[_T]], _R_co] | classmethod[_T, ..., _R_co],
fset: Callable[Concatenate[type[_T], _T_Any, ...], None]
| classmethod[_T, Concatenate[_T_Any, ...], None]
| None = None,
fdel: Callable[[type[_T]], None]
| classmethod[_T, ..., None]
| None = None,
doc: str | None = None,
)
Bases: classproperty_base[_T, _R_co, _T_Any]
A combination of classmethod and property.
Classes:
-
cached–A combination of
classmethodandproperty.
Attributes:
-
fdel(Callable[[type[_T]], None] | None) – -
fget(Callable[[type[_T]], _R_co]) – -
fset(Callable[Concatenate[type[_T], _T_Any, ...], None] | None) –
Source code in jetpytools/types/utils.py
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
fdel instance-attribute ¶
fdel: Callable[[type[_T]], None] | None = (
fdel.__func__ if isinstance(fdel, classmethod) else fdel
)
fget instance-attribute ¶
fget: Callable[[type[_T]], _R_co] = (
fget.__func__ if isinstance(fget, classmethod) else fget
)
fset instance-attribute ¶
fset: Callable[Concatenate[type[_T], _T_Any, ...], None] | None = (
fset.__func__ if isinstance(fset, classmethod) else fset
)
cached ¶
cached(
fget: Callable[[type[_T]], _R_co] | classmethod[_T, ..., _R_co],
fset: Callable[Concatenate[type[_T], _T_Any, ...], None]
| classmethod[_T, Concatenate[_T_Any, ...], None]
| None = None,
fdel: Callable[[type[_T]], None]
| classmethod[_T, ..., None]
| None = None,
doc: str | None = None,
)
Bases: classproperty_base[_T0, _R0_co, _T0_Any]
A combination of classmethod and property.
The value is computed once and then cached in a dictionary (under cache_key) attached to the class type. If a setter or deleter is defined and invoked, the cache is cleared.
Methods:
-
clear_cache–Clear cached properties of an type instance.
Attributes:
-
cache_key– -
fdel(Callable[[type[_T]], None] | None) – -
fget(Callable[[type[_T]], _R_co]) – -
fset(Callable[Concatenate[type[_T], _T_Any, ...], None] | None) –
Source code in jetpytools/types/utils.py
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
fdel instance-attribute ¶
fdel: Callable[[type[_T]], None] | None = (
fdel.__func__ if isinstance(fdel, classmethod) else fdel
)
fget instance-attribute ¶
fget: Callable[[type[_T]], _R_co] = (
fget.__func__ if isinstance(fget, classmethod) else fget
)
fset instance-attribute ¶
fset: Callable[Concatenate[type[_T], _T_Any, ...], None] | None = (
fset.__func__ if isinstance(fset, classmethod) else fset
)
clear_cache classmethod ¶
Clear cached properties of an type instance.
Parameters:
-
(type_¶type) –The type whose cache should be cleared.
-
(names¶str | Iterable[str] | None, default:None) –Specific property names to clear. If None, all cached properties are cleared.
Source code in jetpytools/types/utils.py
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 | |
classproperty_base ¶
classproperty_base(
fget: Callable[[type[_T]], _R_co] | classmethod[_T, ..., _R_co],
fset: Callable[Concatenate[type[_T], _T_Any, ...], None]
| classmethod[_T, Concatenate[_T_Any, ...], None]
| None = None,
fdel: Callable[[type[_T]], None]
| classmethod[_T, ..., None]
| None = None,
doc: str | None = None,
)
Bases: Generic[_T, _R_co, _T_Any]
Attributes:
-
fdel(Callable[[type[_T]], None] | None) – -
fget(Callable[[type[_T]], _R_co]) – -
fset(Callable[Concatenate[type[_T], _T_Any, ...], None] | None) –
Source code in jetpytools/types/utils.py
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
fdel instance-attribute ¶
fdel: Callable[[type[_T]], None] | None = (
fdel.__func__ if isinstance(fdel, classmethod) else fdel
)
fget instance-attribute ¶
fget: Callable[[type[_T]], _R_co] = (
fget.__func__ if isinstance(fget, classmethod) else fget
)
fset instance-attribute ¶
fset: Callable[Concatenate[type[_T], _T_Any, ...], None] | None = (
fset.__func__ if isinstance(fset, classmethod) else fset
)
inject_kwargs_params ¶
inject_kwargs_params(func: Callable[Concatenate[_T_co, _P], _R_co])
Bases: _InjectKwargsParamsBase[_T_co, _P, _R_co]
Descriptor that injects parameters into functions based on an instance's keyword mapping.
When a method wrapped with @inject_kwargs_params is called, the descriptor inspects the function's signature and replaces any arguments matching keys in self.kwargs (or another mapping defined by _kwargs_name) if their values equal the parameter's default.
Initialize the inject_kwargs_params descriptor.
Parameters:
-
(func¶Callable[Concatenate[_T_co, _P], _R_co]) –The target function or method whose parameters will be injected from the instance's
self.kwargsmapping.
Classes:
-
add_to_kwargs–Variant of
inject_kwargs_paramsthat merges unused entries fromself.kwargsinto the keyword arguments
Methods:
-
__call__– -
with_name–Decorator factory that creates a subclass of
inject_kwargs_paramswith a custom name
Source code in jetpytools/types/utils.py
425 426 427 428 429 430 431 432 433 434 | |
add_to_kwargs ¶
add_to_kwargs(func: Callable[Concatenate[_T_co, _P], _R_co])
Bases: _InjectKwargsParamsBase[_T0_co, _P0, _R0_co]
Variant of inject_kwargs_params that merges unused entries from self.kwargs into the keyword arguments passed to the target function.
This allows additional context or configuration values to be forwarded without requiring explicit parameters.
Initialize the inject_kwargs_params descriptor.
Parameters:
-
(func¶Callable[Concatenate[_T_co, _P], _R_co]) –The target function or method whose parameters will be injected from the instance's
self.kwargsmapping.
Methods:
-
__call__– -
with_name–Decorator factory that creates a subclass of
inject_kwargs_paramswith a custom name
Source code in jetpytools/types/utils.py
425 426 427 428 429 430 431 432 433 434 | |
__call__ ¶
__call__(self_, self: _T_co, *args: args, **kwargs: kwargs) -> _R_co
Source code in jetpytools/types/utils.py
495 496 | |
with_name classmethod ¶
with_name(
kwargs_name: str = "kwargs",
) -> Callable[
[Callable[Concatenate[T0, P0], R0]], inject_kwargs_params[T0, P0, R0]
]
Decorator factory that creates a subclass of inject_kwargs_params with a custom name for the keyword argument store.
Source code in jetpytools/types/utils.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
__call__ ¶
__call__(self_, self: _T_co, *args: args, **kwargs: kwargs) -> _R_co
Source code in jetpytools/types/utils.py
495 496 | |
with_name classmethod ¶
with_name(
kwargs_name: str = "kwargs",
) -> Callable[
[Callable[Concatenate[T0, P0], R0]], inject_kwargs_params[T0, P0, R0]
]
Decorator factory that creates a subclass of inject_kwargs_params with a custom name for the keyword argument store.
Source code in jetpytools/types/utils.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
inject_self ¶
Bases: _InjectSelfBase[_T_co, _P, _R_co]
Descriptor that ensures the wrapped function always has a constructed self.
When accessed via a class, it will automatically instantiate an object before calling the function. When accessed via an instance, it simply binds.
Subclasses such as cached, init_kwargs, and init_kwargs.clean define variations in how the injected object is created or reused.
Initialize the inject_self descriptor.
Parameters:
-
(function¶Callable[Concatenate[_T_co, _P], _R_co]) –The function or method to wrap.
-
(*args¶Any, default:()) –Positional arguments to pass when instantiating the target class.
-
(**kwargs¶Any, default:{}) –Keyword arguments to pass when instantiating the target class.
Classes:
-
cached–Variant of
inject_selfthat caches the constructed instance. -
init_kwargs–Variant of
inject_selfthat forwards function keyword arguments to the class constructor -
property–Property variant of
inject_selfthat auto-calls the wrapped method.
Methods:
-
__call__– -
with_args–Decorator factory to construct an
inject_selfor subclass (cached,init_kwargs, etc.)
Attributes:
Source code in jetpytools/types/utils.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
cached ¶
Bases: _InjectSelfBase[_T0_co, _P0, _R0_co]
Variant of inject_self that caches the constructed instance.
The first time the method is accessed via the class, a self object is created and stored. Subsequent calls reuse it.
Initialize the inject_self descriptor.
Parameters:
-
(function¶Callable[Concatenate[_T_co, _P], _R_co]) –The function or method to wrap.
-
(*args¶Any, default:()) –Positional arguments to pass when instantiating the target class.
-
(**kwargs¶Any, default:{}) –Keyword arguments to pass when instantiating the target class.
Classes:
-
property–Property variant of
inject_self.cachedthat auto-calls the wrapped method.
Methods:
-
__call__– -
clear_cache–Clear instances cached by
inject_self.cached. -
with_args–Decorator factory to construct an
inject_selfor subclass (cached,init_kwargs, etc.)
Attributes:
Source code in jetpytools/types/utils.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
property ¶
property(function: Callable[[_T1_co], _R1_co])
Bases: Generic[_T1_co, _P1, _R1_co]
Property variant of inject_self.cached that auto-calls the wrapped method.
Source code in jetpytools/types/utils.py
378 379 | |
__call__ ¶
__call__(self_, self: _T_co, *args: args, **kwargs: kwargs) -> _R_co
Source code in jetpytools/types/utils.py
299 300 | |
clear_cache staticmethod ¶
Clear instances cached by inject_self.cached.
The cache is shared by all inject_self.cached descriptors and is keyed by owning class. Clearing one owner therefore resets every cached descriptor on that class.
Parameters:
-
(owner¶type[Any] | None, default:None) –The class whose cached instance should be removed. If omitted, clears cached instances for every class.
Source code in jetpytools/types/utils.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
with_args classmethod ¶
with_args(
*args: Any, **kwargs: Any
) -> Callable[
[Callable[Concatenate[T0, P0], R0]], inject_self[T0, P0, R0]
]
Decorator factory to construct an inject_self or subclass (cached, init_kwargs, etc.) with specific instantiation arguments.
Source code in jetpytools/types/utils.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
init_kwargs ¶
Bases: _InjectSelfBase[_T0_co, _P0, _R0_co]
Variant of inject_self that forwards function keyword arguments to the class constructor when instantiating self.
Initialize the inject_self descriptor.
Parameters:
-
(function¶Callable[Concatenate[_T_co, _P], _R_co]) –The function or method to wrap.
-
(*args¶Any, default:()) –Positional arguments to pass when instantiating the target class.
-
(**kwargs¶Any, default:{}) –Keyword arguments to pass when instantiating the target class.
Classes:
-
clean–Variant of
inject_self.init_kwargsthat removes any forwarded kwargs from the final function call
Methods:
-
__call__– -
with_args–Decorator factory to construct an
inject_selfor subclass (cached,init_kwargs, etc.)
Attributes:
Source code in jetpytools/types/utils.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
clean ¶
Bases: _InjectSelfBase[_T1_co, _P1, _R1_co]
Variant of inject_self.init_kwargs that removes any forwarded kwargs from the final function call after using them for construction.
Initialize the inject_self descriptor.
Parameters:
-
(function¶Callable[Concatenate[_T_co, _P], _R_co]) –The function or method to wrap.
-
(*args¶Any, default:()) –Positional arguments to pass when instantiating the target class.
-
(**kwargs¶Any, default:{}) –Keyword arguments to pass when instantiating the target class.
Methods:
-
__call__– -
with_args–Decorator factory to construct an
inject_selfor subclass (cached,init_kwargs, etc.)
Attributes:
Source code in jetpytools/types/utils.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
__call__ ¶
__call__(self_, self: _T_co, *args: args, **kwargs: kwargs) -> _R_co
Source code in jetpytools/types/utils.py
299 300 | |
with_args classmethod ¶
with_args(
*args: Any, **kwargs: Any
) -> Callable[
[Callable[Concatenate[T0, P0], R0]], inject_self[T0, P0, R0]
]
Decorator factory to construct an inject_self or subclass (cached, init_kwargs, etc.) with specific instantiation arguments.
Source code in jetpytools/types/utils.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
__call__ ¶
__call__(self_, self: _T_co, *args: args, **kwargs: kwargs) -> _R_co
Source code in jetpytools/types/utils.py
299 300 | |
with_args classmethod ¶
with_args(
*args: Any, **kwargs: Any
) -> Callable[
[Callable[Concatenate[T0, P0], R0]], inject_self[T0, P0, R0]
]
Decorator factory to construct an inject_self or subclass (cached, init_kwargs, etc.) with specific instantiation arguments.
Source code in jetpytools/types/utils.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
property ¶
property(function: Callable[[_T0_co], _R0_co])
Bases: Generic[_T0_co, _R0_co]
Property variant of inject_self that auto-calls the wrapped method.
Source code in jetpytools/types/utils.py
406 407 | |
__call__ ¶
__call__(self_, self: _T_co, *args: args, **kwargs: kwargs) -> _R_co
Source code in jetpytools/types/utils.py
299 300 | |
with_args classmethod ¶
with_args(
*args: Any, **kwargs: Any
) -> Callable[
[Callable[Concatenate[T0, P0], R0]], inject_self[T0, P0, R0]
]
Decorator factory to construct an inject_self or subclass (cached, init_kwargs, etc.) with specific instantiation arguments.
Source code in jetpytools/types/utils.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
complex_hash ¶
Class decorator that automatically adds a __hash__ method to the target class.
The generated __hash__ method computes a hash value derived from: - the class's name - the values of all attributes listed in its type annotations.
This is particularly useful for immutable data structures (e.g., NamedTuples or dataclasses).
Source code in jetpytools/types/utils.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 | |
copy_signature ¶
Utility function to copy the signature of one function to another one.
Especially useful for passthrough functions.
.. code-block::
class SomeClass: def init( self, some: Any, complex: Any, /, *args: Any, long: Any, signature: Any, **kwargs: Any ) -> None: ...
class SomeClassChild(SomeClass): @copy_signature(SomeClass.init) def init(*args: Any, **kwargs: Any) -> None: super().init(*args, **kwargs) # do some other thing
class Example(SomeClass): @copy_signature(SomeClass.init) def init(*args: Any, **kwargs: Any) -> None: super().init(*args, **kwargs) # another thing
Source code in jetpytools/types/utils.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
get_subclasses ¶
Get all subclasses of a given type.
Parameters:
-
(family¶type[T]) –"Main" type all other classes inherit from.
-
(exclude¶Sequence[type[T]], default:[]) –Excluded types from the yield. Note that they won't be excluded from search. For examples, subclasses of these excluded classes will be yield.
Returns:
Source code in jetpytools/types/utils.py
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 | |