Shared utilities¶
The atomistic-cookbook-utils package provides
small helpers used across many recipes, so that recipe code can stay
focused on the science. Add it to your recipe’s environment.yml
under pip: (e.g. atomistic-cookbook-utils >=0.1,<0.2) and
import the helpers you need.
The package source lives at src/atomistic-cookbook-utils/ in this
repository.
API reference¶
- atomistic_cookbook_utils.download_with_retry(url: str, destination: str | Path, *, retries: int = 5, backoff_factor: float = 1.0, status_forcelist: Tuple[int, ...] = (429, 500, 502, 503, 504), overwrite: bool = False, chunk_size: int = 65536) → Path[source]¶
Download
urltodestinationwith automatic retries.The destination’s parent directories are created as needed. If the file already exists and
overwriteis false (the default), the download is skipped and the existing path is returned.Retries follow urllib3’s
Retrywith an exponential backoff. With the defaults (retries=5,backoff_factor=1) the waits between attempts are roughly 1, 2, 4, 8, 16 seconds.- Parameters:
url – URL to fetch.
destination – Where to save the file.
retries – Maximum number of retry attempts on the status codes listed in
status_forcelist(and on connect/read errors).backoff_factor – Exponential backoff factor passed to urllib3
Retry.status_forcelist – HTTP status codes that trigger a retry.
overwrite – If true, re-download even if the destination already exists.
chunk_size – Streaming chunk size in bytes.
- Returns:
The resolved destination path.
- Return type:
- atomistic_cookbook_utils.run_command(command: str, *, cwd: str | Path | None = None, check: bool = True, env: Mapping[str, str] | None = None, print_output: bool = False) → CompletedProcess[source]¶
Run a shell-like command string.
The string is split with
shlex.split()and passed as a list of arguments tosubprocess.run(). Shell features such as pipes, redirection, and glob expansion are not supported — for those, usesubprocessdirectly.By default the child process inherits the parent’s stdout and stderr, so output appears wherever the parent’s output goes. Pass
print_output=Trueto capture the command’s combined stdout and stderr and re-emit it viaprint(), which makes the output show up in sphinx-gallery rendered cells (sphinx-gallery capturessys.stdoutbut not raw file-descriptor writes from children).- Parameters:
command – The command to run, e.g.
"ls -la".cwd – Working directory in which to run the command.
check – If true (the default), raise
subprocess.CalledProcessErroron non-zero exit status. Whenprint_outputis true, any captured output is printed before the exception is raised.env – Optional environment mapping. If omitted, the current process environment is inherited.
print_output – If true, capture the child’s combined stdout and stderr and re-emit it via
print()so it is visible tosys.stdout-based capture systems like sphinx-gallery. The captured text is also accessible as.stdouton the returnedCompletedProcess.
- Returns:
The completed process. When
print_outputis true,result.stdoutholds the captured combined output as text; otherwise it is unset.- Return type: