-
Notifications
You must be signed in to change notification settings - Fork 3k
[data_files] Only match separated split names #4633
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
Changes from 11 commits
d963a0e
159649a
7893856
31bf1bb
3681589
93f4526
e6adffb
9393386
8efbb78
ee15ede
61a18f8
d372b28
3e97fd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,12 @@ | ||||||||
| import os | ||||||||
| from itertools import chain | ||||||||
| from contextlib import contextmanager | ||||||||
| from pathlib import Path, PurePath | ||||||||
| from typing import List | ||||||||
| from unittest.mock import patch | ||||||||
|
|
||||||||
| import fsspec | ||||||||
| import pytest | ||||||||
| from fsspec.spec import AbstractBufferedFile, AbstractFileSystem | ||||||||
| from huggingface_hub.hf_api import DatasetInfo | ||||||||
|
|
||||||||
| from datasets.data_files import ( | ||||||||
|
|
@@ -491,6 +493,75 @@ def test_DataFilesDict_from_hf_local_or_remote_hashing(text_file): | |||||||
| assert Hasher.hash(data_files1) != Hasher.hash(data_files2) | ||||||||
|
|
||||||||
|
|
||||||||
| @contextmanager | ||||||||
| def mock_fs(file_paths: List[str]): | ||||||||
| """ | ||||||||
| Context manager to set up a mock:// filesystem in fsspec containing the provided files | ||||||||
|
|
||||||||
| Example: | ||||||||
|
|
||||||||
| ```py | ||||||||
| >>> with mock_fs(["data/train.txt", "data.test.txt"]) as fs: | ||||||||
| ... assert fsspec.get_filesystem_class("mock").__name__ == "DummyTestFS" | ||||||||
| ... assert type(fs).__name__ == "DummyTestFS" | ||||||||
| ... print(fs.glob("**")) | ||||||||
| ["data", "data/train.txt", "data.test.txt"] | ||||||||
| ``` | ||||||||
| """ | ||||||||
|
|
||||||||
| dir_paths = {file_path.rsplit("/")[0] for file_path in file_paths if "/" in file_path} | ||||||||
| fs_contents = [{"name": dir_path, "type": "directory"} for dir_path in dir_paths] + [ | ||||||||
| {"name": file_path, "type": "file", "size": 10} for file_path in file_paths | ||||||||
| ] | ||||||||
|
|
||||||||
| class DummyTestFS(AbstractFileSystem): | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome that you use a mock file system. I personally think this goes in the right direction. |
||||||||
| protocol = "mock" | ||||||||
| _file_class = AbstractBufferedFile | ||||||||
| _fs_contents = fs_contents | ||||||||
|
|
||||||||
| def __getitem__(self, name): | ||||||||
| for item in self._fs_contents: | ||||||||
| if item["name"] == name: | ||||||||
| return item | ||||||||
| raise IndexError(f"{name} not found!") | ||||||||
|
|
||||||||
| def ls(self, path, detail=True, refresh=True, **kwargs): | ||||||||
| if kwargs.pop("strip_proto", True): | ||||||||
| path = self._strip_protocol(path) | ||||||||
|
|
||||||||
| files = not refresh and self._ls_from_cache(path) | ||||||||
| if not files: | ||||||||
| files = [file for file in self._fs_contents if path == self._parent(file["name"])] | ||||||||
| files.sort(key=lambda file: file["name"]) | ||||||||
| self.dircache[path.rstrip("/")] = files | ||||||||
|
|
||||||||
| if detail: | ||||||||
| return files | ||||||||
| return [file["name"] for file in files] | ||||||||
|
|
||||||||
| def _open( | ||||||||
| self, | ||||||||
| path, | ||||||||
| mode="rb", | ||||||||
| block_size=None, | ||||||||
| autocommit=True, | ||||||||
| cache_options=None, | ||||||||
| **kwargs, | ||||||||
| ): | ||||||||
| return self._file_class( | ||||||||
| self, | ||||||||
| path, | ||||||||
| mode, | ||||||||
| block_size, | ||||||||
| autocommit, | ||||||||
| cache_options=cache_options, | ||||||||
| **kwargs, | ||||||||
| ) | ||||||||
|
|
||||||||
| with patch.dict(fsspec.registry.target, {"mock": DummyTestFS}): | ||||||||
| yield DummyTestFS() | ||||||||
|
||||||||
| with patch.dict(fsspec.registry.target, {"mock": DummyTestFS}): | |
| yield DummyTestFS() | |
| yield DummyTestFS() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up removing the patching and the context manager :) merging
It makes sense if it is not indeed necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also adding a test for "test": "contest.txt"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Indeed, much clearer this way! Thanks.