From e41f8347e75892b1776f6fac7b24e2e38bd4f024 Mon Sep 17 00:00:00 2001 From: Tom Close Date: Wed, 6 Dec 2023 14:48:06 +1100 Subject: [PATCH] touched up generate sample data signatures --- .../fileformats/extras/application/medical.py | 6 ++++- fileformats/application/serialization.py | 10 ++++++-- fileformats/core/fileset.py | 6 ++++- fileformats/generic/__init__.py | 25 +++++++++++++++---- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/extras/fileformats/extras/application/medical.py b/extras/fileformats/extras/application/medical.py index 4020014..9dac965 100644 --- a/extras/fileformats/extras/application/medical.py +++ b/extras/fileformats/extras/application/medical.py @@ -1,5 +1,6 @@ import typing as ty from pathlib import Path +from random import Random import pydicom from fileformats.core import FileSet from fileformats.application import Dicom @@ -22,7 +23,10 @@ def dicom_read_metadata( @FileSet.generate_sample_data.register def dicom_generate_sample_data( - dicom: Dicom, dest_dir: Path, seed: int, stem: ty.Optional[str] + dicom: Dicom, + dest_dir: Path, + seed: ty.Union[int, Random] = 0, + stem: ty.Optional[str] = None, ) -> ty.Iterable[Path]: return next( medimages4tests.dummy.dicom.mri.t1w.siemens.skyra.syngo_d13c.get_image().iterdir() diff --git a/fileformats/application/serialization.py b/fileformats/application/serialization.py index 5c25ce0..a93f5cc 100644 --- a/fileformats/application/serialization.py +++ b/fileformats/application/serialization.py @@ -95,7 +95,10 @@ class Toml(DataSerialization): @FileSet.generate_sample_data.register def generate_json_sample_data( - js: Json, dest_dir: Path, seed: int, stem: ty.Optional[str] + js: Json, + dest_dir: Path, + seed: ty.Union[int, Random] = 0, + stem: ty.Optional[str] = None, ) -> ty.Iterable[Path]: js_file = dest_dir / gen_filename(seed, file_type=js, stem=stem) rng = Random(seed + 1) @@ -109,7 +112,10 @@ def generate_json_sample_data( @FileSet.generate_sample_data.register def generate_yaml_sample_data( - yml: Yaml, dest_dir: Path, seed: int, stem: ty.Optional[str] + yml: Yaml, + dest_dir: Path, + seed: ty.Union[int, Random] = 0, + stem: ty.Optional[str] = None, ) -> ty.Iterable[Path]: yml_file = dest_dir / gen_filename(seed, file_type=yml, stem=stem) rng = Random(seed + 1) diff --git a/fileformats/core/fileset.py b/fileformats/core/fileset.py index 08051c3..e83f2ba 100644 --- a/fileformats/core/fileset.py +++ b/fileformats/core/fileset.py @@ -4,6 +4,7 @@ from enum import Enum, IntEnum from warnings import warn import tempfile +from random import Random from collections import Counter import typing as ty import shutil @@ -871,7 +872,10 @@ def sample( @hook.extra def generate_sample_data( - self, dest_dir: Path, seed: int = 0, stem: ty.Optional[str] = None + self, + dest_dir: Path, + seed: ty.Union[int, Random] = 0, + stem: ty.Optional[str] = None, ) -> ty.Iterable[Path]: """Generate test data at the fspaths of the file-set diff --git a/fileformats/generic/__init__.py b/fileformats/generic/__init__.py index 54427fb..cf82701 100644 --- a/fileformats/generic/__init__.py +++ b/fileformats/generic/__init__.py @@ -278,7 +278,10 @@ class SetOf(WithClassifiers, TypedSet): @FileSet.generate_sample_data.register def fsobject_generate_sample_data( - fsobject: FsObject, dest_dir: Path, seed: int, stem: ty.Optional[str] + fsobject: FsObject, + dest_dir: Path, + seed: ty.Union[int, Random] = 0, + stem: ty.Optional[str] = None, ) -> ty.Iterable[Path]: a_file = dest_dir / gen_filename(seed, file_type=fsobject, stem=stem) a_file.write_text("".join(random.choices(string.printable, k=FILE_LENGTH))) @@ -287,7 +290,10 @@ def fsobject_generate_sample_data( @FileSet.generate_sample_data.register def file_generate_sample_data( - file: File, dest_dir: Path, seed: int, stem: ty.Optional[str] + file: File, + dest_dir: Path, + seed: ty.Union[int, Random] = 0, + stem: ty.Optional[str] = None, ) -> ty.Iterable[Path]: fname = gen_filename(seed, file_type=file, stem=stem) stem = fname[: -len(file.strext)] @@ -321,7 +327,10 @@ def file_generate_sample_data( @FileSet.generate_sample_data.register def directory_generate_sample_data( - directory: Directory, dest_dir: Path, seed: int, stem: ty.Optional[str] + directory: Directory, + dest_dir: Path, + seed: ty.Union[int, Random] = 0, + stem: ty.Optional[str] = None, ) -> ty.Iterable[Path]: rng = Random(str(seed) + directory.mime_like) a_dir = dest_dir / gen_filename(rng, stem=stem) @@ -332,7 +341,10 @@ def directory_generate_sample_data( @FileSet.generate_sample_data.register def directory_containing_generate_sample_data( - directory: DirectoryContaining, dest_dir: Path, seed: int, stem: ty.Optional[str] + directory: DirectoryContaining, + dest_dir: Path, + seed: ty.Union[int, Random] = 0, + stem: ty.Optional[str] = None, ) -> ty.Iterable[Path]: rng = Random(str(seed) + directory.mime_like) a_dir = dest_dir / gen_filename(rng, stem=stem) @@ -344,7 +356,10 @@ def directory_containing_generate_sample_data( @FileSet.generate_sample_data.register def set_of_sample_data( - set_of: SetOf, dest_dir: Path, seed: int, stem: ty.Optional[str] + set_of: SetOf, + dest_dir: Path, + seed: ty.Union[int, Random] = 0, + stem: ty.Optional[str] = None, ) -> ty.Iterable[Path]: rng = Random(str(seed) + set_of.mime_like) return list(