Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python(feature): generate channel value from channel config #89

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions python/lib/sift_py/ingestion/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,49 @@ def __init__(
self.bit_field_elements = bit_field_elements
self.enum_types = enum_types

def value_from(
self, value: Optional[Union[int, float, bool, str]]
) -> Optional[IngestWithConfigDataChannelValue]:
"""
Like `try_value_from` except will return `None` there is a failure to produce a channel value due to a type mismatch.
"""
try:
return self.try_value_from(value)
except ValueError:
return None

def try_value_from(
self, value: Optional[Union[int, float, bool, str]]
) -> IngestWithConfigDataChannelValue:
"""
Generate a channel value for this particular channel configuration. This will raise an exception
if there is a type match, namely, if `value` isn't consistent with the channel's data-type. For a version
of this function that does not raise an exception and simply ignores type mistmatches, see `value_from`. If `value`
is `None` then an empty value will be generated.
"""
if value is None:
return empty_value()

if isinstance(value, int) or isinstance(value, float):
if self.data_type == ChannelDataType.INT_32:
return int32_value(int(value))
elif self.data_type == ChannelDataType.INT_64:
return int64_value(int(value))
elif self.data_type == ChannelDataType.UINT_32:
return uint32_value(int(value))
elif self.data_type == ChannelDataType.UINT_64:
return uint64_value(int(value))
elif self.data_type == ChannelDataType.FLOAT:
return float_value(float(value))
elif self.data_type == ChannelDataType.ENUM:
return enum_value(int(value))
elif isinstance(value, str) and self.data_type == ChannelDataType.STRING:
return string_value(value)
elif isinstance(value, bool) and self.data_type == ChannelDataType.BOOL:
return bool_value(value)

raise ValueError(f"Failed to cast value of type {type(value)} to {self.data_type}")

def as_pb(self, klass: Type[ChannelConfigPb]) -> ChannelConfigPb:
return klass(
name=self.name,
Expand Down
Loading