-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'mr/switch_qualifiers_to_dict' into 'master'
Start to switch to qualifier dicts internally See merge request it/e3-core!26
- Loading branch information
Showing
10 changed files
with
188 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
# The following two functions are used during transition of qualifiers | ||
# from str representation to dict by default | ||
|
||
|
||
def qualifier_dict_to_str(qual: dict) -> str: | ||
"""Serialize a dict qualifier into a str. | ||
:param qual: dict qualifier | ||
:return: a deterministic str that represent the dict qualifier | ||
""" | ||
tmp = [] | ||
for k, v in qual.items(): | ||
if isinstance(v, str): | ||
if v: | ||
tmp.append(f"{k}={v}") | ||
else: | ||
# Empty string is used also for tag value. Should be removed | ||
# once switch to dict is complete | ||
tmp.append(f"{k}") | ||
elif isinstance(v, bool): | ||
if v: | ||
tmp.append(k) | ||
elif v: | ||
tmp.append(";".join(sorted(v))) | ||
|
||
return ",".join(sorted(tmp)) | ||
|
||
|
||
def qualifier_str_to_dict(qual: str | None) -> dict: | ||
"""Parse a str qualifier into a dict. | ||
:param qual: a string representing a qualifier | ||
:return: a dict qualifier | ||
""" | ||
if not qual: | ||
return {} | ||
|
||
result: dict[str, str | bool] = {} | ||
|
||
for key, sep, value in (item.partition("=") for item in qual.split(",")): | ||
if sep == "=": | ||
if value: | ||
result[key] = value | ||
else: | ||
result[key] = "" | ||
else: | ||
# Replace by bool once switch to dict is complete | ||
result[key] = "" | ||
return result |
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
Oops, something went wrong.