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

Use dotty-dict #133

Open
ric-evans opened this issue Jun 22, 2022 · 1 comment
Open

Use dotty-dict #133

ric-evans opened this issue Jun 22, 2022 · 1 comment
Assignees

Comments

@ric-evans
Copy link
Member

replace this:

class DottedKeyError(KeyError):
"""Raised when a value cannot be retrieved using a dot-syntax key."""
def get_val_in_dict_dotted(field: str, dicto: Dict[str, Any]) -> Any:
"""Return value of the `field` at the key/sub-key (dot syntax) from `dicto`.
Raises:
DottedKeyError - if field not found / parent-field is not `dict`-like
"""
try:
if "." not in field: # simple field; ex: "logical_name", "sha512"
return dicto[field] # possible KeyError/TypeError
# compound field; ex: "checksum.sha512"
parent, child = field.split(".", maxsplit=1) # ex: "checksum" & "sha512"
# ex: is "sha512" in "checksum"'s dict?
# possible KeyError/TypeError
return get_val_in_dict_dotted(child, dicto[parent])
except (KeyError, TypeError) as e:
raise DottedKeyError() from e

with this: https://pypi.org/project/dotty-dict/

@ric-evans ric-evans self-assigned this Jun 22, 2022
@ric-evans
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant