Association
The association
module defines utilities and types for managing associations through proxy mechanisms. The module's central feature is the Association
type, which is a union of Annotated types using NewType to represent either a CallableProxyType
or a ProxyType
.
This module also provides the function is_association
, which checks if a given value is an instance of either ProxyType
or CallableProxyType
, and hence can be considered an association according to the module's definition.
The association
function within the module is responsible for creating an association to the passed element. If the given element is not already an association, it will be wrapped in a proxy using the proxy
function from the weakref
module. If it is an association, it is returned unchanged. This enables users to maintain and handle associations to objects that should not be strongly referenced while still allowing for the invocation of callable methods if available.
The module also includes a __get__
method, typically used for extending descriptor classes. This method aids in retrieving the association for a given instance, falling back to the class method's behavior if the instance is None
. The use of this method is internally focused and supports the machinery of the association types.
Association = Union[Annotated[T, NewType('Association', CallableProxyType)], Annotated[T, NewType('Association', ProxyType)]]
module-attribute
association(element)
Creates an association proxy object from a given element.
If the element is not already an association proxy object, it will create a new proxy for it using the proxy
function. If it is already an association proxy object, it simply returns the element as-is.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element |
T
|
The element to be converted into or verified as an association proxy object. |
required |
Returns:
Type | Description |
---|---|
Association[T]
|
Association[T]: An association proxy object for the given element. |
Source code in stateforward/model/association.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
is_association(value)
Determines whether the provided value is an instance of ProxyType
or CallableProxyType
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any
|
The value to be checked if it's an association proxy or callable proxy. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the value is an instance of |
Source code in stateforward/model/association.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|