Utils
zenml.utils
Initialization of the utils module.
The utils
module contains utility functions handling analytics, reading and
writing YAML data as well as other general purpose functions.
Modules
archivable
Archivable mixin.
Classes
Archivable(*args: Any, **kwargs: Any)
Bases: ABC
Archivable mixin class.
Initialize the object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Unused args for subclasses. |
()
|
**kwargs
|
Any
|
Unused keyword args for subclasses. |
{}
|
Source code in src/zenml/utils/archivable.py
38 39 40 41 42 43 44 45 |
|
add_directory(source: str, destination: str) -> None
Adds a directory to the archive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
str
|
Path to the directory. |
required |
destination
|
str
|
The path inside the build context where the directory should be added. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in src/zenml/utils/archivable.py
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 |
|
add_file(source: str, destination: str) -> None
Adds a file to the archive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
str
|
The source of the file to add. This can either be a path or the file content. |
required |
destination
|
str
|
The path inside the archive where the file should be added. |
required |
Source code in src/zenml/utils/archivable.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
get_extra_files() -> Dict[str, str]
Gets all extra files that should be included in the archive.
Returns:
Type | Description |
---|---|
Dict[str, str]
|
A dict {path_in_archive: file_content} for all extra files in the |
Dict[str, str]
|
archive. |
Source code in src/zenml/utils/archivable.py
171 172 173 174 175 176 177 178 |
|
get_files() -> Dict[str, str]
abstractmethod
Gets all regular files that should be included in the archive.
Returns:
Type | Description |
---|---|
Dict[str, str]
|
A dict {path_in_archive: path_on_filesystem} for all regular files |
Dict[str, str]
|
in the archive. |
Source code in src/zenml/utils/archivable.py
162 163 164 165 166 167 168 169 |
|
write_archive(output_file: IO[bytes], archive_type: ArchiveType = ArchiveType.TAR_GZ) -> None
Writes an archive of the build context to the given file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_file
|
IO[bytes]
|
The file to write the archive to. |
required |
archive_type
|
ArchiveType
|
The type of archive to create. |
TAR_GZ
|
Source code in src/zenml/utils/archivable.py
93 94 95 96 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 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 |
|
ArchiveType
Modules
callback_registry
Callback registry implementation.
Classes
CallbackRegistry()
Callback registry class.
Initializes the callback registry.
Source code in src/zenml/utils/callback_registry.py
30 31 32 33 34 |
|
execute_callbacks(raise_on_exception: bool) -> None
Execute all registered callbacks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raise_on_exception
|
bool
|
If True, exceptions raised during the execution of the callbacks will be raised. If False, a warning with the exception will be logged instead. |
required |
Raises:
Type | Description |
---|---|
Exception
|
Exceptions raised in any of the callbacks if
|
Source code in src/zenml/utils/callback_registry.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
register_callback(callback: Callable[P, Any], *args: P.args, **kwargs: P.kwargs) -> None
Register a callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback
|
Callable[P, Any]
|
The callback to register. |
required |
*args
|
args
|
Arguments to call the callback with. |
()
|
**kwargs
|
kwargs
|
Keyword arguments to call the callback with. |
{}
|
Source code in src/zenml/utils/callback_registry.py
36 37 38 39 40 41 42 43 44 45 46 |
|
reset() -> None
Reset the callbacks.
Source code in src/zenml/utils/callback_registry.py
48 49 50 |
|
Functions
code_repository_utils
Utilities for code repositories.
Classes
Functions
find_active_code_repository(path: Optional[str] = None) -> Optional[LocalRepositoryContext]
Find the active code repository for a given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Optional[str]
|
Path at which to look for the code repository. If not given, the source root will be used. |
None
|
Returns:
Type | Description |
---|---|
Optional[LocalRepositoryContext]
|
The local repository context active at that path or None. |
Source code in src/zenml/utils/code_repository_utils.py
86 87 88 89 90 91 92 93 94 95 96 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
set_custom_local_repository(root: str, commit: str, repo: BaseCodeRepository) -> None
Manually defines a local repository for a path.
To explain what this function does we need to take a dive into source
resolving and what happens inside the Docker image entrypoint:
* When trying to resolve an object to a source, we first determine whether
the file is a user file or not.
* If the file is a user file, we check if that user file is inside a clean
code repository using the
code_repository_utils.find_active_code_repository(...)
function. If that
is the case, the object will be resolved to a CodeRepositorySource
which
includes additional information about the current commit and the ID of the
code repository.
* The code_repository_utils.find_active_code_repository(...)
uses the
code repository implementation classes to check whether the code repository
"exists" at that local path. For git repositories, this check might look as
follows: The code repository first checks if there is a git repository at
that path or in any parent directory. If there is, the remote URLs of this
git repository will be checked to see if one matches the URL defined for
the code repository.
* When running a step inside a Docker image, ZenML potentially downloads
files from a code repository. This usually does not download the entire
repository (and in the case of git might not download a .git directory which
defines a local git repository) but only specific files. If we now try to
resolve any object while running in this container, it will not get resolved
to a CodeRepositorySource
as
code_repository_utils.find_active_code_repository(...)
won't find an
active repository. As we downloaded these files, we however know that they
belong to a certain code repository at a specific commit, and that's what we
can define using this function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
str
|
The repository root. |
required |
commit
|
str
|
The commit of the repository. |
required |
repo
|
BaseCodeRepository
|
The code repository associated with the local repository. |
required |
Source code in src/zenml/utils/code_repository_utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 |
|
Modules
code_utils
Code utilities.
Classes
CodeArchive(root: Optional[str] = None)
Bases: Archivable
Code archive class.
This class is used to archive user code before uploading it to the artifact store. If the user code is stored in a Git repository, only files not excluded by gitignores will be included in the archive.
Initialize the object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
Optional[str]
|
Root directory of the archive. |
None
|
Source code in src/zenml/utils/code_utils.py
47 48 49 50 51 52 53 54 |
|
git_repo: Optional[Repo]
property
Git repository active at the code archive root.
Returns:
Type | Description |
---|---|
Optional[Repo]
|
The git repository if available. |
get_files() -> Dict[str, str]
Gets all regular files that should be included in the archive.
Raises:
Type | Description |
---|---|
RuntimeError
|
If the code archive would not include any files. |
Returns:
Type | Description |
---|---|
Dict[str, str]
|
A dict {path_in_archive: path_on_filesystem} for all regular files |
Dict[str, str]
|
in the archive. |
Source code in src/zenml/utils/code_utils.py
95 96 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 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 |
|
write_archive(output_file: IO[bytes], archive_type: ArchiveType = ArchiveType.TAR_GZ) -> None
Writes an archive of the build context to the given file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_file
|
IO[bytes]
|
The file to write the archive to. |
required |
archive_type
|
ArchiveType
|
The type of archive to create. |
TAR_GZ
|
Source code in src/zenml/utils/code_utils.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
Functions
compute_file_hash(file: IO[bytes]) -> str
Compute a hash of the content of a file.
This function will not seek the file before or after the hash computation. This means that the content will be computed based on the current cursor until the end of the file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
IO[bytes]
|
The file for which to compute the hash. |
required |
Returns:
Type | Description |
---|---|
str
|
A hash of the file content. |
Source code in src/zenml/utils/code_utils.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
download_and_extract_code(code_path: str, extract_dir: str) -> None
Download and extract code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code_path
|
str
|
Path where the code is uploaded. |
required |
extract_dir
|
str
|
Directory where to code should be extracted to. |
required |
Source code in src/zenml/utils/code_utils.py
246 247 248 249 250 251 252 253 254 255 256 257 |
|
download_code_from_artifact_store(code_path: str, artifact_store: BaseArtifactStore) -> None
Download code from the artifact store.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code_path
|
str
|
Path where the code is stored. |
required |
artifact_store
|
BaseArtifactStore
|
The artifact store to use for the download. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the code is stored in an artifact store which is not active. |
Source code in src/zenml/utils/code_utils.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
|
download_notebook_code(artifact_store: BaseArtifactStore, file_name: str, download_path: str) -> None
Download code extracted from a notebook cell.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_store
|
BaseArtifactStore
|
The artifact store from which to download the code. |
required |
file_name
|
str
|
The name of the code file. |
required |
download_path
|
str
|
The local path where the file should be downloaded to. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If no file with the given filename exists in this artifact store. |
Source code in src/zenml/utils/code_utils.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
upload_code_if_necessary(code_archive: CodeArchive) -> str
Upload code to the artifact store if necessary.
This function computes a hash of the code to be uploaded, and if an archive with the same hash already exists it will not re-upload but instead return the path to the existing archive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code_archive
|
CodeArchive
|
The code archive to upload. |
required |
Returns:
Type | Description |
---|---|
str
|
The path where the archived code is uploaded. |
Source code in src/zenml/utils/code_utils.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
upload_notebook_code(artifact_store: BaseArtifactStore, cell_code: str, file_name: str) -> None
Upload code extracted from a notebook cell.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_store
|
BaseArtifactStore
|
The artifact store in which to upload the code. |
required |
cell_code
|
str
|
The notebook cell code. |
required |
file_name
|
str
|
The filename to use for storing the cell code. |
required |
Source code in src/zenml/utils/code_utils.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
Modules
cuda_utils
Utilities for managing GPU memory.
Functions
cleanup_gpu_memory(force: bool = False) -> None
Clean up GPU memory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force
|
bool
|
whether to force the cleanup of GPU memory (must be passed explicitly) |
False
|
Source code in src/zenml/utils/cuda_utils.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
daemon
Utility functions to start/stop daemon processes.
This is only implemented for UNIX systems and therefore doesn't work on Windows. Based on https://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
Functions
check_if_daemon_is_running(pid_file: str) -> bool
Checks whether a daemon process indicated by the PID file is running.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid_file
|
str
|
Path to file containing the PID of the daemon process to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the daemon process is running, otherwise False. |
Source code in src/zenml/utils/daemon.py
340 341 342 343 344 345 346 347 348 349 350 |
|
daemonize(pid_file: str, log_file: Optional[str] = None, working_directory: str = '/') -> Callable[[F], F]
Decorator that executes the decorated function as a daemon process.
Use this decorator to easily transform any function into a daemon process.
For example,
import time
from zenml.utils.daemon import daemonize
@daemonize(log_file='/tmp/daemon.log', pid_file='/tmp/daemon.pid')
def sleeping_daemon(period: int) -> None:
print(f"I'm a daemon! I will sleep for {period} seconds.")
time.sleep(period)
print("Done sleeping, flying away.")
sleeping_daemon(period=30)
print("I'm the daemon's parent!.")
time.sleep(10) # just to prove that the daemon is running in parallel
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid_file
|
str
|
a file where the PID of the daemon process will be stored. |
required |
log_file
|
Optional[str]
|
file where stdout and stderr are redirected for the daemon process. If not supplied, the daemon will be silenced (i.e. have its stdout/stderr redirected to /dev/null). |
None
|
working_directory
|
str
|
working directory for the daemon process, defaults to the root directory. |
'/'
|
Returns:
Type | Description |
---|---|
Callable[[F], F]
|
Decorated function that, when called, will detach from the current |
Callable[[F], F]
|
process and continue executing in the background, as a daemon |
Callable[[F], F]
|
process. |
Source code in src/zenml/utils/daemon.py
41 42 43 44 45 46 47 48 49 50 51 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
get_daemon_pid_if_running(pid_file: str) -> Optional[int]
Read and return the PID value from a PID file.
It does this if the daemon process tracked by the PID file is running.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid_file
|
str
|
Path to file containing the PID of the daemon process to check. |
required |
Returns:
Type | Description |
---|---|
Optional[int]
|
The PID of the daemon process if it is running, otherwise None. |
Source code in src/zenml/utils/daemon.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
run_as_daemon(daemon_function: F, *args: Any, pid_file: str, log_file: Optional[str] = None, working_directory: str = '/', **kwargs: Any) -> None
Runs a function as a daemon process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
daemon_function
|
F
|
The function to run as a daemon. |
required |
pid_file
|
str
|
Path to file in which to store the PID of the daemon process. |
required |
log_file
|
Optional[str]
|
Optional file to which the daemons stdout/stderr will be redirected to. |
None
|
working_directory
|
str
|
Working directory for the daemon process, defaults to the root directory. |
'/'
|
args
|
Any
|
Positional arguments to pass to the daemon function. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the daemon function. |
{}
|
Raises:
Type | Description |
---|---|
FileExistsError
|
If the PID file already exists. |
Source code in src/zenml/utils/daemon.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
stop_daemon(pid_file: str) -> None
Stops a daemon process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid_file
|
str
|
Path to file containing the PID of the daemon process to kill. |
required |
Source code in src/zenml/utils/daemon.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
terminate_children() -> None
Terminate all processes that are children of the currently running process.
Source code in src/zenml/utils/daemon.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
dashboard_utils
Utility class to help with interacting with the dashboard.
Classes
Functions
get_cloud_dashboard_url() -> Optional[str]
Get the base url of the cloud dashboard if the server is a ZenML Pro workspace.
Returns:
Type | Description |
---|---|
Optional[str]
|
The base url of the cloud dashboard. |
Source code in src/zenml/utils/dashboard_utils.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
get_component_url(component: ComponentResponse) -> Optional[str]
Function to get the dashboard URL of a given component model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
component
|
ComponentResponse
|
the response model of the given component. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
the URL to the component if the dashboard is available, else None. |
Source code in src/zenml/utils/dashboard_utils.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
get_model_version_url(model_version: ModelVersionResponse) -> Optional[str]
Function to get the dashboard URL of a given model version.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_version
|
ModelVersionResponse
|
the response model of the given model version. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
the URL to the model version if the dashboard is available, else None. |
Source code in src/zenml/utils/dashboard_utils.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
get_run_url(run: PipelineRunResponse) -> Optional[str]
Function to get the dashboard URL of a given pipeline run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
run
|
PipelineRunResponse
|
the response model of the given pipeline run. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
the URL to the pipeline run if the dashboard is available, else None. |
Source code in src/zenml/utils/dashboard_utils.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
get_server_dashboard_url() -> Optional[str]
Get the base url of the dashboard deployed by the server.
Returns:
Type | Description |
---|---|
Optional[str]
|
The server dashboard url. |
Source code in src/zenml/utils/dashboard_utils.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
get_stack_url(stack: StackResponse) -> Optional[str]
Function to get the dashboard URL of a given stack model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stack
|
StackResponse
|
the response model of the given stack. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
the URL to the stack if the dashboard is available, else None. |
Source code in src/zenml/utils/dashboard_utils.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
show_dashboard(local: bool = False, ngrok_token: Optional[str] = None) -> None
Show the ZenML dashboard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
local
|
bool
|
Whether to show the dashboard for the local server or the one for the active server. |
False
|
ngrok_token
|
Optional[str]
|
An ngrok auth token to use for exposing the ZenML dashboard on a public domain. Primarily used for accessing the dashboard in Colab. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If no server is connected. |
Source code in src/zenml/utils/dashboard_utils.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 |
|
show_dashboard_with_url(url: str) -> None
Show the ZenML dashboard at the given URL.
In native environments, the dashboard is opened in the default browser. In notebook environments, the dashboard is embedded in an iframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
URL of the ZenML dashboard. |
required |
Source code in src/zenml/utils/dashboard_utils.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
Modules
deprecation_utils
Deprecation utilities.
Functions
deprecate_pydantic_attributes(*attributes: Union[str, Tuple[str, str]]) -> Any
Utility function for deprecating and migrating pydantic attributes.
Usage: To use this, you can specify it on any pydantic BaseModel subclass like this (all the deprecated attributes need to be non-required):
from pydantic import BaseModel
from typing import Optional
class MyModel(BaseModel):
deprecated: Optional[int] = None
old_name: Optional[str] = None
new_name: str
_deprecation_validator = deprecate_pydantic_attributes(
"deprecated", ("old_name", "new_name")
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*attributes
|
Union[str, Tuple[str, str]]
|
List of attributes to deprecate. This is either the name of the attribute to deprecate, or a tuple containing the name of the deprecated attribute, and it's replacement. |
()
|
Returns:
Type | Description |
---|---|
Any
|
Pydantic validator class method to be used on BaseModel subclasses |
Any
|
to deprecate or migrate attributes. |
Source code in src/zenml/utils/deprecation_utils.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 93 94 95 96 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 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 166 167 168 169 170 171 172 173 174 175 |
|
dict_utils
Util functions for dictionaries.
Functions
dict_to_bytes(dict_: Dict[str, Any]) -> bytes
Converts a dictionary to bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict_
|
Dict[str, Any]
|
The dictionary to convert. |
required |
Returns:
Type | Description |
---|---|
bytes
|
The dictionary as bytes. |
Source code in src/zenml/utils/dict_utils.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
recursive_update(original: Dict[str, Any], update: Dict[str, Any]) -> Dict[str, Any]
Recursively updates a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original
|
Dict[str, Any]
|
The dictionary to update. |
required |
update
|
Dict[str, Any]
|
The dictionary containing the updated values. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
The updated dictionary. |
Source code in src/zenml/utils/dict_utils.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
remove_none_values(dict_: Dict[str, Any], recursive: bool = False) -> Dict[str, Any]
Removes all key-value pairs with None
value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict_
|
Dict[str, Any]
|
The dict from which the key-value pairs should be removed. |
required |
recursive
|
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
The updated dictionary. |
Source code in src/zenml/utils/dict_utils.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
docker_utils
Utility functions relating to Docker.
Functions
build_image(image_name: str, dockerfile: Union[str, List[str]], build_context_root: Optional[str] = None, dockerignore: Optional[str] = None, extra_files: Sequence[Tuple[str, str]] = (), **custom_build_options: Any) -> None
Builds a docker image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_name
|
str
|
The name to use for the built docker image. |
required |
dockerfile
|
Union[str, List[str]]
|
Path to a dockerfile or a list of strings representing the Dockerfile lines/commands. |
required |
build_context_root
|
Optional[str]
|
Optional path to a directory that will be sent to the Docker daemon as build context. If left empty, the Docker build context will be empty. |
None
|
dockerignore
|
Optional[str]
|
Optional path to a dockerignore file. If no value is
given, the .dockerignore in the root of the build context will be
used if it exists. Otherwise, all files inside |
None
|
extra_files
|
Sequence[Tuple[str, str]]
|
Additional files to include in the build context. The files should be passed as a tuple (filepath_inside_build_context, file_content) and will overwrite existing files in the build context if they share the same path. |
()
|
**custom_build_options
|
Any
|
Additional options that will be passed unmodified to the Docker build call when building the image. You can use this to for example specify build args or a target stage. See https://docker-py.readthedocs.io/en/stable/images.html#docker.models.images.ImageCollection.build for a full list of available options. |
{}
|
Source code in src/zenml/utils/docker_utils.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
check_docker() -> bool
Checks if Docker is installed and running.
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/zenml/utils/docker_utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
get_image_digest(image_name: str) -> Optional[str]
Gets the digest of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_name
|
str
|
Name of the image to get the digest for. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
Returns the repo digest for the given image if there exists exactly one. |
Optional[str]
|
If there are zero or multiple repo digests, returns |
Source code in src/zenml/utils/docker_utils.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
|
is_local_image(image_name: str) -> bool
Returns whether an image was pulled from a registry or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_name
|
str
|
Name of the image to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/zenml/utils/docker_utils.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
|
push_image(image_name: str, docker_client: Optional[DockerClient] = None) -> str
Pushes an image to a container registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_name
|
str
|
The full name (including a tag) of the image to push. |
required |
docker_client
|
Optional[DockerClient]
|
Optional Docker client to use for pushing the image. If no client is given, a new client will be created using the default Docker environment. |
None
|
Returns:
Type | Description |
---|---|
str
|
The Docker repository digest of the pushed image. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If fetching the repository digest of the image failed. |
Source code in src/zenml/utils/docker_utils.py
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 275 276 277 278 279 280 281 282 283 284 285 286 |
|
tag_image(image_name: str, target: str) -> None
Tags an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_name
|
str
|
The name of the image to tag. |
required |
target
|
str
|
The full target name including a tag. |
required |
Source code in src/zenml/utils/docker_utils.py
289 290 291 292 293 294 295 296 297 298 |
|
Modules
downloaded_repository_context
Downloaded code repository.
Classes
enum_utils
Util functions for enums.
Classes
StrEnum
Bases: str
, Enum
Base enum type for string enum values.
names() -> List[str]
classmethod
Get all enum names as a list of strings.
Returns:
Type | Description |
---|---|
List[str]
|
A list of all enum names. |
Source code in src/zenml/utils/enum_utils.py
31 32 33 34 35 36 37 38 |
|
values() -> List[str]
classmethod
Get all enum values as a list of strings.
Returns:
Type | Description |
---|---|
List[str]
|
A list of all enum values. |
Source code in src/zenml/utils/enum_utils.py
40 41 42 43 44 45 46 47 |
|
env_utils
Utility functions for handling environment variables.
Functions
reconstruct_environment_variables(env: Optional[Dict[str, str]] = None) -> None
Reconstruct environment variables that were split into chunks.
Reconstructs the environment variables with values that were split into individual chunks because they were too large. The input environment variables are modified in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env
|
Optional[Dict[str, str]]
|
Input environment variables dictionary. If not supplied, the OS environment variables are used. |
None
|
Source code in src/zenml/utils/env_utils.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
split_environment_variables(size_limit: int, env: Optional[Dict[str, str]] = None) -> None
Split long environment variables into chunks.
Splits the input environment variables with values that exceed the supplied maximum length into individual components. The input environment variables are modified in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size_limit
|
int
|
Maximum length of an environment variable value. |
required |
env
|
Optional[Dict[str, str]]
|
Input environment variables dictionary. If not supplied, the OS environment variables are used. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If an environment variable value is too large and requires more than 10 chunks. |
Source code in src/zenml/utils/env_utils.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
substitute_env_variable_placeholders(value: V, raise_when_missing: bool = True) -> V
Substitute environment variable placeholders in an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
V
|
The object in which to substitute the placeholders. |
required |
raise_when_missing
|
bool
|
If True, an exception will be raised when an environment variable is missing. Otherwise, a warning will be logged instead. |
True
|
Returns:
Type | Description |
---|---|
V
|
The object with placeholders substituted. |
Source code in src/zenml/utils/env_utils.py
113 114 115 116 117 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 |
|
Modules
filesync_model
Filesync utils for ZenML.
Classes
FileSyncModel
Bases: BaseModel
Pydantic model synchronized with a configuration file.
Use this class as a base Pydantic model that is automatically synchronized with a configuration file on disk.
This class overrides the setattr and getattr magic methods to ensure that the FileSyncModel instance acts as an in-memory cache of the information stored in the associated configuration file.
config_validator(data: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo) -> FileSyncModel
classmethod
Wrap model validator to infer the config_file during initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Any
|
The raw data that is provided before the validation. |
required |
handler
|
ValidatorFunctionWrapHandler
|
The actual validation function pydantic would use for the built-in validation function. |
required |
info
|
ValidationInfo
|
The context information during the execution of this validation function. |
required |
Returns:
Type | Description |
---|---|
FileSyncModel
|
the actual instance after the validation |
Raises:
Type | Description |
---|---|
ValidationError
|
if you try to validate through a JSON string. You need to provide a config_file path when you create a FileSyncModel. |
AssertionError
|
if the raw input does not include a config_file path for the configuration file. |
Source code in src/zenml/utils/filesync_model.py
48 49 50 51 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
load_config() -> None
Loads the model from the configuration file on disk.
Source code in src/zenml/utils/filesync_model.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
write_config() -> None
Writes the model to the configuration file.
Source code in src/zenml/utils/filesync_model.py
137 138 139 140 |
|
Functions
Modules
function_utils
Utility functions for python functions.
Functions
create_cli_wrapped_script(func: F, flavor: str = 'accelerate') -> Iterator[Tuple[Path, Path]]
Create a script with the CLI-wrapped function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
F
|
The function to use. |
required |
flavor
|
str
|
The flavor to use. |
'accelerate'
|
Yields:
Type | Description |
---|---|
Tuple[Path, Path]
|
The paths of the script and the output. |
Raises:
Type | Description |
---|---|
ValueError
|
If the function is not defined in a module. |
Source code in src/zenml/utils/function_utils.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
git_utils
Utility function to clone a Git repository.
Functions
clone_git_repository(url: str, to_path: str, branch: Optional[str] = None, commit: Optional[str] = None) -> Repo
Clone a Git repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
URL of the repository to clone. |
required |
to_path
|
str
|
Path to clone the repository to. |
required |
branch
|
Optional[str]
|
Branch to clone. Defaults to "main". |
None
|
commit
|
Optional[str]
|
Commit to checkout. If specified, the branch argument is ignored. |
None
|
Returns:
Type | Description |
---|---|
Repo
|
The cloned repository. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the repository could not be cloned. |
Source code in src/zenml/utils/git_utils.py
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 50 51 52 53 54 55 56 57 58 59 60 61 |
|
integration_utils
Util functions for integration.
Functions
parse_requirement(requirement: str) -> Tuple[Optional[str], Optional[str]]
Parse a requirement string into name and extras.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
requirement
|
str
|
A requirement string. |
required |
Returns:
Type | Description |
---|---|
Tuple[Optional[str], Optional[str]]
|
A tuple of name and extras. |
Source code in src/zenml/utils/integration_utils.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
io_utils
Various utility functions for the io module.
Functions
copy_dir(source_dir: str, destination_dir: str, overwrite: bool = False) -> None
Copies dir from source to destination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_dir
|
str
|
Path to copy from. |
required |
destination_dir
|
str
|
Path to copy to. |
required |
overwrite
|
bool
|
Boolean. If false, function throws an error before overwrite. |
False
|
Source code in src/zenml/utils/io_utils.py
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 |
|
create_dir_if_not_exists(dir_path: str) -> None
Creates directory if it does not exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path
|
str
|
Local path in filesystem. |
required |
Source code in src/zenml/utils/io_utils.py
174 175 176 177 178 179 180 181 |
|
create_dir_recursive_if_not_exists(dir_path: str) -> None
Creates directory recursively if it does not exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path
|
str
|
Local path in filesystem. |
required |
Source code in src/zenml/utils/io_utils.py
184 185 186 187 188 189 190 191 |
|
create_file_if_not_exists(file_path: str, file_contents: str = '{}') -> None
Creates file if it does not exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Local path in filesystem. |
required |
file_contents
|
str
|
Contents of file. |
'{}'
|
Source code in src/zenml/utils/io_utils.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
find_files(dir_path: PathType, pattern: str) -> Iterable[str]
Find files in a directory that match pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path
|
PathType
|
The path to directory. |
required |
pattern
|
str
|
pattern like *.png. |
required |
Yields:
Type | Description |
---|---|
Iterable[str]
|
All matching filenames in the directory. |
Source code in src/zenml/utils/io_utils.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
get_global_config_directory() -> str
Gets the global config directory for ZenML.
Returns:
Type | Description |
---|---|
str
|
The global config directory for ZenML. |
Source code in src/zenml/utils/io_utils.py
53 54 55 56 57 58 59 60 61 62 |
|
get_grandparent(dir_path: str) -> str
Get grandparent of dir.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path
|
str
|
The path to directory. |
required |
Returns:
Type | Description |
---|---|
str
|
The input paths parents parent. |
Raises:
Type | Description |
---|---|
ValueError
|
If dir_path does not exist. |
Source code in src/zenml/utils/io_utils.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
get_parent(dir_path: str) -> str
Get parent of dir.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path
|
str
|
The path to directory. |
required |
Returns:
Type | Description |
---|---|
str
|
Parent (stem) of the dir as a string. |
Raises:
Type | Description |
---|---|
ValueError
|
If dir_path does not exist. |
Source code in src/zenml/utils/io_utils.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
is_remote(path: str) -> bool
Returns True if path exists remotely.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Any path as a string. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if remote path, else False. |
Source code in src/zenml/utils/io_utils.py
146 147 148 149 150 151 152 153 154 155 |
|
is_root(path: str) -> bool
Returns true if path has no parent in local filesystem.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Local path in filesystem. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if root, else False. |
Source code in src/zenml/utils/io_utils.py
41 42 43 44 45 46 47 48 49 50 |
|
move(source: str, destination: str, overwrite: bool = False) -> None
Moves dir or file from source to destination. Can be used to rename.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
str
|
Local path to copy from. |
required |
destination
|
str
|
Local path to copy to. |
required |
overwrite
|
bool
|
boolean, if false, then throws an error before overwrite. |
False
|
Source code in src/zenml/utils/io_utils.py
208 209 210 211 212 213 214 215 216 |
|
read_file_contents_as_string(file_path: str) -> str
Reads contents of file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to file. |
required |
Returns:
Type | Description |
---|---|
str
|
Contents of file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If file does not exist. |
Source code in src/zenml/utils/io_utils.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
resolve_relative_path(path: str) -> str
Takes relative path and resolves it absolutely.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Local path in filesystem. |
required |
Returns:
Type | Description |
---|---|
str
|
Resolved path. |
Source code in src/zenml/utils/io_utils.py
194 195 196 197 198 199 200 201 202 203 204 205 |
|
write_file_contents_as_string(file_path: str, content: str) -> None
Writes contents of file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to file. |
required |
content
|
str
|
Contents of file. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If content is not of type str. |
Source code in src/zenml/utils/io_utils.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
json_utils
Carried over version of some functions from the pydantic v1 json module.
Check out the latest version here: https://github.com/pydantic/pydantic/blob/v1.10.15/pydantic/json.py
Functions
decimal_encoder(dec_value: Decimal) -> Union[int, float]
Encodes a Decimal as int of there's no exponent, otherwise float.
This is useful when we use ConstrainedDecimal to represent Numeric(x,0) where an integer (but not int typed) is used. Encoding this as a float results in failed round-tripping between encode and parse. Our ID type is a prime example of this.
decimal_encoder(Decimal("1.0")) 1.0
decimal_encoder(Decimal("1")) 1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dec_value
|
Decimal
|
The input Decimal value |
required |
Returns:
Type | Description |
---|---|
Union[int, float]
|
the encoded result |
Source code in src/zenml/utils/json_utils.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
isoformat(obj: Union[datetime.date, datetime.time]) -> str
Function to convert a datetime into iso format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Union[date, time]
|
input datetime |
required |
Returns:
Type | Description |
---|---|
str
|
the corresponding time in iso format. |
Source code in src/zenml/utils/json_utils.py
44 45 46 47 48 49 50 51 52 53 |
|
materializer_utils
Util functions for materializers.
Classes
Functions
select_materializer(data_type: Type[Any], materializer_classes: Sequence[Type[BaseMaterializer]]) -> Type[BaseMaterializer]
Select a materializer for a given data type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_type
|
Type[Any]
|
The data type for which to select the materializer. |
required |
materializer_classes
|
Sequence[Type[BaseMaterializer]]
|
Available materializer classes. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If no materializer can handle the given data type. |
Returns:
Type | Description |
---|---|
Type[BaseMaterializer]
|
The first materializer that can handle the given data type. |
Source code in src/zenml/utils/materializer_utils.py
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 50 |
|
metadata_utils
Utility functions to handle metadata for ZenML entities.
Classes
Functions
log_metadata(metadata: Dict[str, MetadataType], step_id: Optional[UUID] = None, step_name: Optional[str] = None, run_id_name_or_prefix: Optional[Union[UUID, str]] = None, artifact_version_id: Optional[UUID] = None, artifact_name: Optional[str] = None, artifact_version: Optional[str] = None, infer_artifact: bool = False, model_version_id: Optional[UUID] = None, model_name: Optional[str] = None, model_version: Optional[Union[ModelStages, int, str]] = None, infer_model: bool = False) -> None
log_metadata(metadata: Dict[str, MetadataType]) -> None
log_metadata(
*, metadata: Dict[str, MetadataType], step_id: UUID
) -> None
log_metadata(
*,
metadata: Dict[str, MetadataType],
step_name: str,
run_id_name_or_prefix: Union[UUID, str],
) -> None
log_metadata(
*,
metadata: Dict[str, MetadataType],
run_id_name_or_prefix: Union[UUID, str],
) -> None
log_metadata(
*,
metadata: Dict[str, MetadataType],
artifact_version_id: UUID,
) -> None
log_metadata(
*,
metadata: Dict[str, MetadataType],
artifact_name: str,
artifact_version: Optional[str] = None,
) -> None
log_metadata(
*,
metadata: Dict[str, MetadataType],
infer_artifact: bool = False,
artifact_name: Optional[str] = None,
) -> None
log_metadata(
*,
metadata: Dict[str, MetadataType],
model_version_id: UUID,
) -> None
log_metadata(
*,
metadata: Dict[str, MetadataType],
model_name: str,
model_version: Union[ModelStages, int, str],
) -> None
log_metadata(
*,
metadata: Dict[str, MetadataType],
infer_model: bool = False,
) -> None
Logs metadata for various resource types in a generalized way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metadata
|
Dict[str, MetadataType]
|
The metadata to log. |
required |
step_id
|
Optional[UUID]
|
The ID of the step. |
None
|
step_name
|
Optional[str]
|
The name of the step. |
None
|
run_id_name_or_prefix
|
Optional[Union[UUID, str]]
|
The id, name or prefix of the run |
None
|
artifact_version_id
|
Optional[UUID]
|
The ID of the artifact version |
None
|
artifact_name
|
Optional[str]
|
The name of the artifact. |
None
|
artifact_version
|
Optional[str]
|
The version of the artifact. |
None
|
infer_artifact
|
bool
|
Flag deciding whether the artifact version should be inferred from the step context. |
False
|
model_version_id
|
Optional[UUID]
|
The ID of the model version. |
None
|
model_name
|
Optional[str]
|
The name of the model. |
None
|
model_version
|
Optional[Union[ModelStages, int, str]]
|
The version of the model. |
None
|
infer_model
|
bool
|
Flag deciding whether the model version should be inferred from the step context. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
If no identifiers are provided and the function is not called from within a step. |
Source code in src/zenml/utils/metadata_utils.py
112 113 114 115 116 117 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|
networking_utils
Utility functions for networking.
Classes
Functions
find_available_port() -> int
Finds a local random unoccupied TCP port.
Returns:
Type | Description |
---|---|
int
|
A random unoccupied TCP port. |
Source code in src/zenml/utils/networking_utils.py
57 58 59 60 61 62 63 64 65 66 67 |
|
get_or_create_ngrok_tunnel(ngrok_token: str, port: int) -> str
Get or create an ngrok tunnel at the given port.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ngrok_token
|
str
|
The ngrok auth token. |
required |
port
|
int
|
The port to tunnel. |
required |
Returns:
Type | Description |
---|---|
str
|
The public URL of the ngrok tunnel. |
Raises:
Type | Description |
---|---|
ImportError
|
If the |
Source code in src/zenml/utils/networking_utils.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
port_available(port: int, address: str = '127.0.0.1') -> bool
Checks if a local port is available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
port
|
int
|
TCP port number |
required |
address
|
str
|
IP address on the local machine |
'127.0.0.1'
|
Returns:
Type | Description |
---|---|
bool
|
True if the port is available, otherwise False |
Source code in src/zenml/utils/networking_utils.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
port_is_open(hostname: str, port: int) -> bool
Check if a TCP port is open on a remote host.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hostname
|
str
|
hostname of the remote machine |
required |
port
|
int
|
TCP port number |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the port is open, False otherwise |
Source code in src/zenml/utils/networking_utils.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
replace_internal_hostname_with_localhost(hostname: str) -> str
Replaces an internal Docker or K3D hostname with localhost.
Localhost URLs that are directly accessible on the host machine are not
accessible from within a Docker or K3D container running on that same
machine, but there are special hostnames featured by both Docker
(host.docker.internal
) and K3D (host.k3d.internal
) that can be used to
access host services from within the containers.
Use this method to replace one of these special hostnames with localhost if used outside a container or in a container where special hostnames are not available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hostname
|
str
|
The hostname to replace. |
required |
Returns:
Type | Description |
---|---|
str
|
The original or replaced hostname. |
Source code in src/zenml/utils/networking_utils.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
replace_localhost_with_internal_hostname(url: str) -> str
Replaces the localhost with an internal Docker or K3D hostname in a given URL.
Localhost URLs that are directly accessible on the host machine are not
accessible from within a Docker or K3D container running on that same
machine, but there are special hostnames featured by both Docker
(host.docker.internal
) and K3D (host.k3d.internal
) that can be used to
access host services from within the containers.
Use this method to attempt to replace localhost
in a URL with one of these
special hostnames, if they are available inside a container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The URL to update. |
required |
Returns:
Type | Description |
---|---|
str
|
The updated URL. |
Source code in src/zenml/utils/networking_utils.py
115 116 117 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 |
|
scan_for_available_port(start: int = SCAN_PORT_RANGE[0], stop: int = SCAN_PORT_RANGE[1]) -> Optional[int]
Scan the local network for an available port in the given range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start
|
int
|
the beginning of the port range value to scan |
SCAN_PORT_RANGE[0]
|
stop
|
int
|
the (inclusive) end of the port range value to scan |
SCAN_PORT_RANGE[1]
|
Returns:
Type | Description |
---|---|
Optional[int]
|
The first available port in the given range, or None if no available |
Optional[int]
|
port is found. |
Source code in src/zenml/utils/networking_utils.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
notebook_utils
Notebook utilities.
Classes
Functions
compute_cell_replacement_module_name(cell_code: str) -> str
Compute the replacement module name for a given cell code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cell_code
|
str
|
The code of the notebook cell. |
required |
Returns:
Type | Description |
---|---|
str
|
The replacement module name. |
Source code in src/zenml/utils/notebook_utils.py
126 127 128 129 130 131 132 133 134 135 136 |
|
enable_notebook_code_extraction(_obj: Optional[AnyObject] = None) -> Union[AnyObject, Callable[[AnyObject], AnyObject]]
Decorator to enable code extraction from notebooks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_obj
|
Optional[AnyObject]
|
The class or function for which to enable code extraction. |
None
|
Returns:
Type | Description |
---|---|
Union[AnyObject, Callable[[AnyObject], AnyObject]]
|
The decorated class or function. |
Source code in src/zenml/utils/notebook_utils.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
get_active_notebook_cell_code() -> Optional[str]
Get the code of the currently active notebook cell.
Returns:
Type | Description |
---|---|
Optional[str]
|
The code of the currently active notebook cell. |
Source code in src/zenml/utils/notebook_utils.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
is_defined_in_notebook_cell(obj: Any) -> bool
Check whether an object is defined in a notebook cell.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The object to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the object is defined in a notebook cell. |
Source code in src/zenml/utils/notebook_utils.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
load_notebook_cell_code(obj: Any) -> Optional[str]
Load the notebook cell code for an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The object for which to load the cell code. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
The notebook cell code if it was saved. |
Source code in src/zenml/utils/notebook_utils.py
98 99 100 101 102 103 104 105 106 107 |
|
try_to_save_notebook_cell_code(obj: Any) -> None
Try to save the notebook cell code for an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The object for which to save the notebook cell code. |
required |
Source code in src/zenml/utils/notebook_utils.py
83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
warn_about_notebook_cell_magic_commands(cell_code: str) -> None
Warn about magic commands in the cell code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cell_code
|
str
|
The cell code. |
required |
Source code in src/zenml/utils/notebook_utils.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
package_utils
Utility functions for the package.
Functions
clean_requirements(requirements: List[str]) -> List[str]
Clean requirements list from redundant requirements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
requirements
|
List[str]
|
List of requirements. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
Cleaned list of requirements |
Raises:
Type | Description |
---|---|
TypeError
|
If input is not a list |
ValueError
|
If any element in the list is not a string |
Source code in src/zenml/utils/package_utils.py
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 |
|
is_latest_zenml_version() -> bool
Checks if the currently running ZenML package is on the latest version.
Returns:
Type | Description |
---|---|
bool
|
True in case the current running zenml code is the latest available version on PYPI, otherwise False. |
Raises:
Type | Description |
---|---|
RuntimeError
|
In case something goe wrong |
Source code in src/zenml/utils/package_utils.py
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 50 51 52 |
|
pagination_utils
Pagination utilities.
Classes
Functions
depaginate(list_method: Callable[..., Page[AnyResponse]], **kwargs: Any) -> List[AnyResponse]
Depaginate the results from a client or store method that returns pages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
list_method
|
Callable[..., Page[AnyResponse]]
|
The list method to depaginate. |
required |
**kwargs
|
Any
|
Arguments for the list method. |
{}
|
Returns:
Type | Description |
---|---|
List[AnyResponse]
|
A list of the corresponding Response Models. |
Source code in src/zenml/utils/pagination_utils.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
pipeline_docker_image_builder
Implementation of Docker image builds to run ZenML pipelines.
Classes
PipelineDockerImageBuilder
Builds Docker images to run a ZenML pipeline.
build_docker_image(docker_settings: DockerSettings, tag: str, stack: Stack, include_files: bool, entrypoint: Optional[str] = None, extra_files: Optional[Dict[str, str]] = None, code_repository: Optional[BaseCodeRepository] = None) -> Tuple[str, Optional[str], Optional[str]]
Builds (and optionally pushes) a Docker image to run a pipeline.
Use the image name returned by this method whenever you need to uniquely reference the pushed image in order to pull or run it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docker_settings
|
DockerSettings
|
The settings for the image build. |
required |
tag
|
str
|
The tag to use for the image. |
required |
stack
|
Stack
|
The stack on which the pipeline will be deployed. |
required |
include_files
|
bool
|
Whether to include files in the build context. |
required |
entrypoint
|
Optional[str]
|
Entrypoint to use for the final image. If left empty, no entrypoint will be included in the image. |
None
|
extra_files
|
Optional[Dict[str, str]]
|
Extra files to add to the build context. Keys are the path inside the build context, values are either the file content or a file path. |
None
|
code_repository
|
Optional[BaseCodeRepository]
|
The code repository from which files will be downloaded. |
None
|
Returns:
Type | Description |
---|---|
str
|
A tuple (image_digest, dockerfile, requirements): |
Optional[str]
|
|
Optional[str]
|
the image was pushed or is just stored locally. |
Tuple[str, Optional[str], Optional[str]]
|
|
Tuple[str, Optional[str], Optional[str]]
|
build the image. |
Tuple[str, Optional[str], Optional[str]]
|
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If the stack does not contain an image builder. |
ValueError
|
If no Dockerfile and/or custom parent image is specified and the Docker configuration doesn't require an image build. |
ValueError
|
If the specified Dockerfile does not exist. |
Source code in src/zenml/utils/pipeline_docker_image_builder.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
gather_requirements_files(docker_settings: DockerSettings, stack: Stack, code_repository: Optional[BaseCodeRepository] = None, log: bool = True) -> List[Tuple[str, str, List[str]]]
staticmethod
Gathers and/or generates pip requirements files.
This method is called in PipelineDockerImageBuilder.build_docker_image
but it is also called by other parts of the codebase, e.g. the
AzureMLStepOperator
, which needs to upload the requirements files to
AzureML where the step image is then built.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docker_settings
|
DockerSettings
|
Docker settings that specifies which requirements to install. |
required |
stack
|
Stack
|
The stack on which the pipeline will run. |
required |
code_repository
|
Optional[BaseCodeRepository]
|
The code repository from which files will be downloaded. |
None
|
log
|
bool
|
If True, will log the requirements. |
True
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If the command to export the local python packages failed. |
FileNotFoundError
|
If the specified requirements file does not exist. |
Returns:
Type | Description |
---|---|
List[Tuple[str, str, List[str]]]
|
List of tuples (filename, file_content, pip_options) of all |
List[Tuple[str, str, List[str]]]
|
requirements files. |
List[Tuple[str, str, List[str]]]
|
The files will be in the following order: |
List[Tuple[str, str, List[str]]]
|
|
List[Tuple[str, str, List[str]]]
|
|
List[Tuple[str, str, List[str]]]
|
|
List[Tuple[str, str, List[str]]]
|
|
Source code in src/zenml/utils/pipeline_docker_image_builder.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
|
Functions
Modules
proxy_utils
Proxy design pattern utils.
Functions
make_proxy_class(interface: Type[ABC], attribute: str) -> Callable[[C], C]
Proxy class decorator.
Use this decorator to transform the decorated class into a proxy that
forwards all calls defined in the interface
interface to the attribute
class attribute that implements the same interface.
This class is useful in cases where you need to have a base class that acts as a proxy or facade for one or more other classes. Both the decorated class and the class attribute must inherit from the same ABC interface for this to work. Only regular methods are supported, not class methods or attributes.
Example: Let's say you have an interface called BodyBuilder
, a base class
called FatBob
and another class called BigJim
. BigJim
implements the
BodyBuilder
interface, but FatBob
does not. And let's say you want
FatBob
to look as if it implements the BodyBuilder
interface, but in
fact it just forwards all calls to BigJim
. You could do this:
from abc import ABC, abstractmethod
class BodyBuilder(ABC):
@abstractmethod
def build_body(self):
pass
class BigJim(BodyBuilder):
def build_body(self):
print("Looks fit!")
class FatBob(BodyBuilder)
def __init__(self):
self.big_jim = BigJim()
def build_body(self):
self.big_jim.build_body()
fat_bob = FatBob()
fat_bob.build_body()
But this leads to a lot of boilerplate code with bigger interfaces and makes everything harder to maintain. This is where the proxy class decorator comes in handy. Here's how to use it:
from zenml.utils.proxy_utils import make_proxy_class
from typing import Optional
@make_proxy_class(BodyBuilder, "big_jim")
class FatBob(BodyBuilder)
big_jim: Optional[BodyBuilder] = None
def __init__(self):
self.big_jim = BigJim()
fat_bob = FatBob()
fat_bob.build_body()
This is the same as implementing FatBob to call BigJim explicitly, but it has the advantage that you don't need to write a lot of boilerplate code of modify the FatBob class every time you change something in the BodyBuilder interface.
This proxy decorator also allows to extend classes dynamically at runtime:
if the attribute
class attribute is set to None, the proxy class
will assume that the interface is not implemented by the class and will
raise a NotImplementedError:
@make_proxy_class(BodyBuilder, "big_jim")
class FatBob(BodyBuilder)
big_jim: Optional[BodyBuilder] = None
def __init__(self):
self.big_jim = None
fat_bob = FatBob()
# Raises NotImplementedError, class not extended yet:
fat_bob.build_body()
fat_bob.big_jim = BigJim()
# Now it works:
fat_bob.build_body()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interface
|
Type[ABC]
|
The interface to implement. |
required |
attribute
|
str
|
The attribute of the base class to forward calls to. |
required |
Returns:
Type | Description |
---|---|
Callable[[C], C]
|
The proxy class. |
Source code in src/zenml/utils/proxy_utils.py
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 50 51 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 93 94 95 96 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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
pydantic_utils
Utilities for pydantic models.
Classes
TemplateGenerator(instance_or_class: Union[BaseModel, Type[BaseModel]])
Class to generate templates for pydantic models or classes.
Initializes the template generator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance_or_class
|
Union[BaseModel, Type[BaseModel]]
|
The pydantic model or model class for which to generate a template. |
required |
Source code in src/zenml/utils/pydantic_utils.py
85 86 87 88 89 90 91 92 93 94 |
|
run() -> Dict[str, Any]
Generates the template.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
The template dictionary. |
Source code in src/zenml/utils/pydantic_utils.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
YAMLSerializationMixin
Bases: BaseModel
Class to serialize/deserialize pydantic models to/from YAML.
from_yaml(path: str) -> M
classmethod
Creates an instance from a YAML file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to a YAML file. |
required |
Returns:
Type | Description |
---|---|
M
|
The model instance. |
Source code in src/zenml/utils/pydantic_utils.py
223 224 225 226 227 228 229 230 231 232 233 234 |
|
yaml(sort_keys: bool = False, **kwargs: Any) -> str
YAML string representation..
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sort_keys
|
bool
|
Whether to sort the keys in the YAML representation. |
False
|
**kwargs
|
Any
|
Kwargs to pass to the pydantic model_dump(...) method. |
{}
|
Returns:
Type | Description |
---|---|
str
|
YAML string representation. |
Source code in src/zenml/utils/pydantic_utils.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
Functions
before_validator_handler(method: Callable[..., Any]) -> Callable[[Any, Any, Any], Any]
Decorator to handle the raw input data for pydantic model validators.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
Callable[..., Any]
|
the class method with the actual validation logic. |
required |
Returns:
Type | Description |
---|---|
Callable[[Any, Any, Any], Any]
|
the validator method |
Source code in src/zenml/utils/pydantic_utils.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
|
has_validators(pydantic_class: Type[BaseModel], field_name: Optional[str] = None) -> bool
Function to check if a Pydantic model or a pydantic field has validators.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pydantic_class
|
Type[BaseModel]
|
The class defining the pydantic model. |
required |
field_name
|
Optional[str]
|
Optional, field info. If specified, this function will focus on a singular field within the class. If not specified, it will check model validators. |
None
|
Returns:
Type | Description |
---|---|
bool
|
Whether the specified field or class has a validator or not. |
Source code in src/zenml/utils/pydantic_utils.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
|
model_validator_data_handler(raw_data: Any, base_class: Type[BaseModel], validation_info: ValidationInfo) -> Dict[str, Any]
Utility function to parse raw input data of varying types to a dict.
With the change to pydantic v2, validators which operate with "before" (or previously known as the "pre" parameter) are getting "Any" types of raw input instead of a "Dict[str, Any]" as before. Depending on the use-case, this can create conflicts after the migration and this function will be used as a helper function to handle different types of raw input data.
A code snippet to showcase how the behavior changes. The "before" validator prints the type of the input:
class Base(BaseModel):
a: int = 3
class MyClass(Base):
@model_validator(mode="before")
@classmethod
def before_validator(cls, data: Any) -> Any:
print(type(data))
return {}
one = MyClass() # prints "<class 'dict'>"
MyClass.model_validate(one) # prints NOTHING, it is already validated
MyClass.model_validate("asdf") # prints "<class 'str'>", fails without the modified return.
MyClass.model_validate(RandomClass()) # prints "<class 'RandomClass'>", fails without the modified return.
MyClass.model_validate(Base()) # prints "<class 'Base'>", fails without the modified return.
MyClass.model_validate_json(json.dumps("aria")) # prints "<class 'str'>", fails without the modified return.
MyClass.model_validate_json(json.dumps([1])) # prints "<class 'list'>", fails without the modified return.
MyClass.model_validate_json(one.model_dump_json()) # prints "<class 'dict'>"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_data
|
Any
|
The raw data passed to the validator, can be "Any" type. |
required |
base_class
|
Type[BaseModel]
|
The class that the validator belongs to |
required |
validation_info
|
ValidationInfo
|
Extra information about the validation process. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
if the type of the data is not processable. |
ValueError
|
in case of an unknown validation mode. |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
A dictionary which will be passed to the eventual validator of pydantic. |
Source code in src/zenml/utils/pydantic_utils.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
|
update_model(original: M, update: Union[BaseModel, Dict[str, Any]], recursive: bool = True, exclude_none: bool = False) -> M
Updates a pydantic model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original
|
M
|
The model to update. |
required |
update
|
Union[BaseModel, Dict[str, Any]]
|
The update values. |
required |
recursive
|
bool
|
If |
True
|
exclude_none
|
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
M
|
The updated model. |
Source code in src/zenml/utils/pydantic_utils.py
44 45 46 47 48 49 50 51 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 |
|
validate_function_args(__func: Callable[..., Any], __config: Optional[ConfigDict], *args: Any, **kwargs: Any) -> Dict[str, Any]
Validates arguments passed to a function.
This function validates that all arguments to call the function exist and that the types match.
It raises a pydantic.ValidationError if the validation fails.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__func
|
Callable[..., Any]
|
The function for which the arguments are passed. |
required |
__config
|
Optional[ConfigDict]
|
The pydantic config for the underlying model that is created to validate the types of the arguments. |
required |
*args
|
Any
|
Function arguments. |
()
|
**kwargs
|
Any
|
Function keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
The validated arguments. |
Source code in src/zenml/utils/pydantic_utils.py
237 238 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 275 276 277 278 279 280 281 282 283 284 |
|
Modules
requirements_utils
Requirement utils.
Classes
Functions
get_requirements_for_component(component: ComponentResponse, python_version: Optional[str] = None) -> Tuple[List[str], List[str]]
Get requirements for a component model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
component
|
ComponentResponse
|
The component for which to get the requirements. |
required |
python_version
|
Optional[str]
|
The Python version to use for the requirements. |
None
|
Returns:
Type | Description |
---|---|
Tuple[List[str], List[str]]
|
Tuple of PyPI and APT requirements of the component. |
Source code in src/zenml/utils/requirements_utils.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
get_requirements_for_stack(stack: StackResponse, python_version: Optional[str] = None) -> Tuple[List[str], List[str]]
Get requirements for a stack model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stack
|
StackResponse
|
The stack for which to get the requirements. |
required |
python_version
|
Optional[str]
|
The Python version to use for the requirements. |
None
|
Returns:
Type | Description |
---|---|
Tuple[List[str], List[str]]
|
Tuple of PyPI and APT requirements of the stack. |
Source code in src/zenml/utils/requirements_utils.py
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 50 51 52 53 54 55 |
|
secret_utils
Utility functions for secrets and secret references.
Classes
SecretReference
Bases: NamedTuple
Class representing a secret reference.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The secret name. |
key |
str
|
The secret key. |
Functions
ClearTextField(*args: Any, **kwargs: Any) -> Any
Marks a pydantic field to prevent secret references.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Positional arguments which will be forwarded
to |
()
|
**kwargs
|
Any
|
Keyword arguments which will be forwarded to
|
{}
|
Returns:
Type | Description |
---|---|
Any
|
Pydantic field info. |
Source code in src/zenml/utils/secret_utils.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
SecretField(*args: Any, **kwargs: Any) -> Any
Marks a pydantic field as something containing sensitive information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Positional arguments which will be forwarded
to |
()
|
**kwargs
|
Any
|
Keyword arguments which will be forwarded to
|
{}
|
Returns:
Type | Description |
---|---|
Any
|
Pydantic field info. |
Source code in src/zenml/utils/secret_utils.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
is_clear_text_field(field: FieldInfo) -> bool
Returns whether a pydantic field prevents secret references or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field
|
FieldInfo
|
The field to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/zenml/utils/secret_utils.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
is_secret_field(field: FieldInfo) -> bool
Returns whether a pydantic field contains sensitive information or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field
|
FieldInfo
|
The field to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/zenml/utils/secret_utils.py
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 |
|
is_secret_reference(value: Any) -> bool
Checks whether any value is a secret reference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
The value to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/zenml/utils/secret_utils.py
43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
parse_secret_reference(reference: str) -> SecretReference
Parses a secret reference.
This function assumes the input string is a valid secret reference and does not perform any additional checks. If you pass an invalid secret reference here, this will most likely crash.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference
|
str
|
The string representing a valid secret reference. |
required |
Returns:
Type | Description |
---|---|
SecretReference
|
The parsed secret reference. |
Source code in src/zenml/utils/secret_utils.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
server_utils
Utility functions for ZenML servers.
Classes
Functions
connected_to_local_server() -> bool
Check if the client is connected to a local server.
Returns:
Type | Description |
---|---|
bool
|
True if the client is connected to a local server, False otherwise. |
Source code in src/zenml/utils/server_utils.py
43 44 45 46 47 48 49 50 51 52 |
|
get_local_server() -> Optional[LocalServerDeployment]
Get the active local server.
Call this function to retrieve the local server deployed on this machine.
Returns:
Type | Description |
---|---|
Optional[LocalServerDeployment]
|
The local server deployment or None, if no local server deployment was |
Optional[LocalServerDeployment]
|
found. |
Source code in src/zenml/utils/server_utils.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
settings_utils
Utility functions for ZenML settings.
Classes
Functions
get_flavor_setting_key(flavor: Flavor) -> str
Gets the setting key for a flavor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
flavor
|
Flavor
|
The flavor for which to get the key. |
required |
Returns:
Type | Description |
---|---|
str
|
The setting key for the flavor. |
Source code in src/zenml/utils/settings_utils.py
44 45 46 47 48 49 50 51 52 53 |
|
get_general_settings() -> Dict[str, Type[BaseSettings]]
Returns all general settings.
Returns:
Type | Description |
---|---|
Dict[str, Type[BaseSettings]]
|
Dictionary mapping general settings keys to their type. |
Source code in src/zenml/utils/settings_utils.py
123 124 125 126 127 128 129 130 131 132 133 134 |
|
get_stack_component_for_settings_key(key: str, stack: Stack) -> StackComponent
Gets the stack component of a stack for a given settings key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The settings key for which to get the component. |
required |
stack
|
Stack
|
The stack from which to get the component. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the key is invalid or the stack does not contain a component of the correct flavor. |
Returns:
Type | Description |
---|---|
StackComponent
|
The stack component. |
Source code in src/zenml/utils/settings_utils.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
get_stack_component_setting_key(stack_component: StackComponent) -> str
Gets the setting key for a stack component.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stack_component
|
StackComponent
|
The stack component for which to get the key. |
required |
Returns:
Type | Description |
---|---|
str
|
The setting key for the stack component. |
Source code in src/zenml/utils/settings_utils.py
32 33 34 35 36 37 38 39 40 41 |
|
is_general_setting_key(key: str) -> bool
Checks whether the key refers to a general setting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
If the key refers to a general setting. |
Source code in src/zenml/utils/settings_utils.py
80 81 82 83 84 85 86 87 88 89 |
|
is_stack_component_setting_key(key: str) -> bool
Checks whether a settings key refers to a stack component.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
If the key refers to a stack component. |
Source code in src/zenml/utils/settings_utils.py
68 69 70 71 72 73 74 75 76 77 |
|
is_valid_setting_key(key: str) -> bool
Checks whether a settings key is valid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
If the key is valid. |
Source code in src/zenml/utils/settings_utils.py
56 57 58 59 60 61 62 63 64 65 |
|
validate_setting_keys(setting_keys: Sequence[str]) -> None
Validates settings keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
setting_keys
|
Sequence[str]
|
The keys to validate. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If any key is invalid. |
Source code in src/zenml/utils/settings_utils.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
singleton
Utility class to turn classes into singleton classes.
Classes
SingletonMetaClass(*args: Any, **kwargs: Any)
Bases: type
Singleton metaclass.
Use this metaclass to make any class into a singleton class:
class OneRing(metaclass=SingletonMetaClass):
def __init__(self, owner):
self._owner = owner
@property
def owner(self):
return self._owner
the_one_ring = OneRing('Sauron')
the_lost_ring = OneRing('Frodo')
print(the_lost_ring.owner) # Sauron
OneRing._clear() # ring destroyed
Initialize a singleton class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Additional arguments. |
()
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Source code in src/zenml/utils/singleton.py
40 41 42 43 44 45 46 47 48 |
|
source_code_utils
Utilities for getting the source code of objects.
Classes
Functions
get_hashed_source_code(value: Any) -> str
Returns a hash of the objects source code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
object to get source from. |
required |
Returns:
Type | Description |
---|---|
str
|
Hash of source code. |
Raises:
Type | Description |
---|---|
TypeError
|
If unable to compute the hash. |
Source code in src/zenml/utils/source_code_utils.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
get_source_code(value: Any) -> str
Returns the source code of an object.
If executing within a IPython kernel environment, then this monkey-patches
inspect
module temporarily with a workaround to get source from the cell.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
object to get source from. |
required |
Returns:
Type | Description |
---|---|
str
|
Source code of object. |
Source code in src/zenml/utils/source_code_utils.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 93 94 95 96 97 98 99 100 |
|
source_utils
Utilities for loading/resolving objects.
Classes
Functions
get_implicit_source_root() -> str
Get the implicit source root (the parent directory of the main module).
Raises:
Type | Description |
---|---|
RuntimeError
|
If the main module file can't be found. |
Returns:
Type | Description |
---|---|
str
|
The implicit source root. |
Source code in src/zenml/utils/source_utils.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
|
get_resolved_notebook_sources() -> Dict[str, str]
Get all notebook sources that were resolved in this process.
Returns:
Type | Description |
---|---|
Dict[str, str]
|
Dictionary mapping the import path of notebook sources to the code |
Dict[str, str]
|
of their notebook cell. |
Source code in src/zenml/utils/source_utils.py
806 807 808 809 810 811 812 813 |
|
get_source_root() -> str
Get the source root.
The source root will be determined in the following order: - The manually specified custom source root if it was set. - The ZenML repository directory if one exists in the current working directory or any parent directories. - The parent directory of the main module file.
Returns:
Type | Description |
---|---|
str
|
The source root. |
Source code in src/zenml/utils/source_utils.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
|
get_source_type(module: ModuleType) -> SourceType
Get the type of a source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module
|
ModuleType
|
The module for which to get the source type. |
required |
Returns:
Type | Description |
---|---|
SourceType
|
The source type. |
Source code in src/zenml/utils/source_utils.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
|
is_distribution_package_file(file_path: str, module_name: str) -> bool
Checks if a file/module belongs to a distribution package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
The file path to check. |
required |
module_name
|
str
|
The module name. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the file/module belongs to a distribution package, False |
bool
|
otherwise. |
Source code in src/zenml/utils/source_utils.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
|
is_internal_module(module_name: str) -> bool
Checks if a module is internal (=part of the zenml package).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_name
|
str
|
Name of the module to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the module is internal, False otherwise. |
Source code in src/zenml/utils/source_utils.py
352 353 354 355 356 357 358 359 360 361 |
|
is_standard_lib_file(file_path: str) -> bool
Checks if a file belongs to the Python standard library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
The file path to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the file belongs to the Python standard library, False |
bool
|
otherwise. |
Source code in src/zenml/utils/source_utils.py
377 378 379 380 381 382 383 384 385 386 387 388 389 |
|
is_user_file(file_path: str) -> bool
Checks if a file is a user file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
The file path to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the file is a user file, False otherwise. |
Source code in src/zenml/utils/source_utils.py
364 365 366 367 368 369 370 371 372 373 374 |
|
load(source: Union[Source, str]) -> Any
Load a source or import path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
Union[Source, str]
|
The source to load. |
required |
Returns:
Type | Description |
---|---|
Any
|
The loaded object. |
Source code in src/zenml/utils/source_utils.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 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 |
|
load_and_validate_class(source: Union[str, Source], expected_class: Type[Any]) -> Type[Any]
Loads a source class and validates its class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
Union[str, Source]
|
The source. |
required |
expected_class
|
Type[Any]
|
The class that the source should resolve to. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If the source does not resolve to the expected class. |
Returns:
Type | Description |
---|---|
Type[Any]
|
The resolved source class. |
Source code in src/zenml/utils/source_utils.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
|
prepend_python_path(path: str) -> Iterator[None]
Context manager to temporarily prepend a path to the python path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path that will be prepended to sys.path for the duration of the context manager. |
required |
Yields:
Type | Description |
---|---|
None
|
None |
Source code in src/zenml/utils/source_utils.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
|
resolve(obj: Union[Type[Any], Callable[..., Any], ModuleType, FunctionType, BuiltinFunctionType, NoneType], skip_validation: bool = False) -> Source
Resolve an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Union[Type[Any], Callable[..., Any], ModuleType, FunctionType, BuiltinFunctionType, NoneType]
|
The object to resolve. |
required |
skip_validation
|
bool
|
If True, the validation that the object exist in the module is skipped. |
False
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If the object can't be resolved. |
Returns:
Type | Description |
---|---|
Source
|
The source of the resolved object. |
Source code in src/zenml/utils/source_utils.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 275 |
|
set_custom_source_root(source_root: Optional[str]) -> None
Sets a custom source root.
If set this has the highest priority and will always be used as the source root.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_root
|
Optional[str]
|
The source root to use. |
required |
Source code in src/zenml/utils/source_utils.py
338 339 340 341 342 343 344 345 346 347 348 349 |
|
validate_source_class(source: Union[Source, str], expected_class: Type[Any]) -> bool
Validates that a source resolves to a certain class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
Union[Source, str]
|
The source to validate. |
required |
expected_class
|
Type[Any]
|
The class that the source should resolve to. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the source resolves to the expected class, False otherwise. |
Source code in src/zenml/utils/source_utils.py
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
|
Modules
string_utils
Utils for strings.
Functions
b64_decode(input_: str) -> str
Returns a decoded string of the base 64 encoded input string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_
|
str
|
Base64 encoded string. |
required |
Returns:
Type | Description |
---|---|
str
|
Decoded string. |
Source code in src/zenml/utils/string_utils.py
89 90 91 92 93 94 95 96 97 98 99 100 |
|
b64_encode(input_: str) -> str
Returns a base 64 encoded string of the input string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_
|
str
|
The input to encode. |
required |
Returns:
Type | Description |
---|---|
str
|
Base64 encoded string. |
Source code in src/zenml/utils/string_utils.py
75 76 77 78 79 80 81 82 83 84 85 86 |
|
format_name_template(name_template: str, substitutions: Optional[Dict[str, str]] = None) -> str
Formats a name template with the given arguments.
Default substitutions for {date}
and {time}
placeholders will be used if
not included in the provided substitutions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name_template
|
str
|
The name template to format. |
required |
substitutions
|
Optional[Dict[str, str]]
|
Substitutions to use in the template. |
None
|
Returns:
Type | Description |
---|---|
str
|
The formatted name template. |
Raises:
Type | Description |
---|---|
KeyError
|
If a key in template is missing in the substitutions. |
ValueError
|
If the formatted name is empty. |
Source code in src/zenml/utils/string_utils.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
get_human_readable_filesize(bytes_: int) -> str
Convert a file size in bytes into a human-readable string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bytes_
|
int
|
The number of bytes to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
A human-readable string. |
Source code in src/zenml/utils/string_utils.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
get_human_readable_time(seconds: float) -> str
Convert seconds into a human-readable string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seconds
|
float
|
The number of seconds to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
A human-readable string. |
Source code in src/zenml/utils/string_utils.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
random_str(length: int) -> str
Generate a random human-readable string of given length.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
length
|
int
|
Length of string |
required |
Returns:
Type | Description |
---|---|
str
|
Random human-readable string. |
Source code in src/zenml/utils/string_utils.py
103 104 105 106 107 108 109 110 111 112 113 |
|
substitute_string(value: V, substitution_func: Callable[[str], str]) -> V
Recursively substitute strings in objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
V
|
An object in which the strings should be recursively substituted. This can be a pydantic model, dict, set, list, tuple or any primitive type. |
required |
substitution_func
|
Callable[[str], str]
|
The function that does the actual string substitution. |
required |
Returns:
Type | Description |
---|---|
V
|
The object with the substitution function applied to all string values. |
Source code in src/zenml/utils/string_utils.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
validate_name(model: BaseModel) -> None
Validator to ensure that the given name has only allowed characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
BaseModel
|
The model to validate. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the name has invalid characters. |
Source code in src/zenml/utils/string_utils.py
116 117 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 |
|
tag_utils
Utility functions for tags.
Classes
Tag
Bases: BaseModel
A model representing a tag.
to_request() -> TagRequest
Convert the tag to a TagRequest.
Returns:
Type | Description |
---|---|
TagRequest
|
The tag as a TagRequest. |
Source code in src/zenml/utils/tag_utils.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
Functions
add_tags(tags: List[Union[str, Tag]], pipeline: Optional[Union[UUID, str]] = None, run: Optional[Union[UUID, str]] = None, run_template: Optional[Union[UUID, str]] = None, artifact: Optional[Union[UUID, str]] = None, artifact_version_id: Optional[UUID] = None, artifact_name: Optional[str] = None, artifact_version: Optional[str] = None, infer_artifact: bool = False) -> None
add_tags(tags: List[Union[str, Tag]]) -> None
add_tags(
*, tags: List[Union[str, Tag]], run: Union[UUID, str]
) -> None
add_tags(
*,
tags: List[Union[str, Tag]],
artifact: Union[UUID, str],
) -> None
add_tags(
*,
tags: List[Union[str, Tag]],
artifact_version_id: UUID,
) -> None
add_tags(
*,
tags: List[Union[str, Tag]],
artifact_name: str,
artifact_version: Optional[str] = None,
) -> None
add_tags(
*,
tags: List[Union[str, Tag]],
infer_artifact: bool = False,
artifact_name: Optional[str] = None,
) -> None
add_tags(
*,
tags: List[Union[str, Tag]],
pipeline: Union[UUID, str],
) -> None
add_tags(
*,
tags: List[Union[str, Tag]],
run_template: Union[UUID, str],
) -> None
Add tags to various resource types in a generalized way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tags
|
List[Union[str, Tag]]
|
The tags to add. |
required |
pipeline
|
Optional[Union[UUID, str]]
|
The ID or the name of the pipeline. |
None
|
run
|
Optional[Union[UUID, str]]
|
The id, name or prefix of the run. |
None
|
run_template
|
Optional[Union[UUID, str]]
|
The ID or the name of the run template. |
None
|
artifact
|
Optional[Union[UUID, str]]
|
The ID or the name of the artifact. |
None
|
artifact_version_id
|
Optional[UUID]
|
The ID of the artifact version. |
None
|
artifact_name
|
Optional[str]
|
The name of the artifact. |
None
|
artifact_version
|
Optional[str]
|
The version of the artifact. |
None
|
infer_artifact
|
bool
|
Flag deciding whether the artifact version should be inferred from the step context. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
If no identifiers are provided and the function is not called from within a step, or if exclusive is provided for a resource type that doesn't support it. |
Source code in src/zenml/utils/tag_utils.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
|
remove_tags(tags: List[str], pipeline: Optional[Union[UUID, str]] = None, run: Optional[Union[UUID, str]] = None, run_template: Optional[Union[UUID, str]] = None, artifact: Optional[Union[UUID, str]] = None, artifact_version_id: Optional[UUID] = None, artifact_name: Optional[str] = None, artifact_version: Optional[str] = None, infer_artifact: bool = False) -> None
remove_tags(tags: List[str]) -> None
remove_tags(
*, tags: List[str], pipeline: Union[UUID, str]
) -> None
remove_tags(
*, tags: List[str], run: Union[UUID, str]
) -> None
remove_tags(
*, tags: List[str], run_template: Union[UUID, str]
) -> None
remove_tags(
*, tags: List[str], artifact: Union[UUID, str]
) -> None
remove_tags(
*, tags: List[str], artifact_version_id: UUID
) -> None
remove_tags(
*,
tags: List[str],
artifact_name: str,
artifact_version: Optional[str] = None,
) -> None
remove_tags(
*,
tags: List[str],
infer_artifact: bool = False,
artifact_name: Optional[str] = None,
) -> None
Remove tags from various resource types in a generalized way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tags
|
List[str]
|
The tags to remove. |
required |
pipeline
|
Optional[Union[UUID, str]]
|
The ID or the name of the pipeline. |
None
|
run
|
Optional[Union[UUID, str]]
|
The id, name or prefix of the run. |
None
|
run_template
|
Optional[Union[UUID, str]]
|
The ID or the name of the run template. |
None
|
artifact
|
Optional[Union[UUID, str]]
|
The ID or the name of the artifact. |
None
|
artifact_version_id
|
Optional[UUID]
|
The ID of the artifact version. |
None
|
artifact_name
|
Optional[str]
|
The name of the artifact. |
None
|
artifact_version
|
Optional[str]
|
The version of the artifact. |
None
|
infer_artifact
|
bool
|
Flag deciding whether the artifact version should be inferred from the step context. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
If no identifiers are provided and the function is not called from within a step. |
Source code in src/zenml/utils/tag_utils.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
|
time_utils
Time utils.
Functions
expires_in(expires_at: datetime, expired_str: str, skew_tolerance: Optional[int] = None) -> str
Returns a human-readable string of the time until an expiration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expires_at
|
datetime
|
Expiration time. |
required |
expired_str
|
str
|
String to return if the expiration is in the past. |
required |
skew_tolerance
|
Optional[int]
|
Seconds of skew tolerance to subtract from the expiration time. If the expiration is within the skew tolerance, the function will return the expired string. |
None
|
Returns:
Type | Description |
---|---|
str
|
Human readable string. |
Source code in src/zenml/utils/time_utils.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
seconds_to_human_readable(time_seconds: int) -> str
Converts seconds to human-readable format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_seconds
|
int
|
Seconds to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
Human readable string. |
Source code in src/zenml/utils/time_utils.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
to_local_tz(dt: datetime) -> datetime
Convert a datetime to the local timezone.
If the input datetime is timezone-naive, it will be assumed to be in the UTC timezone.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dt
|
datetime
|
datetime to convert. |
required |
Returns:
Type | Description |
---|---|
datetime
|
Datetime in the local timezone. |
Source code in src/zenml/utils/time_utils.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
to_utc_timezone(dt: datetime) -> datetime
Convert a datetime to the UTC timezone.
If the input datetime is timezone-naive, it will be assumed to be in the UTC timezone.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dt
|
datetime
|
datetime to convert. |
required |
Returns:
Type | Description |
---|---|
datetime
|
Datetime in the UTC timezone. |
Source code in src/zenml/utils/time_utils.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
utc_now(tz_aware: Union[bool, datetime] = False) -> datetime
Get the current time in the UTC timezone.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tz_aware
|
Union[bool, datetime]
|
Use this flag to control whether the returned datetime is timezone-aware or timezone-naive. If a datetime is provided, the returned datetime will be timezone-aware if and only if the input datetime is also timezone-aware. |
False
|
Returns:
Type | Description |
---|---|
datetime
|
The current UTC time. If tz_aware is a datetime, the returned datetime |
datetime
|
will be timezone-aware only if the input datetime is also timezone-aware. |
datetime
|
If tz_aware is a boolean, the returned datetime will be timezone-aware |
datetime
|
if True, and timezone-naive if False. |
Source code in src/zenml/utils/time_utils.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 |
|
utc_now_tz_aware() -> datetime
Get the current timezone-aware UTC time.
Returns:
Type | Description |
---|---|
datetime
|
The current UTC time. |
Source code in src/zenml/utils/time_utils.py
47 48 49 50 51 52 53 |
|
typed_model
Utility classes for adding type information to Pydantic models.
Classes
BaseTypedModel
Bases: BaseModel
Typed Pydantic model base class.
Use this class as a base class instead of BaseModel to automatically
add a type
literal attribute to the model that stores the name of the
class.
This can be useful when serializing models to JSON and then de-serializing them as part of a submodel union field, e.g.:
class BluePill(BaseTypedModel):
...
class RedPill(BaseTypedModel):
...
class TheMatrix(BaseTypedModel):
choice: Union[BluePill, RedPill] = Field(..., discriminator='type')
matrix = TheMatrix(choice=RedPill())
d = matrix.dict()
new_matrix = TheMatrix.model_validate(d)
assert isinstance(new_matrix.choice, RedPill)
It can also facilitate de-serializing objects when their type isn't known:
matrix = TheMatrix(choice=RedPill())
d = matrix.dict()
new_matrix = BaseTypedModel.from_dict(d)
assert isinstance(new_matrix.choice, RedPill)
from_dict(model_dict: Dict[str, Any]) -> BaseTypedModel
classmethod
Instantiate a Pydantic model from a serialized JSON-able dict representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_dict
|
Dict[str, Any]
|
the model attributes serialized as JSON-able dict. |
required |
Returns:
Type | Description |
---|---|
BaseTypedModel
|
A BaseTypedModel created from the serialized representation. |
Raises:
Type | Description |
---|---|
RuntimeError
|
if the model_dict contains an invalid type. |
Source code in src/zenml/utils/typed_model.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
from_json(json_str: str) -> BaseTypedModel
classmethod
Instantiate a Pydantic model from a serialized JSON representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_str
|
str
|
the model attributes serialized as JSON. |
required |
Returns:
Type | Description |
---|---|
BaseTypedModel
|
A BaseTypedModel created from the serialized representation. |
Source code in src/zenml/utils/typed_model.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
BaseTypedModelMeta
Bases: ModelMetaclass
Metaclass responsible for adding type information to Pydantic models.
__new__(mcs, name: str, bases: Tuple[Type[Any], ...], dct: Dict[str, Any]) -> BaseTypedModelMeta
Creates a Pydantic BaseModel class.
This includes a hidden attribute that reflects the full class identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the class. |
required |
bases
|
Tuple[Type[Any], ...]
|
The base classes of the class. |
required |
dct
|
Dict[str, Any]
|
The class dictionary. |
required |
Returns:
Type | Description |
---|---|
BaseTypedModelMeta
|
A Pydantic BaseModel class that includes a hidden attribute that |
BaseTypedModelMeta
|
reflects the full class identifier. |
Raises:
Type | Description |
---|---|
TypeError
|
If the class is not a Pydantic BaseModel class. |
Source code in src/zenml/utils/typed_model.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
Modules
typing_utils
Carried over version of some functions from the pydantic v1 typing module.
Check out the latest version here: https://github.com/pydantic/pydantic/blob/v1.10.14/pydantic/typing.py
Functions
all_literal_values(type_: Type[Any]) -> Tuple[Any, ...]
Fetches the literal values defined in a type in a recursive manner.
This method is used to retrieve all Literal values as Literal can be
used recursively (see https://www.python.org/dev/peps/pep-0586)
e.g. Literal[Literal[Literal[1, 2, 3], "foo"], 5, None]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_
|
Type[Any]
|
type to check. |
required |
Returns:
Type | Description |
---|---|
Tuple[Any, ...]
|
tuple of all the literal values defined in the type. |
Source code in src/zenml/utils/typing_utils.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
get_args(tp: Type[Any]) -> Tuple[Any, ...]
Get type arguments with all substitutions performed.
For unions, basic simplifications used by Union constructor are performed. Examples:: get_args(Dict[str, int]) == (str, int) get_args(int) == () get_args(Union[int, Union[T, int], str][int]) == (int, str) get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int]) get_args(Callable[[], T][int]) == ([], int)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tp
|
Type[Any]
|
the type to check. |
required |
Returns:
Type | Description |
---|---|
Tuple[Any, ...]
|
Tuple of all the args. |
Source code in src/zenml/utils/typing_utils.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
get_origin(tp: Type[Any]) -> Optional[Type[Any]]
Fetches the origin of a given type.
We can't directly use typing.get_origin
since we need a fallback to
support custom generic classes like ConstrainedList
It should be useless once https://github.com/cython/cython/issues/3537 is
solved and https://github.com/pydantic/pydantic/pull/1753 is merged.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tp
|
Type[Any]
|
type to check |
required |
Returns:
Type | Description |
---|---|
Optional[Type[Any]]
|
the origin type of the provided type. |
Source code in src/zenml/utils/typing_utils.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
is_literal_type(type_: Type[Any]) -> bool
Checks if the provided type is a literal type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_
|
Type[Any]
|
type to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
boolean indicating whether the type is union type. |
Source code in src/zenml/utils/typing_utils.py
90 91 92 93 94 95 96 97 98 99 |
|
is_none_type(type_: Any) -> bool
Checks if the provided type is a none type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_
|
Any
|
type to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
boolean indicating whether the type is a none type. |
Source code in src/zenml/utils/typing_utils.py
43 44 45 46 47 48 49 50 51 52 |
|
is_optional(tp: Type[Any]) -> bool
Checks whether a given annotation is typing.Optional.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tp
|
Type[Any]
|
the type to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
boolean indicating if the type is typing.Optional. |
Source code in src/zenml/utils/typing_utils.py
214 215 216 217 218 219 220 221 222 223 |
|
is_union(type_: Optional[Type[Any]]) -> bool
Checks if the provided type is a union type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_
|
Optional[Type[Any]]
|
type to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
boolean indicating whether the type is union type. |
Source code in src/zenml/utils/typing_utils.py
73 74 75 76 77 78 79 80 81 82 83 84 |
|
literal_values(type_: Type[Any]) -> Tuple[Any, ...]
Fetches the literal values defined in a type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_
|
Type[Any]
|
type to check. |
required |
Returns:
Type | Description |
---|---|
Tuple[Any, ...]
|
tuple of the literal values. |
Source code in src/zenml/utils/typing_utils.py
102 103 104 105 106 107 108 109 110 111 |
|
uuid_utils
Utility functions for handling UUIDs.
Functions
generate_uuid_from_string(value: str) -> UUID
Deterministically generates a UUID from a string seed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
The string from which to generate the UUID. |
required |
Returns:
Type | Description |
---|---|
UUID
|
The generated UUID. |
Source code in src/zenml/utils/uuid_utils.py
62 63 64 65 66 67 68 69 70 71 72 73 |
|
is_valid_uuid(value: Any, version: int = 4) -> bool
Checks if a string is a valid UUID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
String to check. |
required |
version
|
int
|
Version of UUID to check for. |
4
|
Returns:
Type | Description |
---|---|
bool
|
True if string is a valid UUID, False otherwise. |
Source code in src/zenml/utils/uuid_utils.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
parse_name_or_uuid(name_or_id: Optional[str]) -> Optional[Union[str, UUID]]
Convert a "name or id" string value to a string or UUID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name_or_id
|
Optional[str]
|
Name or id to convert. |
required |
Returns:
Type | Description |
---|---|
Optional[Union[str, UUID]]
|
A UUID if name_or_id is a UUID, string otherwise. |
Source code in src/zenml/utils/uuid_utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
visualization_utils
Utility functions for dashboard visualizations.
Classes
Functions
format_csv_visualization_as_html(csv_visualization: str, max_rows: int = 10, max_cols: int = 10) -> str
Formats a CSV visualization as an HTML table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_visualization
|
str
|
CSV visualization as a string. |
required |
max_rows
|
int
|
Maximum number of rows to display. Remaining rows will be replaced by an ellipsis in the middle of the table. |
10
|
max_cols
|
int
|
Maximum number of columns to display. Remaining columns will be replaced by an ellipsis at the end of each row. |
10
|
Returns:
Type | Description |
---|---|
str
|
HTML table as a string. |
Source code in src/zenml/utils/visualization_utils.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
visualize_artifact(artifact: ArtifactVersionResponse, title: Optional[str] = None) -> None
Visualize an artifact in notebook environments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact
|
ArtifactVersionResponse
|
The artifact to visualize. |
required |
title
|
Optional[str]
|
Optional title to show before the visualizations. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If not in a notebook environment. |
Source code in src/zenml/utils/visualization_utils.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
yaml_utils
Utility functions to help with YAML files and data.
Classes
UUIDEncoder
Bases: JSONEncoder
JSON encoder for UUID objects.
default(obj: Any) -> Any
Default UUID encoder for JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
Object to encode. |
required |
Returns:
Type | Description |
---|---|
Any
|
Encoded object. |
Source code in src/zenml/utils/yaml_utils.py
171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
Functions
append_yaml(file_path: str, contents: Dict[Any, Any]) -> None
Append contents to a YAML file at file_path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to YAML file. |
required |
contents
|
Dict[Any, Any]
|
Contents of YAML file as dict. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
if directory does not exist. |
Source code in src/zenml/utils/yaml_utils.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
comment_out_yaml(yaml_string: str) -> str
Comments out a yaml string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_string
|
str
|
The yaml string to comment out. |
required |
Returns:
Type | Description |
---|---|
str
|
The commented out yaml string. |
Source code in src/zenml/utils/yaml_utils.py
107 108 109 110 111 112 113 114 115 116 117 118 |
|
is_json_serializable(obj: Any) -> bool
Checks whether an object is JSON serializable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The object to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the object is JSON serializable using pydantics encoder class. |
Source code in src/zenml/utils/yaml_utils.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
is_yaml(file_path: str) -> bool
Returns True if file_path is YAML, else False.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to YAML file. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if is yaml, else False. |
Source code in src/zenml/utils/yaml_utils.py
93 94 95 96 97 98 99 100 101 102 103 104 |
|
read_json(file_path: str) -> Any
Read JSON on file path and returns contents as dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to JSON file. |
required |
Returns:
Type | Description |
---|---|
Any
|
Contents of the file in a dict. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
if file does not exist. |
Source code in src/zenml/utils/yaml_utils.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
|
read_yaml(file_path: str) -> Any
Read YAML on file path and returns contents as dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to YAML file. |
required |
Returns:
Type | Description |
---|---|
Any
|
Contents of the file in a dict. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
if file does not exist. |
Source code in src/zenml/utils/yaml_utils.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
write_json(file_path: str, contents: Any, encoder: Optional[Type[json.JSONEncoder]] = None, **json_dump_args: Any) -> None
Write contents as JSON format to file_path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to JSON file. |
required |
contents
|
Any
|
Contents of JSON file. |
required |
encoder
|
Optional[Type[JSONEncoder]]
|
Custom JSON encoder to use when saving json. |
None
|
**json_dump_args
|
Any
|
Extra arguments to pass to json.dumps. |
{}
|
Raises:
Type | Description |
---|---|
FileNotFoundError
|
if directory does not exist. |
Source code in src/zenml/utils/yaml_utils.py
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 |
|
write_yaml(file_path: str, contents: Union[Dict[Any, Any], List[Any]], sort_keys: bool = True) -> None
Write contents as YAML format to file_path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to YAML file. |
required |
contents
|
Union[Dict[Any, Any], List[Any]]
|
Contents of YAML file as dict or list. |
required |
sort_keys
|
bool
|
If |
True
|
Raises:
Type | Description |
---|---|
FileNotFoundError
|
if directory does not exist. |
Source code in src/zenml/utils/yaml_utils.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|