corelay.tracker
A module that contains the Tracker, which is used to track Slot definitions in classes
that inherit from Plugboard.
Classes
A meta class to track attributes of a type. |
|
Tracks all public class attributes, i.e., all class attributes not enclosed in double underscores. |
- class corelay.tracker.MetaTracker[source]
Bases:
ABCMetaA meta class to track attributes of a type.
Note
This is used to track the slots of a class. In CoRelAy, slots are declared as class attributes, and during instantiation, the class attributes are converted to respective instance attributes of the data type declared in the slot. For example, a
Paramslot is used to declare parameters of processors. When a processor has a class attribute that is aParamwith a data type ofint, then when the processor is instantiated, an instance attribute of the same name is created with the data type ofint.There are two ways a slot can be declared:
The old way of declaring a slot, which is to declare it as a class attribute and assign it a
Slotinstance, e.g., param = Param(int, 0). This is not the recommended way of declaring a slot anymore, because it causes problems with static type checkers, like MyPy. In Python, class attributes can be accessed using the class or using the instance. For example, if a classTesthas a class attribute a = 5, then Test.a and Test().a will both return 5. Static type checkers, like MyPy, do not know that we are converting the class attributeato an instance attribute of typeintduring instantiation, so they will assume that when a slot is accessed using the instance, it will have the same type as the class attribute. This means that the static type checker will show an error when the slot is accessed using the instance, because the type of the class attribute isSlotand notint.The new way of declaring a slot, which is to declare it as a class attribute of type
typing.Annotated. Thetyping.Annotatedtype is a special type that allows us to add metadata to a type hint. The first argument oftyping.Annotatedis the actual type of the attribute, and then any number of additional arguments can be passed, which are used as metadata. So, for example, a parameter slot can be declared as param: Annotated[float, Param(float, 0.0)]. Since the static type checker knows that the actual type of the attribute isfloat, it will not show an error when the slot is accessed using the instance. The metadata is used to store the instance of the slot, which contains the information about the data type, the default value, etc. Unfortunately, this is only a declaration, which has no effect on the runtime. This means, that no actual class attribute is created. Buttyping.Annotatedwill add a special__annotations__attribute to the class, which contains adictwith the names of the declared class attributes and their metadata.
The
MetaTrackermeta class is used to track the class attributes of a class that are not special “dunder” attributes like__class__, as well as the declared class attributes from the__annotations__attribute. The tracked class attributes are stored in ancollections.OrderedDictcalledTracker.__tracked__. TheTrackerclass uses theMetaTrackermeta class and allows users to access the tracked class attributes. The class attributes are tracked in the order they were declared in the class, with the caveat that first, all class attributes come in the order of declaration, and then all declared class attributes come in the order of declaration. As long as only one method of declaring a slot is used, the order of declaration will be preserved.For more information on meta classes, please refer to PEP 3115.
Example
>>> class OrderedInts(metaclass=MetaTracker): ... a = 14 ... b = 21 ... c = 42 ... OrderedInts(a=0).__tracked__ collections.OrderedDict([('a', 0), ('b', 21), ('c', 42)])
- classmethod __prepare__(class_name: str, base_classes: tuple[type, ...], /, **kwargs: Any) OrderedDict[str, Any][source]
Prepare the class dict to be an
collections.OrderedDict. This is done to preserve the order of declaration of the class attributes.- Parameters:
- Returns:
Returns a
collections.OrderedDict, which will be used as the dictionary for the class attributes.- Return type:
- static __new__(mcs, class_name: str, base_classes: tuple[type, ...], class_attributes: OrderedDict[str, Any]) MetaTracker[source]
Is called when a new class is created with the
MetaTrackeras its metaclass. Attaches a newTracker.__tracked__attribute to the class, which is adictwith all public attributes of the class, i.e., those not enclosed in double underscores. If the class that is being created already has aTracker.__tracked__attribute, the new attributes are appended to it.- Parameters:
- Returns:
Returns the new class with the
Tracker.__tracked__attribute.- Return type:
- class corelay.tracker.Tracker[source]
Bases:
objectTracks all public class attributes, i.e., all class attributes not enclosed in double underscores. This makes them available in a class attribute
Tracker.__tracked__using the meta classMetaTracker.- __tracked__: OrderedDict[str, Any]
An
collections.OrderedDictwith all public class attributes, i.e., all class attributes not enclosed with double underscores.
- classmethod collect(dtype: type | tuple[type, ...]) OrderedDict[str, Any][source]
Retrieves all tracked class attributes of a certain type.
- Parameters:
dtype (type | tuple[type, ...]) – The type or types of the class attributes to retrieve.
- Returns:
Returns an
collections.OrderedDictthat contains the public class attributes, i.e., all class attributes not enclosed in double underscores, of the given type or types. The keys are the attribute names and the values are the attribute values.- Return type:
- classmethod get(attribute_name: str) Any[source]
Retrieves a tracked class attribute by name.
- Parameters:
attribute_name (str) – The name of the class attribute to retrieve.
- Raises:
AttributeError – The class attribute does not exist.
- Returns:
Returns the value of the class attribute with the given name. If the class attribute does not exist
Noneis returned.- Return type:
- collect_attr(dtype: type | tuple[type, ...]) OrderedDict[str, Any][source]
Retrieves all instance attributes, corresponding to tracked class attributes of a certain type.
- Parameters:
dtype (type | tuple[type, ...]) – The type or types of the instance attributes to retrieve.
- Returns:
Returns an
collections.OrderedDictthat contains the instance attributes, corresponding to tracked class attributes, of the given type or types. The keys are the attribute names and the values are the attribute values.- Return type:
- get_attr(attribute_name: str) Any[source]
Retrieves an instance attribute, corresponding to a tracked class attribute, by name.
- Parameters:
attribute_name (str) – The name of the class attribute to retrieve.
- Raises:
AttributeError – The instance attribute does not exist.
- Returns:
Returns the value of the instance attribute with the given name. If the instance attribute does not exist
Noneis returned.- Return type: