Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions pathable/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from typing import TypeVar
from typing import Union

from pyrsistent import pdeque
from pyrsistent import PDeque

from pathable.protocols import Subscriptable
from pathable.types import LookupKey
from pathable.types import LookupValue
Expand Down Expand Up @@ -52,25 +55,32 @@ def len(self, parts: Sequence[Hashable]) -> int:
return len(d)

@classmethod
def _open(cls, content: Subscriptable[SK, SV], parts: Sequence[Hashable]) -> Union[SV, Subscriptable[SK, SV]]:
def _open(cls, content: Subscriptable[SK, SV], parts: PDeque[Hashable]) -> Union[SV, Subscriptable[SK, SV]]:
result: Union[SV, Subscriptable[SK, SV]] = content
for part in parts:
part = cast(SK, part)
# content not travelable
if not isinstance(result, Subscriptable):
raise ValueError
# content trvelable but has no specific part
if not part in result:
raise KeyError
result = result[part]
return result

try:
part = parts[0]
except IndexError:
return result
else:
parts = parts.popleft()

part = cast(SK, part)
# content not subscriptable
if not isinstance(result, Subscriptable):
raise ValueError
# content subscriptable but has no specific part
if not part in result:
raise KeyError
result = result[part]
return cls._open(cast(Subscriptable[SK, SV], result), parts)

@contextmanager
def open(
self, parts: Sequence[Hashable]
) -> Iterator[Union[Subscriptable[SK, SV], Any]]:
try:
yield self._open(self.content, parts)
yield self._open(self.content, pdeque(parts))
finally:
pass

Expand All @@ -85,13 +95,14 @@ def __init__(self, content: Subscriptable[CSK, CSV]):
self._content_refs[id(content)] = content

@classmethod
@lru_cache(maxsize=None)
def _open_content_id(cls, content_id: int, parts: Sequence[Hashable]) -> Union[CSV, Subscriptable[CSK, CSV]]:
@lru_cache
def _open_content_id(cls, content_id: int, parts: PDeque[Hashable]) -> Union[CSV, Subscriptable[CSK, CSV]]:
content: Subscriptable[CSK, CSV] = cls._content_refs[content_id]
return super()._open(content, parts)

@classmethod
def _open(cls, content: Subscriptable[CSK, CSV], parts: Sequence[Hashable]) -> Union[CSV, Subscriptable[CSK, CSV]]:
def _open(cls, content: Subscriptable[CSK, CSV], parts: PDeque[Hashable]) -> Union[CSV, Subscriptable[CSK, CSV]]:
cls._content_refs[id(content)] = content
return cls._open_content_id(id(content), parts)


Expand Down
44 changes: 43 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ classifiers = [

[tool.poetry.dependencies]
python = ">=3.9,<4.0"
pyrsistent = "^0.20.0"

[tool.poetry.extras]
dev = ["pre-commit"]
Expand Down
Loading