-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathpath.py
More file actions
32 lines (24 loc) · 892 Bytes
/
path.py
File metadata and controls
32 lines (24 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Path of indices"""
from __future__ import annotations
from typing import NamedTuple
__all__ = ["Path"]
class Path(NamedTuple):
"""A generic path of string or integer indices"""
prev: Path | None
"""path with the previous indices"""
key: str | int
"""current index in the path (string or integer)"""
typename: str | None
"""name of the parent type to avoid path ambiguity"""
def add_key(self, key: str | int, typename: str | None = None) -> Path:
"""Return a new Path containing the given key."""
return Path(self, key, typename)
def as_list(self) -> list[str | int]:
"""Return a list of the path keys."""
flattened: list[str | int] = []
append = flattened.append
curr: Path | None = self
while curr:
append(curr.key)
curr = curr.prev
return flattened[::-1]