Template to produce filenames for sharded datasets. Attributes: data_dir: the directory that contains the files for the shards. template: template of the sharded files, e.g. '${SPLIT}/data.${FILEFORMAT}-${SHARD_INDEX}'. dataset_name: the name of the dataset. split: the split
| 485 | |
| 486 | @dataclasses.dataclass() |
| 487 | class ShardedFileTemplate: |
| 488 | """Template to produce filenames for sharded datasets. |
| 489 | |
| 490 | Attributes: |
| 491 | data_dir: the directory that contains the files for the shards. |
| 492 | template: template of the sharded files, e.g. |
| 493 | '${SPLIT}/data.${FILEFORMAT}-${SHARD_INDEX}'. |
| 494 | dataset_name: the name of the dataset. |
| 495 | split: the split of the dataset. |
| 496 | filetype_suffix: the filetype suffix to denote the type of file. For |
| 497 | example, `tfrecord`. |
| 498 | """ |
| 499 | |
| 500 | data_dir: epath.Path |
| 501 | template: str = DEFAULT_FILENAME_TEMPLATE |
| 502 | dataset_name: str | None = None |
| 503 | split: str | None = None |
| 504 | filetype_suffix: str | None = None |
| 505 | |
| 506 | def __post_init__(self): |
| 507 | self.data_dir = epath.Path(self.data_dir) |
| 508 | if self.split is not None and not self.split: |
| 509 | raise ValueError(f'Split must be a non-empty string: {self}') |
| 510 | if self.split is not None and not any( |
| 511 | char.isalnum() for char in self.split |
| 512 | ): |
| 513 | raise ValueError( |
| 514 | 'Split name should contain at least one alphanumeric character.' |
| 515 | f' Given split name: {self.split}' |
| 516 | ) |
| 517 | if self.filetype_suffix is not None and not self.filetype_suffix: |
| 518 | raise ValueError(f'Filetype suffix must be a non-empty string: {self}') |
| 519 | if not self.template: |
| 520 | self.template = DEFAULT_FILENAME_TEMPLATE |
| 521 | |
| 522 | @functools.cached_property |
| 523 | def regex(self) -> re.Pattern[str]: |
| 524 | """Returns the regular expression for this template. |
| 525 | |
| 526 | Can be used to test whether a filename matches to this template. |
| 527 | """ |
| 528 | return _regex_for_template(self.template) |
| 529 | |
| 530 | def parse_filename_info(self, filename: str) -> FilenameInfo | None: |
| 531 | """Parses the filename using this template. |
| 532 | |
| 533 | Note that when the filename doesn't specify the dataset name, split, or |
| 534 | filetype suffix, but this template does, then the value in the template will |
| 535 | be used. |
| 536 | |
| 537 | Arguments: |
| 538 | filename: the filename that should be parsed. |
| 539 | |
| 540 | Returns: |
| 541 | the FilenameInfo corresponding to the given file if it could be parsed. |
| 542 | None otherwise. |
| 543 | """ |
| 544 |
no outgoing calls
no test coverage detected