Utils
ghflowgen.utils ¶
Contain the utility functions.
ghflowgen.utils.export ¶
Contain utility functions to export data to JSON format.
ghflowgen.utils.export.generate_unique_tmp_path ¶
generate_unique_tmp_path(path: Path) -> Path
Return a unique temporary path given a path.
This function updates the name to add a UUID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The input path. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The unique name. |
Example usage:
>>> import tempfile
>>> from pathlib import Path
>>> from ghflowgen.utils.export import generate_unique_tmp_path
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = generate_unique_tmp_path(Path(tmpdir).joinpath("data.json"))
... path
...
PosixPath('/.../data-....json')
ghflowgen.utils.export.load_json ¶
load_json(path: Path) -> Any
Load the data from a given JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path to the JSON file. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The data from the JSON file. |
Example usage:
>>> import tempfile
>>> from pathlib import Path
>>> from ghflowgen.utils.export import save_json, load_json
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_json(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
ghflowgen.utils.export.save_json ¶
save_json(
to_save: Any, path: Path, *, exist_ok: bool = False
) -> None
Save the given data in a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
Any
|
The data to write in a JSON file. |
required |
path
|
Path
|
The path where to write the JSON file. |
required |
exist_ok
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
if the file already exists. |
Example usage:
>>> import tempfile
>>> from pathlib import Path
>>> from ghflowgen.utils.export import save_json
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_json(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
ghflowgen.utils.pypi ¶
Contain PyPI utility functions.
ghflowgen.utils.pypi.get_pypi_versions
cached
¶
get_pypi_versions(
package: str, reverse: bool = False
) -> tuple[str, ...]
Get the package versions available on PyPI.
The package versions are read from PyPI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package
|
str
|
The package name. |
required |
reverse
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
A list containing the sorted version strings. |
Example usage:
>>> from ghflowgen.utils.pypi import get_pypi_versions
>>> versions = get_pypi_versions("requests") # doctest: +SKIP