-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added non-string labels in tasks. (#243)
- Loading branch information
Showing
11 changed files
with
107 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import base64 | ||
import enum | ||
from typing import Any, Callable, Dict, Optional, Tuple | ||
|
||
|
||
class LabelType(enum.IntEnum): | ||
"""Possible label types.""" | ||
|
||
ANY = enum.auto() | ||
INT = enum.auto() | ||
STR = enum.auto() | ||
FLOAT = enum.auto() | ||
BOOL = enum.auto() | ||
BYTES = enum.auto() | ||
|
||
|
||
_LABEL_PARSERS: Dict[LabelType, Callable[[str], Any]] = { | ||
LabelType.INT: int, | ||
LabelType.STR: str, | ||
LabelType.FLOAT: float, | ||
LabelType.BOOL: lambda x: x.lower() == "true", | ||
LabelType.BYTES: base64.b64decode, | ||
LabelType.ANY: lambda x: x, | ||
} | ||
|
||
|
||
def prepare_label(label_value: Any) -> Tuple[str, int]: | ||
""" | ||
Prepare label value for serialization. | ||
:param label_value: label value to prepare. | ||
:return: tuple of prepared label value and its type. | ||
""" | ||
var_type = type(label_value) | ||
if var_type in (int, str, float, bool): | ||
return str(label_value), LabelType[var_type.__name__.upper()].value | ||
if var_type == bytes: | ||
return base64.b64encode(label_value).decode(), LabelType.BYTES.value | ||
return str(label_value), LabelType.ANY.value | ||
|
||
|
||
def parse_label(label_value: Any, label_type: Optional[int] = None) -> Any: | ||
""" | ||
Parse label value from serialized format. | ||
:param label_value: label value to parse. | ||
:param label_type: label type. | ||
:return: parsed label value. | ||
""" | ||
if label_type is None: | ||
return label_value | ||
label_type = LabelType(label_type) | ||
if label_type in _LABEL_PARSERS: | ||
return _LABEL_PARSERS[label_type](label_value) | ||
raise ValueError(f"Unsupported label type: {label_type}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters