monkey_wrench.input_output._common module
- monkey_wrench.input_output._common.copy_files_between_directories(source_directory: ~typing.Annotated[~pathlib.Path, ~pydantic.types.PathType(path_type=dir), ~pydantic.functional_validators.AfterValidator(func=~monkey_wrench.input_output._types.<lambda>)], destination_directory: ~typing.Annotated[~pathlib.Path, ~pydantic.types.PathType(path_type=dir), ~pydantic.functional_validators.AfterValidator(func=~monkey_wrench.input_output._types.<lambda>)], pattern: ~monkey_wrench.generic.models._pattern.Pattern | None = None) list[Path][source]
Copy (top-level) files whose names include the pattern from one directory to another.
Warning
The copying is not performed recursively. Only the top-level files are copied.
- Parameters:
source_directory – The source directory to copy files from.
destination_directory – The destination directory to copy files to.
pattern – The pattern to filter the files.
- Returns:
The list of filepaths that have been copied.
- monkey_wrench.input_output._common.copy_single_file_to_directory(destination_directory: ~typing.Annotated[~pathlib.Path, ~pydantic.types.PathType(path_type=dir), ~pydantic.functional_validators.AfterValidator(func=~monkey_wrench.input_output._types.<lambda>)], filepath: ~typing.Annotated[~pathlib.Path, ~pydantic.types.PathType(path_type=file), ~pydantic.functional_validators.AfterValidator(func=~monkey_wrench.input_output._types.<lambda>)]) None[source]
Copy a single file with the given path to another destination directory.
- Parameters:
destination_directory – The destination directory to copy the given file to.
filepath – The path of the file that needs to be copied.
- monkey_wrench.input_output._common.datetime_to_filename(prefix: str, datetime_object: datetime, extension: str = '.nc') Path[source]
Generate a CHIMP-compliant filename based on the datetime object and the given prefix.
- Parameters:
prefix – A string with which the filename will start.
datetime_object – The datetime object to retrieve the timestamp string from.
extension – The file extension, Defaults to
".nc".
- Returns:
A filename with the following format
"<prefix>_<year><month><day>_<hour>_<minute><extension>".
- monkey_wrench.input_output._common.__dispatch(prefix: str, single_item_or_list: datetime | str | list[datetime] | set[datetime] | tuple[datetime, ...] | list[str] | set[str] | tuple[str, ...], datetime_parser: type[DateTimeParserBase] | None = None, extension: str = '.nc') Path | list[Path][source]
Dispatch the given input to its corresponding CHIMP-compliant filename function.
- monkey_wrench.input_output._common.output_filename_from_datetime(datetime_objects: datetime | list[datetime] | set[datetime] | tuple[datetime, ...], extension: str = '.nc') Path | list[Path] | set[Path] | tuple[Path, ...][source]
Generate (a) CHIMP-compliant output filename(s) based on (a) datetime object(s).
- Parameters:
datetime_objects – Either a single datetime object , or a list/set/tuple of datetime objects.
extension – The file extension, Defaults to
".nc".
- Returns:
Depending on the input, either a single filename, or a list/set/tuple of filenames. The type of the output matches the type of the input in case of a list/set.tuple, e.g. a tuple of strings as input will result in a tuple of paths.
Example
>>> output_filename_from_datetime(datetime(2020, 1, 1, 0, 12)) PosixPath('chimp_20200101_00_12.nc')
>>> output_filename_from_datetime( ... [datetime(2020, 1, 1, 0, 12), datetime(2020, 3, 4, 2, 42)] ... ) [PosixPath('chimp_20200101_00_12.nc'), PosixPath('chimp_20200304_02_42.nc')]