Skip to content

Commit

Permalink
Merge pull request #96 from PainterQubits/develop
Browse files Browse the repository at this point in the history
Merge develop into main
  • Loading branch information
alexhad6 authored Jun 29, 2023
2 parents a677eb4 + dce15fa commit 7b85ca3
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 360 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.9.0] (June 29 2023)

### Added

- `ParamDB.path` to retrieve the database path.
- `ParamDB.latest_commit` to retrieve the latest commit entry.

## [0.8.0] (June 9 2023)

### Changed
Expand Down Expand Up @@ -99,7 +106,8 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Database class `ParamDB` to store parameters in a SQLite file
- Ability to retrieve the commit history as `CommitEntry` objects

[unreleased]: https://github.com/PainterQubits/paramdb/compare/v0.8.0...develop
[unreleased]: https://github.com/PainterQubits/paramdb/compare/v0.9.0...develop
[0.9.0]: https://github.com/PainterQubits/paramdb/releases/tag/v0.9.0
[0.8.0]: https://github.com/PainterQubits/paramdb/releases/tag/v0.8.0
[0.7.0]: https://github.com/PainterQubits/paramdb/releases/tag/v0.7.0
[0.6.0]: https://github.com/PainterQubits/paramdb/releases/tag/v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ authors:
- family-names: "Hadley"
given-names: "Alex"
title: "ParamDB"
version: 0.8.0
date-released: 2023-06-09
version: 0.9.0
date-released: 2023-06-29
url: "https://github.com/PainterQubits/paramdb"
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
project = "ParamDB"
copyright = "2023, California Institute of Technology"
author = "Alex Hadley"
release = "0.8.0"
release = "0.9.0"

# General configuration
extensions = [
Expand Down
23 changes: 20 additions & 3 deletions paramdb/_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,16 @@ class Root(Struct):
"""

def __init__(self, path: str):
self._path = path
self._engine = create_engine(URL.create("sqlite+pysqlite", database=path))
self._Session = sessionmaker(self._engine) # pylint: disable=invalid-name
_Base.metadata.create_all(self._engine)

@property
def path(self) -> str:
"""Path of the database file."""
return self._path

def commit(self, message: str, data: T) -> int:
"""
Commit the given data to the database with the given message and return the ID
Expand Down Expand Up @@ -198,10 +204,9 @@ def load(self, commit_id: int | None = None, *, load_classes: bool = True) -> An
if data is None:
raise IndexError(
f"cannot load most recent commit because database"
f" '{self._engine.url.database}' has no commits"
f" '{self._path}' has no commits"
if commit_id is None
else f"commit {commit_id} does not exist in database"
f" '{self._engine.url.database}'"
else f"commit {commit_id} does not exist in database" f" '{self._path}'"
)
return json.loads(
_decompress(data),
Expand All @@ -217,6 +222,18 @@ def num_commits(self) -> int:
count = session.execute(select_stmt).scalar()
return count if count is not None else 0

@property
def latest_commit(self) -> CommitEntry | None:
"""Latest commit added to the database, or None if the database is empty."""
max_id_func = func.max(_Snapshot.id) # pylint: disable=not-callable
select_max_id = select(max_id_func).scalar_subquery()
select_stmt = select(
_Snapshot.id, _Snapshot.message, _Snapshot.timestamp
).where(_Snapshot.id == select_max_id)
with self._Session() as session:
latest_entry = session.execute(select_stmt).mappings().first()
return None if latest_entry is None else CommitEntry(**latest_entry)

def commit_history(
self,
start: int | None = None,
Expand Down
Loading

0 comments on commit 7b85ca3

Please sign in to comment.