creates functions to split full access paths and combine the atomic parts back together

This commit is contained in:
Mose Müller
2024-04-25 16:30:20 +02:00
parent 036e80b920
commit c75b203c3d
2 changed files with 91 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
import inspect
import logging
import re
from collections.abc import Callable
from itertools import chain
from typing import Any
@@ -7,6 +8,52 @@ from typing import Any
logger = logging.getLogger(__name__)
def parse_full_access_path(path: str) -> list[str]:
"""
Splits a full access path into its atomic parts, separating attribute names, numeric
indices, and string keys within indices.
The reverse function is given by `get_path_from_path_parts`.
Args:
path: str
The full access path string to be split into components.
Returns:
list[str]
A list of components that make up the path, including attribute names,
numeric indices, and string keys as separate elements.
"""
# <word_with_underscore> | [<any number of digits>]
# | ["<anything except ">"]
# | ['<anything except '>']
pattern = r'\w+|\[\d+\]|\["[^"]*"\]|\[\'[^\']*\']'
return re.findall(pattern, path)
def get_path_from_path_parts(path_parts: list[str]) -> str:
"""Creates the full access path from its atomic parts.
The reverse function is given by `parse_full_access_path`.
Args:
path_parts: list[str]
A list of components that make up the path, including attribute names,
numeric indices and string keys enclosed in square brackets as separate
elements.
Returns:
str
The full access path corresponding to the path_parts.
"""
path = ""
for path_part in path_parts:
if not path_part.startswith("[") and path != "":
path += "."
path += path_part
return path
def get_attribute_doc(attr: Any) -> str | None:
"""This function takes an input attribute attr and returns its documentation
string if it's different from the documentation of its type, otherwise,