valarpy.connection
Module Contents
- class valarpy.connection.Valar(config: Union[None, str, pathlib.Path, List[Union[str, pathlib.Path, None]], Mapping[str, Union[str, int]]] = None)
Global valarpy connection.
Constructor.
- Parameters
config – The connection info, which must contain “database” and optionally parameters passed to peewee If a dict, used as-is. If a path or str, attempts to read JSON from that path. If a list of paths, strs, and Nones, reads from the first extant file found in the list. If None, attempts to read JSON from the
VALARPY_CONFIG
environment variable, if set.- Raises
FileNotFoundError – If a path was supplied but does not point to a file
TypeError – If the type was not recognized
InterfaceError – On some connection issues
- classmethod find_extant_path(*paths: Union[pathlib.Path, str, None]) pathlib.Path
Finds the first extant path in the list. It is rare to need to call this directly.
- Parameters
*paths – A list of file paths; values of None are skipped
- Returns
The first Path that exists
- Raises
FileNotFoundError – If the path found is not a file
- classmethod get_preferred_paths() List[pathlib.Path]
Gets a list of preferred paths to look for config files, in order from highest-priority to least-priority. Starts with the
VALARPY_CONFIG
environment variable, if it is set.Returns: A list of
Path
instances
- rolling_back() Generator[peewee._transaction, None, None]
Starts a transaction or savepoint that will be rolled back whether it fails or succeeds. Useful for testing.
- Yields
A peewee Transaction type; this should generally not be used
- atomic() Generator[peewee._transaction, None, None]
Starts a transaction or savepoint that will be rolled back only on failure.
- Yields
A peewee Transaction type; this should generally not be used
Examples
Here, both
testing1
andtesting2
are created atomically in a single transaction, or neither are created on failure because the transaction is automatically rolled back.@valar.atomic def create_stuff():
Refs(name=”testing1”).save() Refs(name=”testing2”).save()
- reconnect(hard: bool = False) None
Closes and then opens the connection. This may be useful for fixing connection issues.
- Parameters
hard – Forcibly close and re-open connection
- open() None
Opens the database connection. This is already called by
__enter__
.
- close() None
Closes the connection. This is already called by
__exit__
.