id
frequenz.client.microgrid.id ¤
Provides strongly-typed unique identifiers for entities.
This module offers a base class,
BaseId
, which can be subclassed to
create distinct ID types for different components or concepts within a system.
These IDs ensure type safety, meaning that an ID for one type of entity (e.g., a
sensor) cannot be mistakenly used where an ID for another type (e.g., a
microgrid) is expected.
Creating Custom ID Types¤
To define a new ID type, create a class that inherits from
BaseId
and provide a unique
str_prefix
as a keyword argument in the class definition. This prefix is used
in the string representation of the ID and must be unique across all ID types.
Note
The str_prefix
must be unique across all ID types. If you try to use a
prefix that is already registered, a ValueError
will be raised when defining
the class.
To encourage consistency, the class name must end with the suffix "Id" (e.g.,
MyNewId
). This check can be bypassed by passing allow_custom_name=True
when
defining the class (e.g., class MyCustomName(BaseId, str_prefix="MCN",
allow_custom_name=True):
).
Tip
Use the @typing.final
decorator to prevent subclassing of
ID classes.
Creating a standard ID type
Creating an ID type with a non-standard name
from typing import final
from frequenz.client.microgrid.id import BaseId
@final
class CustomNameForId(BaseId, str_prefix="CST", allow_custom_name=True):
"""An ID with a custom name, not ending in 'Id'."""
custom_id = CustomNameForId(456)
print(custom_id) # Output: CST456
print(int(custom_id)) # Output: 456
Predefined ID Types¤
This module predefines the following ID types:
ComponentId
: For identifying generic components.MicrogridId
: For identifying microgrids.SensorId
: For identifying sensors.
Classes¤
frequenz.client.microgrid.id.BaseId ¤
A base class for unique identifiers.
Subclasses must provide a unique str_prefix
keyword argument during
definition, which is used in the string representation of the ID.
By default, subclass names must end with "Id". This can be overridden by
passing allow_custom_name=True
during class definition.
For more information and examples, see the module's documentation.
Source code in frequenz/client/microgrid/id.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
Attributes¤
Functions¤
__eq__ ¤
Check if this instance is equal to another object.
Equality is defined as being of the exact same type and having the same underlying ID.
Source code in frequenz/client/microgrid/id.py
__hash__ ¤
__hash__() -> int
Return the hash of this instance.
The hash is based on the exact type and the underlying ID to ensure that IDs of different types but with the same numeric value have different hashes.
Source code in frequenz/client/microgrid/id.py
__init__ ¤
__init__(id_: int) -> None
Initialize this instance.
PARAMETER | DESCRIPTION |
---|---|
id_
|
The numeric unique identifier.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the ID is negative. |
Source code in frequenz/client/microgrid/id.py
__init_subclass__ ¤
Initialize a subclass, set its string prefix, and perform checks.
PARAMETER | DESCRIPTION |
---|---|
str_prefix
|
The string prefix for the ID type (e.g., "MID"). Must be unique across all ID types.
TYPE:
|
allow_custom_name
|
If True, bypasses the check that the class name must end with "Id". Defaults to False.
TYPE:
|
**kwargs
|
Forwarded to the parent's init_subclass.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the |
TypeError
|
If |
Source code in frequenz/client/microgrid/id.py
__lt__ ¤
Check if this instance is less than another object.
Comparison is only defined between instances of the exact same type.
Source code in frequenz/client/microgrid/id.py
__new__ ¤
Create a new instance of the ID class, only if it is a subclass of BaseId.
Source code in frequenz/client/microgrid/id.py
frequenz.client.microgrid.id.ComponentId ¤
Bases: BaseId
A unique identifier for a microgrid component.
Source code in frequenz/client/microgrid/id.py
Attributes¤
Functions¤
__eq__ ¤
Check if this instance is equal to another object.
Equality is defined as being of the exact same type and having the same underlying ID.
Source code in frequenz/client/microgrid/id.py
__hash__ ¤
__hash__() -> int
Return the hash of this instance.
The hash is based on the exact type and the underlying ID to ensure that IDs of different types but with the same numeric value have different hashes.
Source code in frequenz/client/microgrid/id.py
__init__ ¤
__init__(id_: int) -> None
Initialize this instance.
PARAMETER | DESCRIPTION |
---|---|
id_
|
The numeric unique identifier.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the ID is negative. |
Source code in frequenz/client/microgrid/id.py
__init_subclass__ ¤
Initialize a subclass, set its string prefix, and perform checks.
PARAMETER | DESCRIPTION |
---|---|
str_prefix
|
The string prefix for the ID type (e.g., "MID"). Must be unique across all ID types.
TYPE:
|
allow_custom_name
|
If True, bypasses the check that the class name must end with "Id". Defaults to False.
TYPE:
|
**kwargs
|
Forwarded to the parent's init_subclass.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the |
TypeError
|
If |
Source code in frequenz/client/microgrid/id.py
__lt__ ¤
Check if this instance is less than another object.
Comparison is only defined between instances of the exact same type.
Source code in frequenz/client/microgrid/id.py
__new__ ¤
Create a new instance of the ID class, only if it is a subclass of BaseId.
Source code in frequenz/client/microgrid/id.py
frequenz.client.microgrid.id.MicrogridId ¤
Bases: BaseId
A unique identifier for a microgrid.
Source code in frequenz/client/microgrid/id.py
Attributes¤
Functions¤
__eq__ ¤
Check if this instance is equal to another object.
Equality is defined as being of the exact same type and having the same underlying ID.
Source code in frequenz/client/microgrid/id.py
__hash__ ¤
__hash__() -> int
Return the hash of this instance.
The hash is based on the exact type and the underlying ID to ensure that IDs of different types but with the same numeric value have different hashes.
Source code in frequenz/client/microgrid/id.py
__init__ ¤
__init__(id_: int) -> None
Initialize this instance.
PARAMETER | DESCRIPTION |
---|---|
id_
|
The numeric unique identifier.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the ID is negative. |
Source code in frequenz/client/microgrid/id.py
__init_subclass__ ¤
Initialize a subclass, set its string prefix, and perform checks.
PARAMETER | DESCRIPTION |
---|---|
str_prefix
|
The string prefix for the ID type (e.g., "MID"). Must be unique across all ID types.
TYPE:
|
allow_custom_name
|
If True, bypasses the check that the class name must end with "Id". Defaults to False.
TYPE:
|
**kwargs
|
Forwarded to the parent's init_subclass.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the |
TypeError
|
If |
Source code in frequenz/client/microgrid/id.py
__lt__ ¤
Check if this instance is less than another object.
Comparison is only defined between instances of the exact same type.
Source code in frequenz/client/microgrid/id.py
__new__ ¤
Create a new instance of the ID class, only if it is a subclass of BaseId.
Source code in frequenz/client/microgrid/id.py
frequenz.client.microgrid.id.SensorId ¤
Bases: BaseId
A unique identifier for a microgrid sensor.
Source code in frequenz/client/microgrid/id.py
Attributes¤
Functions¤
__eq__ ¤
Check if this instance is equal to another object.
Equality is defined as being of the exact same type and having the same underlying ID.
Source code in frequenz/client/microgrid/id.py
__hash__ ¤
__hash__() -> int
Return the hash of this instance.
The hash is based on the exact type and the underlying ID to ensure that IDs of different types but with the same numeric value have different hashes.
Source code in frequenz/client/microgrid/id.py
__init__ ¤
__init__(id_: int) -> None
Initialize this instance.
PARAMETER | DESCRIPTION |
---|---|
id_
|
The numeric unique identifier.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the ID is negative. |
Source code in frequenz/client/microgrid/id.py
__init_subclass__ ¤
Initialize a subclass, set its string prefix, and perform checks.
PARAMETER | DESCRIPTION |
---|---|
str_prefix
|
The string prefix for the ID type (e.g., "MID"). Must be unique across all ID types.
TYPE:
|
allow_custom_name
|
If True, bypasses the check that the class name must end with "Id". Defaults to False.
TYPE:
|
**kwargs
|
Forwarded to the parent's init_subclass.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the |
TypeError
|
If |
Source code in frequenz/client/microgrid/id.py
__lt__ ¤
Check if this instance is less than another object.
Comparison is only defined between instances of the exact same type.
Source code in frequenz/client/microgrid/id.py
__new__ ¤
Create a new instance of the ID class, only if it is a subclass of BaseId.