Skip to content

Commit

Permalink
Minor attr -> dataclasses cleanup
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 346958035
  • Loading branch information
Conchylicultor authored and copybara-github committed Dec 11, 2020
1 parent 073a4c4 commit 82f1ecf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
3 changes: 1 addition & 2 deletions tensorflow_datasets/core/tfrecords_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ def read_files(
a tf.data.Dataset instance.
"""
if not file_instructions:
msg = 'Instruction {} corresponds to no data!'.format(
file_instructions)
msg = f'Instruction {file_instructions} corresponds to no data!'
raise AssertionError(msg)

# Prepend path to filename
Expand Down
34 changes: 12 additions & 22 deletions tensorflow_datasets/core/utils/shard_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,29 @@
sharding needs.
"""

from typing import List, Sequence
from typing import Any, List, Sequence

import attr
import dataclasses


@attr.s(frozen=True)
class FileInstruction(object): # TODO(epot): Uses dataclasses instead
@dataclasses.dataclass(eq=True, frozen=True)
class FileInstruction(object):
"""Instruction to read a single shard/file.
Attributes:
filename: The filenames contains the relative path, not absolute.
skip: Indicates which example read in the shard (`ds.skip().take()`). `None`
skip: Indicates which example read in the shard (`ds.skip().take()`). `0`
if no skipping
take: Indicates how many examples to read (`None` to read all)
take: Indicates how many examples to read (`-1` to read all)
num_examples: `int`, The total number of examples
"""
filename = attr.ib()
skip = attr.ib()
take = attr.ib()
num_examples = attr.ib()
filename: str
skip: int
take: int
num_examples: int

def asdict(self):
return {
'filename': self.filename,
'skip': self.skip,
'take': self.take,
'num_examples': self.num_examples,
}

def replace(self, **kwargs):
new_attrs = self.asdict()
new_attrs.update(kwargs)
return type(self)(**new_attrs)
def replace(self, **kwargs: Any) -> 'FileInstruction':
return dataclasses.replace(self, **kwargs)


def get_file_instructions(
Expand Down

0 comments on commit 82f1ecf

Please sign in to comment.