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
845 846 847 | |
LinearRangeLut ¶
Singleton ¶
Handy class to inherit to have the SingletonMeta metaclass.
SingletonMeta ¶
Bases: type
Methods:
-
__call__–
__call__ ¶
Source code in jetpytools/types/utils.py
871 872 873 874 875 876 877 878 879 880 881 882 883 | |
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
766 767 768 769 770 771 772 773 774 | |
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
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 | |
deleter ¶
deleter(fdel: Callable[..., None]) -> cachedproperty[_R_co, _T_Any]
Source code in jetpytools/types/utils.py
762 | |
getter ¶
getter(fget: Callable[..., _R_co]) -> cachedproperty[_R_co, _T_Any]
Source code in jetpytools/types/utils.py
758 | |
setter ¶
setter(fset: Callable[[Any, _T_Any], None]) -> cachedproperty[_R_co, _T_Any]
Source code in jetpytools/types/utils.py
760 | |
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
829 830 831 832 833 834 835 836 837 838 839 | |
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
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
fdel instance-attribute ¶
fdel: Callable[[type[_T]], None] | None = (
__func__ if isinstance(fdel, classmethod) else fdel
)
fget instance-attribute ¶
fget: Callable[[type[_T]], _R_co] = (
__func__ if isinstance(fget, classmethod) else fget
)
fset instance-attribute ¶
fset: Callable[Concatenate[type[_T], _T_Any, ...], None] | None = (
__func__ if isinstance(fset, classmethod) else fset
)
cached ¶
cached(
fget: Callable[[type[_T" backlink-type="used-by" backlink-anchor="jetpytools.types.utils.classproperty.cached" optional hover>_T]], _R_co] | classmethod[_T" backlink-type="used-by" backlink-anchor="jetpytools.types.utils.classproperty.cached" optional hover>_T, ..., _R_co],
fset: Callable[Concatenate[type[_T" backlink-type="used-by" backlink-anchor="jetpytools.types.utils.classproperty.cached" optional hover>_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
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
fdel instance-attribute ¶
fdel: Callable[[type[_T]], None] | None = (
__func__ if isinstance(fdel, classmethod) else fdel
)
fget instance-attribute ¶
fget: Callable[[type[_T]], _R_co] = (
__func__ if isinstance(fget, classmethod) else fget
)
fset instance-attribute ¶
fset: Callable[Concatenate[type[_T], _T_Any, ...], None] | None = (
__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
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 | |
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
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
fdel instance-attribute ¶
fdel: Callable[[type[_T]], None] | None = (
__func__ if isinstance(fdel, classmethod) else fdel
)
fget instance-attribute ¶
fget: Callable[[type[_T]], _R_co] = (
__func__ if isinstance(fget, classmethod) else fget
)
fset instance-attribute ¶
fset: Callable[Concatenate[type[_T], _T_Any, ...], None] | None = (
__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
407 408 409 410 411 412 413 414 415 416 | |
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
407 408 409 410 411 412 413 414 415 416 | |
__call__ ¶
__call__(self_, self: _T_co, *args: args, **kwargs: kwargs) -> _R_co
Source code in jetpytools/types/utils.py
477 478 | |
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
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
__call__ ¶
__call__(self_, self: _T_co, *args: args, **kwargs: kwargs) -> _R_co
Source code in jetpytools/types/utils.py
477 478 | |
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
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
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__– -
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
360 361 | |
__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 | |
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
388 389 | |
__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
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | |
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
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |