mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-21 00:40:01 +02:00
simplifies, documents and tests parse_serialized_key helper function
This commit is contained in:
parent
a2c60a9c40
commit
87d172b94b
@ -9,18 +9,42 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
def parse_serialized_key(serialized_key: str) -> str | int | float:
|
def parse_serialized_key(serialized_key: str) -> str | int | float:
|
||||||
processed_key: int | float | str = serialized_key
|
"""
|
||||||
if serialized_key.startswith("["):
|
Parse a serialized key and convert it to an appropriate type (int, float, or str).
|
||||||
assert serialized_key.endswith("]")
|
|
||||||
processed_key = serialized_key[1:-1]
|
|
||||||
if '"' in processed_key or "'" in processed_key:
|
|
||||||
processed_key = processed_key[1:-1]
|
|
||||||
elif "." in processed_key:
|
|
||||||
processed_key = float(processed_key)
|
|
||||||
else:
|
|
||||||
processed_key = int(processed_key)
|
|
||||||
|
|
||||||
return processed_key
|
Args:
|
||||||
|
serialized_key: str
|
||||||
|
The serialized key, which might be enclosed in brackets and quotes.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int | float | str:
|
||||||
|
The processed key as an integer, float, or unquoted string.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
```python
|
||||||
|
print(parse_serialized_key("attr_name")) # Outputs: attr_name (str)
|
||||||
|
print(parse_serialized_key("[123]")) # Outputs: 123 (int)
|
||||||
|
print(parse_serialized_key("[12.3]")) # Outputs: 12.3 (float)
|
||||||
|
print(parse_serialized_key("['hello']")) # Outputs: hello (str)
|
||||||
|
print(parse_serialized_key('["12.34"]')) # Outputs: 12.34 (str)
|
||||||
|
print(parse_serialized_key('["complex"]')) # Outputs: complex (str)
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Strip outer brackets if present
|
||||||
|
if serialized_key.startswith("[") and serialized_key.endswith("]"):
|
||||||
|
serialized_key = serialized_key[1:-1]
|
||||||
|
|
||||||
|
# Strip quotes if the resulting string is quoted
|
||||||
|
if serialized_key.startswith(("'", '"')) and serialized_key.endswith(("'", '"')):
|
||||||
|
return serialized_key[1:-1]
|
||||||
|
|
||||||
|
# Try converting to float or int if the string is not quoted
|
||||||
|
try:
|
||||||
|
return float(serialized_key) if "." in serialized_key else int(serialized_key)
|
||||||
|
except ValueError:
|
||||||
|
# Return the original string if it's not a valid number
|
||||||
|
return serialized_key
|
||||||
|
|
||||||
|
|
||||||
def parse_full_access_path(path: str) -> list[str]:
|
def parse_full_access_path(path: str) -> list[str]:
|
||||||
|
@ -8,9 +8,24 @@ from pydase.utils.helpers import (
|
|||||||
get_path_from_path_parts,
|
get_path_from_path_parts,
|
||||||
is_property_attribute,
|
is_property_attribute,
|
||||||
parse_full_access_path,
|
parse_full_access_path,
|
||||||
|
parse_serialized_key,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"serialized_key, expected",
|
||||||
|
[
|
||||||
|
("attr_name", "attr_name"),
|
||||||
|
("[0]", 0),
|
||||||
|
("[0.0]", 0.0),
|
||||||
|
('["some_key"]', "some_key"),
|
||||||
|
('["12.34"]', "12.34"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_parse_serialized_key(serialized_key: str, expected: str) -> None:
|
||||||
|
assert parse_serialized_key(serialized_key) == expected
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"full_access_path, expected",
|
"full_access_path, expected",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user