enum_proto
frequenz.client.common.enum_proto ¤
Conversion of protobuf int enums to Python enums.
Attributes¤
frequenz.client.common.enum_proto.EnumT
module-attribute
¤
A type variable that is bound to an enum.
Functions¤
frequenz.client.common.enum_proto.enum_from_proto ¤
Convert a protobuf int enum value to a python enum.
Example
import enum
from proto import proto_pb2 # Just an example. pylint: disable=import-error
@enum.unique
class SomeEnum(enum.Enum):
# These values should match the protobuf enum values.
UNSPECIFIED = 0
SOME_VALUE = 1
enum_value = enum_from_proto(proto_pb2.SomeEnum.SOME_ENUM_SOME_VALUE, SomeEnum)
# -> SomeEnum.SOME_VALUE
enum_value = enum_from_proto(42, SomeEnum)
# -> 42
enum_value = enum_from_proto(
proto_pb2.SomeEnum.SOME_ENUM_UNKNOWN_VALUE, SomeEnum, allow_invalid=False
)
# -> ValueError
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The protobuf int enum value.
TYPE:
|
enum_type
|
The python enum type to convert to. |
allow_invalid
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EnumT | int
|
The resulting python enum value if the protobuf value is known, otherwise
the input value converted to a plain |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |