proto
frequenz.client.common.proto ¤
General utilities for converting common types to/from protobuf types.
Functions¤
frequenz.client.common.proto.datetime_from_proto ¤
Convert a protobuf Timestamp to a datetime.
| PARAMETER | DESCRIPTION |
|---|---|
ts
|
Timestamp object to convert
TYPE:
|
tz
|
Timezone to use for the datetime |
| RETURNS | DESCRIPTION |
|---|---|
datetime
|
Timestamp converted to datetime |
Source code in frequenz/client/common/proto/_timestamp.py
frequenz.client.common.proto.datetime_to_proto ¤
datetime_to_proto(dt: datetime) -> Timestamp
datetime_to_proto(dt: datetime | None) -> Timestamp | None
Convert a datetime to a protobuf Timestamp.
Returns None if dt is None.
| PARAMETER | DESCRIPTION |
|---|---|
dt
|
datetime object to convert
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Timestamp | None
|
datetime converted to Timestamp |
Source code in frequenz/client/common/proto/_timestamp.py
frequenz.client.common.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.
TYPE:
|
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 |