Io
zenml.io
The io
module handles file operations for the ZenML package.
It offers a standard interface for reading, writing and manipulating files and
directories. It is heavily influenced and inspired by the io
module of tfx
.
Modules
fileio
Functionality for reading, writing and managing files.
Classes
Functions
convert_to_str(path: PathType) -> str
Converts a "PathType" to a str using UTF-8.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
The path as a string. |
Source code in src/zenml/io/fileio.py
40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
copy(src: PathType, dst: PathType, overwrite: bool = False) -> None
Copy a file from the source to the destination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
PathType
|
The path of the file to copy. |
required |
dst
|
PathType
|
The path to copy the source file to. |
required |
overwrite
|
bool
|
Whether to overwrite the destination file if it exists. |
False
|
Raises:
Type | Description |
---|---|
FileExistsError
|
If a file already exists at the destination and
|
Source code in src/zenml/io/fileio.py
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 93 94 |
|
exists(path: PathType) -> bool
Check whether a given path exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/zenml/io/fileio.py
97 98 99 100 101 102 103 104 105 106 |
|
glob(pattern: PathType) -> List[PathType]
Find all files matching the given pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
PathType
|
The pattern to match. |
required |
Returns:
Type | Description |
---|---|
List[PathType]
|
A list of paths matching the pattern. |
Source code in src/zenml/io/fileio.py
109 110 111 112 113 114 115 116 117 118 |
|
isdir(path: PathType) -> bool
Check whether the given path is a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/zenml/io/fileio.py
121 122 123 124 125 126 127 128 129 130 |
|
listdir(path: str, only_file_names: bool = True) -> List[str]
Lists all files in a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the directory. |
required |
only_file_names
|
bool
|
If True, only return the file names, not the full path. |
True
|
Returns:
Type | Description |
---|---|
List[str]
|
A list of files in the directory. |
Source code in src/zenml/io/fileio.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
makedirs(path: PathType) -> None
Make a directory at the given path, recursively creating parents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the directory. |
required |
Source code in src/zenml/io/fileio.py
155 156 157 158 159 160 161 |
|
mkdir(path: PathType) -> None
Make a directory at the given path; parent directory must exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the directory. |
required |
Source code in src/zenml/io/fileio.py
164 165 166 167 168 169 170 |
|
open(path: PathType, mode: str = 'r') -> Any
Opens a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
mode
|
str
|
The mode to open the file in. |
'r'
|
Returns:
Type | Description |
---|---|
Any
|
The opened file. |
Source code in src/zenml/io/fileio.py
55 56 57 58 59 60 61 62 63 64 65 |
|
remove(path: PathType) -> None
Remove the file at the given path. Dangerous operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file to remove. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the file does not exist. |
Source code in src/zenml/io/fileio.py
173 174 175 176 177 178 179 180 181 182 183 184 |
|
rename(src: PathType, dst: PathType, overwrite: bool = False) -> None
Rename a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
PathType
|
The path of the file to rename. |
required |
dst
|
PathType
|
The path to rename the source file to. |
required |
overwrite
|
bool
|
If a file already exists at the destination, this
method will overwrite it if overwrite= |
False
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the source and destination file systems are not the same. |
Source code in src/zenml/io/fileio.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
rmtree(dir_path: str) -> None
Deletes a directory recursively. Dangerous operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path
|
str
|
The path to the directory to delete. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If the path is not pointing to a directory. |
Source code in src/zenml/io/fileio.py
212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
size(path: PathType) -> Optional[int]
Get the size of a file or directory in bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
Returns:
Type | Description |
---|---|
Optional[int]
|
The size of the file or directory in bytes or |
Optional[int]
|
file system does not implement the |
Source code in src/zenml/io/fileio.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
stat(path: PathType) -> Any
Get the stat descriptor for a given file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
Returns:
Type | Description |
---|---|
Any
|
The stat descriptor. |
Source code in src/zenml/io/fileio.py
227 228 229 230 231 232 233 234 235 236 |
|
walk(top: PathType, topdown: bool = True, onerror: Optional[Callable[..., None]] = None) -> Iterable[Tuple[PathType, List[PathType], List[PathType]]]
Return an iterator that walks the contents of the given directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
top
|
PathType
|
The path of directory to walk. |
required |
topdown
|
bool
|
Whether to walk directories topdown or bottom-up. |
True
|
onerror
|
Optional[Callable[..., None]]
|
Callable that gets called if an error occurs. |
None
|
Returns:
Type | Description |
---|---|
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
An Iterable of Tuples, each of which contain the path of the current |
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
directory path, a list of directories inside the current directory |
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
and a list of files inside the current directory. |
Source code in src/zenml/io/fileio.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
|
Modules
filesystem
Defines the filesystem abstraction of ZenML.
Classes
BaseFilesystem
Bases: ABC
Abstract Filesystem base class.
Design inspired by the Filesystem
abstraction in TFX:
https://github.com/tensorflow/tfx/blob/master/tfx/dsl/io/filesystem.py
copyfile(src: PathType, dst: PathType, overwrite: bool = False) -> None
abstractmethod
staticmethod
Copy a file from the source to the destination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
PathType
|
The path of the file to copy. |
required |
dst
|
PathType
|
The path to copy the source file to. |
required |
overwrite
|
bool
|
Whether to overwrite the destination file if it exists. |
False
|
Raises:
Type | Description |
---|---|
FileExistsError
|
If a file already exists at the destination and
|
Source code in src/zenml/io/filesystem.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
exists(path: PathType) -> bool
abstractmethod
staticmethod
Check whether a given path exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/zenml/io/filesystem.py
85 86 87 88 89 90 91 92 93 94 95 |
|
glob(pattern: PathType) -> List[PathType]
abstractmethod
staticmethod
Find all files matching the given pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
PathType
|
The pattern to match. |
required |
Returns:
Type | Description |
---|---|
List[PathType]
|
A list of paths matching the pattern. |
Source code in src/zenml/io/filesystem.py
97 98 99 100 101 102 103 104 105 106 107 |
|
isdir(path: PathType) -> bool
abstractmethod
staticmethod
Check whether the given path is a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/zenml/io/filesystem.py
109 110 111 112 113 114 115 116 117 118 119 |
|
listdir(path: PathType) -> List[PathType]
abstractmethod
staticmethod
Lists all files in a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the directory. |
required |
Returns:
Type | Description |
---|---|
List[PathType]
|
A list of files in the directory. |
Source code in src/zenml/io/filesystem.py
121 122 123 124 125 126 127 128 129 130 131 |
|
makedirs(path: PathType) -> None
abstractmethod
staticmethod
Make a directory at the given path, recursively creating parents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
Path to the directory. |
required |
Source code in src/zenml/io/filesystem.py
133 134 135 136 137 138 139 140 |
|
mkdir(path: PathType) -> None
abstractmethod
staticmethod
Make a directory at the given path; parent directory must exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
Path to the directory. |
required |
Source code in src/zenml/io/filesystem.py
142 143 144 145 146 147 148 149 |
|
open(path: PathType, mode: str = 'r') -> Any
abstractmethod
staticmethod
Opens a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
mode
|
str
|
The mode to open the file in. |
'r'
|
Returns:
Type | Description |
---|---|
Any
|
The opened file. |
Source code in src/zenml/io/filesystem.py
55 56 57 58 59 60 61 62 63 64 65 66 |
|
remove(path: PathType) -> None
abstractmethod
staticmethod
Remove the file at the given path. Dangerous operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file to remove. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the file does not exist. |
Source code in src/zenml/io/filesystem.py
151 152 153 154 155 156 157 158 159 160 161 |
|
rename(src: PathType, dst: PathType, overwrite: bool = False) -> None
abstractmethod
staticmethod
Rename a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
PathType
|
The path of the file to rename. |
required |
dst
|
PathType
|
The path to rename the source file to. |
required |
overwrite
|
bool
|
If a file already exists at the destination, this
method will overwrite it if overwrite= |
False
|
Raises:
Type | Description |
---|---|
FileExistsError
|
If a file already exists at the destination
and overwrite is not set to |
Source code in src/zenml/io/filesystem.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
rmtree(path: PathType) -> None
abstractmethod
staticmethod
Deletes a directory recursively. Dangerous operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the directory to delete. |
required |
Source code in src/zenml/io/filesystem.py
180 181 182 183 184 185 186 187 |
|
size(path: PathType) -> int
staticmethod
Get the size of a file in bytes.
To be implemented by subclasses but not abstract for backwards compatibility.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
Returns:
Type | Description |
---|---|
int
|
The size of the file in bytes. |
Source code in src/zenml/io/filesystem.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
stat(path: PathType) -> Any
abstractmethod
staticmethod
Get the stat descriptor for a given file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
Returns:
Type | Description |
---|---|
Any
|
The stat descriptor. |
Source code in src/zenml/io/filesystem.py
189 190 191 192 193 194 195 196 197 198 199 |
|
walk(top: PathType, topdown: bool = True, onerror: Optional[Callable[..., None]] = None) -> Iterable[Tuple[PathType, List[PathType], List[PathType]]]
abstractmethod
staticmethod
Return an iterator that walks the contents of the given directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
top
|
PathType
|
The path of directory to walk. |
required |
topdown
|
bool
|
Whether to walk directories topdown or bottom-up. |
True
|
onerror
|
Optional[Callable[..., None]]
|
Callable that gets called if an error occurs. |
None
|
Returns:
Type | Description |
---|---|
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
An Iterable of Tuples, each of which contain the path of the current |
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
directory path, a list of directories inside the current directory |
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
and a list of files inside the current directory. |
Source code in src/zenml/io/filesystem.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
filesystem_registry
Filesystem registry managing filesystem plugins.
Classes
FileIORegistry()
Registry of pluggable filesystem implementations.
Initialize the registry.
Source code in src/zenml/io/filesystem_registry.py
46 47 48 49 |
|
get_filesystem_for_path(path: PathType) -> Type[BaseFilesystem]
Get filesystem plugin for given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to get the filesystem for. |
required |
Returns:
Type | Description |
---|---|
Type[BaseFilesystem]
|
The filesystem plugin for the given path. |
Raises:
Type | Description |
---|---|
ValueError
|
If no filesystem plugin is registered for the given path. |
Source code in src/zenml/io/filesystem_registry.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
get_filesystem_for_scheme(scheme: PathType) -> Type[BaseFilesystem]
Get filesystem plugin for given scheme string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scheme
|
PathType
|
The scheme to get the filesystem for. |
required |
Returns:
Type | Description |
---|---|
Type[BaseFilesystem]
|
The filesystem plugin for the given scheme. |
Raises:
Type | Description |
---|---|
ValueError
|
If no filesystem plugin is registered for the given scheme. |
Source code in src/zenml/io/filesystem_registry.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
register(filesystem_cls: Type[BaseFilesystem]) -> None
Register a filesystem implementation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filesystem_cls
|
Type[BaseFilesystem]
|
Subclass of |
required |
Source code in src/zenml/io/filesystem_registry.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
Functions
local_filesystem
Local filesystem using Python's built-in modules (os
, shutil
, glob
).
Classes
LocalFilesystem
Bases: BaseFilesystem
Filesystem that uses local file operations.
Implementation inspired by TFX: https://github.com/tensorflow/tfx/blob/master/tfx/dsl/io/plugins/local.py
copyfile(src: PathType, dst: PathType, overwrite: bool = False) -> None
staticmethod
Copy a file from the source to the destination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
PathType
|
The source path. |
required |
dst
|
PathType
|
The destination path. |
required |
overwrite
|
bool
|
Whether to overwrite the destination file if it exists. |
False
|
Raises:
Type | Description |
---|---|
FileExistsError
|
If the destination file exists and overwrite is False. |
Source code in src/zenml/io/local_filesystem.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
exists(path: PathType) -> bool
staticmethod
Returns True
if the given path exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
Whether the path exists. |
Source code in src/zenml/io/local_filesystem.py
93 94 95 96 97 98 99 100 101 102 103 |
|
glob(pattern: PathType) -> List[PathType]
staticmethod
Return the paths that match a glob pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
PathType
|
The glob pattern. |
required |
Returns:
Type | Description |
---|---|
List[PathType]
|
List[PathType]: The paths that match the glob pattern. |
Source code in src/zenml/io/local_filesystem.py
105 106 107 108 109 110 111 112 113 114 115 |
|
isdir(path: PathType) -> bool
staticmethod
Returns whether the given path points to a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
Whether the path points to a directory. |
Source code in src/zenml/io/local_filesystem.py
117 118 119 120 121 122 123 124 125 126 127 |
|
listdir(path: PathType) -> List[PathType]
staticmethod
Returns a list of files under a given directory in the filesystem.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the directory. |
required |
Returns:
Type | Description |
---|---|
List[PathType]
|
List[PathType]: The list of files under the given directory. |
Source code in src/zenml/io/local_filesystem.py
129 130 131 132 133 134 135 136 137 138 139 |
|
makedirs(path: PathType) -> None
staticmethod
Make a directory at the given path, recursively creating parents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the directory. |
required |
Source code in src/zenml/io/local_filesystem.py
141 142 143 144 145 146 147 148 |
|
mkdir(path: PathType) -> None
staticmethod
Make a directory at the given path; parent directory must exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the directory. |
required |
Source code in src/zenml/io/local_filesystem.py
150 151 152 153 154 155 156 157 |
|
open(path: PathType, mode: str = 'r') -> Any
staticmethod
Open a file at the given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
mode
|
str
|
The mode to open the file. |
'r'
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The file object. |
Source code in src/zenml/io/local_filesystem.py
57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
remove(path: PathType) -> None
staticmethod
Remove the file at the given path. Dangerous operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
Source code in src/zenml/io/local_filesystem.py
159 160 161 162 163 164 165 166 |
|
rename(src: PathType, dst: PathType, overwrite: bool = False) -> None
staticmethod
Rename source file to destination file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
PathType
|
The path of the file to rename. |
required |
dst
|
PathType
|
The path to rename the source file to. |
required |
overwrite
|
bool
|
If a file already exists at the destination, this
method will overwrite it if overwrite= |
False
|
Raises:
Type | Description |
---|---|
FileExistsError
|
If the destination file exists and overwrite is False. |
Source code in src/zenml/io/local_filesystem.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
rmtree(path: PathType) -> None
staticmethod
Deletes dir recursively. Dangerous operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the directory. |
required |
Source code in src/zenml/io/local_filesystem.py
189 190 191 192 193 194 195 196 |
|
size(path: PathType) -> int
staticmethod
Get the size of a file in bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
Returns:
Type | Description |
---|---|
int
|
The size of the file in bytes. |
Source code in src/zenml/io/local_filesystem.py
210 211 212 213 214 215 216 217 218 219 220 |
|
stat(path: PathType) -> Any
staticmethod
Return the stat descriptor for a given file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathType
|
The path to the file. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The stat descriptor for the file. |
Source code in src/zenml/io/local_filesystem.py
198 199 200 201 202 203 204 205 206 207 208 |
|
walk(top: PathType, topdown: bool = True, onerror: Optional[Callable[..., None]] = None) -> Iterable[Tuple[PathType, List[PathType], List[PathType]]]
staticmethod
Return an iterator that walks the contents of the given directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
top
|
PathType
|
Path of directory to walk. |
required |
topdown
|
bool
|
Whether to walk directories topdown or bottom-up. |
True
|
onerror
|
Optional[Callable[..., None]]
|
Callable that gets called if an error occurs. |
None
|
Yields:
Type | Description |
---|---|
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
An Iterable of Tuples, each of which contain the path of the |
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
current directory path, a list of directories inside the |
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
current directory and a list of files inside the current |
Iterable[Tuple[PathType, List[PathType], List[PathType]]]
|
directory. |
Source code in src/zenml/io/local_filesystem.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|