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

implement memoryfs save() and load() #1652

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion fsspec/implementations/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from errno import ENOTEMPTY
from io import BytesIO
from pathlib import PurePath, PureWindowsPath
from typing import Any, ClassVar
from typing import Any, ClassVar, BinaryIO

from fsspec import AbstractFileSystem
from fsspec.implementations.local import LocalFileSystem
Expand Down Expand Up @@ -265,6 +265,17 @@ def rm(self, path, recursive=False, maxdepth=None):
self.rm_file(p)
else:
self.rmdir(p)

def save(self, file: BinaryIO):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please have brief docstrings

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if a URL/storage_options would be friendlier - we do have fsspec.open(), after all.

import pickle
pickle.dump({"store": self.store, "pseudo_dirs": self.pseudo_dirs}, file)

def load(self, file: BinaryIO):
import pickle
_internal = pickle.load(file)
self.store = _internal["store"]
self.pseudo_dirs = _internal["pseudo_dirs"]



class MemoryFile(BytesIO):
Expand Down
17 changes: 17 additions & 0 deletions fsspec/implementations/tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,20 @@ def test_open_path_windows(m):
f.write(b"some\nlines\nof\ntext")

assert m.read_text(path) == "some\nlines\nof\ntext"

def test_save_and_load(m):
from io import BytesIO
internal = BytesIO()
with m.open("/foo/bar/file", "wb") as f:
f.write(b"some\nlines\nof\ntext")
with m.open("/foo/file", "wb") as f:
f.write(b"some\nlines\nof\ntext")
m.makedirs("/foo/foo")
m.save(internal)
m.store = None
m.pseudo_dirs = None
internal.seek(0)
m.load(internal)
assert m.cat("/foo/file") == b"some\nlines\nof\ntext"
assert m.ls("/", False) == ["/foo"]
assert sorted(m.ls("/foo", False)) == sorted(["/foo/foo", "/foo/bar", "/foo/file"])
Loading