IO
I/O routines for Tapes.
Classes:
-
TapeSaver
–A class for saving Tape objects using YAML format.
Functions:
-
load_tapes
–Load tapes from dir with YAML or JSON files.
-
save_json_tape
–Save a Tape object to a JSON file.
-
stream_yaml_tapes
–Stream YAML tapes to a file.
TapeSaver
A class for saving Tape objects using YAML format.
This class provides functionality to save Tape objects using a YAML dumper. It handles the serialization of Tape objects into YAML format.
Example
dumper = yaml.SafeDumper(output_file)
saver = TapeSaver(dumper)
saver.save(tape)
Methods:
-
__init__
–Initialize TapeIOYML with a YAML dumper.
-
save
–Saves the tape data using the configured dumper.
Source code in tapeagents/io.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
__init__(yaml_dumper)
Initialize TapeIOYML with a YAML dumper.
Parameters:
-
yaml_dumper
(SafeDumper
) –The YAML dumper instance to use for serialization.
Source code in tapeagents/io.py
34 35 36 37 38 39 40 |
|
save(tape)
Saves the tape data using the configured dumper.
Parameters:
-
tape
(Tape
) –The tape object containing the data to be saved.
Source code in tapeagents/io.py
42 43 44 45 46 47 48 49 |
|
load_tapes(tape_class, path, file_extension='.yaml')
Load tapes from dir with YAML or JSON files.
This function loads tapes from a file or directory and converts them into tape objects using the specified tape class or type adapter.
Parameters:
-
tape_class
(Union[Type, TypeAdapter]
) –The class or type adapter used to validate and create tape objects.
-
path
(Union[Path, str]
) –Path to a file or directory containing tape configurations.
-
file_extension
(str
, default:'.yaml'
) –File extension to filter by when loading from directory. Must be either '.yaml' or '.json'. Defaults to '.yaml'.
Returns:
-
list[Tape]
–list[Tape]: A list of validated tape objects.
Raises:
-
FileNotFoundError
–If the specified path does not exist.
-
ValueError
–If an unsupported file extension is provided.
Example
tapes = load_tapes(TapeClass, "configs/tapes.yaml")
tapes = load_tapes(tape_adapter, "configs/tapes", ".json")
Source code in tapeagents/io.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
|
save_json_tape(tape, tapes_dir, name='')
Save a Tape object to a JSON file.
Parameters:
-
tape
(Tape
) –The Tape object to be saved
-
tapes_dir
(str
) –Directory path where the JSON file will be saved
-
name
(str
, default:''
) –Name of the output JSON file. If empty, tapes_dir is used as the full path. If provided without .json extension, it will be added automatically. Defaults to "".
Example
tape = Tape(...)
save_json_tape(tape, "/path/to/dir", "my_tape")
# Saves to /path/to/dir/my_tape.json
Source code in tapeagents/io.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
stream_yaml_tapes(filename, mode='w')
Stream YAML tapes to a file.
This function creates a context manager that allows streaming YAML documents to a file. It handles file creation, directory creation if necessary, and proper resource cleanup.
Parameters:
-
filename
(Union[Path, str]
) –Path to the output YAML file. Can be either a string or Path object.
-
mode
(str
, default:'w'
) –File opening mode. Defaults to "w" (write mode).
Yields:
-
TapeSaver
–Generator[TapeSaver, None, None]: A TapeSaver instance that can be used to write YAML documents.
Raises:
-
OSError
–If there are issues with file/directory creation or permissions.
-
YAMLError
–If there are YAML serialization errors.
Source code in tapeagents/io.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|