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

add from_file #65

Merged
merged 1 commit into from
Nov 29, 2024
Merged
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
28 changes: 28 additions & 0 deletions lazy_dataset/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def new(
elif isinstance(examples, Dataset):
dataset = from_dataset(
examples, immutable_warranty=immutable_warranty, name=name)
elif isinstance(examples, (str, Path)):
dataset = from_file(
examples, immutable_warranty=immutable_warranty, name=name)
else:
raise TypeError(type(examples), examples)
return dataset
Expand Down Expand Up @@ -140,6 +143,31 @@ def from_list(
return ListDataset(examples, name=name).map(deserialize)


def from_file(
examples: [str, Path],
immutable_warranty: str = 'pickle',
name: str = None,
):
assert isinstance(examples, (str, Path)), examples
examples = Path(examples)
if examples.suffix == '.json':
import json
with open(examples) as fd:
examples = json.load(fd)
elif examples.suffix == '.yaml':
import yaml
with open(examples) as fd:
examples = yaml.load(fd)
else:
raise NotImplementedError(examples.suffix, examples)

assert isinstance(examples, (tuple, list, dict)), (type(examples), examples)

if immutable_warranty is None:
return ListDataset(examples, name=name)
return new(examples, immutable_warranty=immutable_warranty, name=name)


def from_dataset(
examples: 'Dataset',
immutable_warranty: str = 'pickle',
Expand Down