Skip to content

Memory Path API

pathlib_next.mempath

MemBytesIO(dest)

Bases: BytesIO

A BytesIO that writes its buffer back into the backing bytearray (dest) on close, so MemPath files persist across open() calls.

Source code in src/pathlib_next/mempath.py
24
25
26
def __init__(self, dest: bytearray) -> None:
    self._bytes = dest
    super().__init__()

MemPath(*segments, backend=None, **kwargs)

Bases: Path

In-memory Path implementation over nested dicts (see MemPathBackend) -- a lightweight virtual filesystem for mocks, tests, or transient storage, and the reference exemplar for Track A of extending this library (subclassing Path directly; see docs/guides/extending.md).

Source code in src/pathlib_next/mempath.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def __init__(
    self, *segments: str | Pathname | Path, backend: MemPathBackend = None, **kwargs
):
    _segments = []
    _backend = None
    for segment in segments:
        if isinstance(segment, MemPath):
            _segments.extend(segment.segments)
            _backend = segment.backend
        elif isinstance(segment, Path):
            raise NotImplementedError()
        elif isinstance(segment, Pathname):
            _segments.extend(segment.segments)
        else:
            _segments.append(segment)
    self._segments = "/".join(_segments).split("/")
    # `is not None`, not truthiness: a freshly-created root's backend is
    # an *empty* dict, which is falsy -- `if _backend:` silently treated
    # that as "no backend found" and gave the child a disconnected new
    # one, breaking backend sharing for any join off an empty MemPath.
    if _backend is not None and backend is None:
        backend = _backend
    self._backend = backend if backend is not None else MemPathBackend()
    self._normalized = None

MemPathBackend

Bases: dict

Nested-dict storage backing one or more MemPath trees. A dict value is a directory; a bytearray value is a file's content. Share one instance across MemPaths (via backend=) to give them the same virtual filesystem.