-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formalizing the JobStore document format as a pydantic model #424
Merged
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9921c9b
added moto to tests
hrushikesh-s f94915c
updated job store class
hrushikesh-s 08d8d93
Changing the document parse type from dict to Pydantic
hrushikesh-s 3d56be1
Merge branch 'main' of https://github.com/materialsproject/jobflow in…
hrushikesh-s 172fbca
using the 'memory_job_store' fixture
hrushikesh-s fed9c2a
fix mypy for job_store.py
hrushikesh-s 120b35c
modifiying datatype from List to typing.List
hrushikesh-s 1430909
refracting the name of job_store.py to job_output_schema.py, to maint…
hrushikesh-s 34984db
bug fix
hrushikesh-s 39ae630
adding validator to output attribute
hrushikesh-s fa15289
Fix typos
utf 926c926
Merge branch 'main' into pydantic
utf e78c3f3
added support to pydantic v2
hrushikesh-s 7427d0b
Merge branch 'materialsproject:main' into pydantic
hrushikesh-s d52e29d
Merge branch 'pydantic' of https://github.com/hrushikesh-s/jobflow in…
hrushikesh-s d397c88
added support to pydantic v1
hrushikesh-s 0321931
added support to pydantic v2
hrushikesh-s f40c16f
update docstrings
hrushikesh-s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""A Pydantic model for Jobstore document.""" | ||
|
||
from typing import Generic, List, TypeVar | ||
|
||
from monty.json import MontyDecoder | ||
from pydantic import BaseModel, Field, validator | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class JobStoreDocument(BaseModel, Generic[T]): | ||
"""A Pydantic model for Jobstore document.""" | ||
|
||
uuid: str = Field( | ||
None, description="An unique identifier for the job. Generated automatically." | ||
) | ||
index: int = Field( | ||
None, | ||
description="The index of the job (number of times the job has been replaced).", | ||
) | ||
output: T = Field( | ||
None, | ||
description="This is a reference to the future job output.", | ||
) | ||
completed_at: str = Field(None, description="The time the job was completed.") | ||
metadata: dict = Field( | ||
None, | ||
description="Metadeta information supplied by the user.", | ||
) | ||
hosts: List[str] = Field( | ||
None, | ||
description="The list of UUIDs of the hosts containing the job.", | ||
) | ||
name: str = Field( | ||
None, | ||
description="The name of the job.", | ||
) | ||
|
||
@validator("output", pre=True) | ||
def reserialize_output(cls, v): | ||
""" | ||
Pre-validator for the 'output' field. | ||
|
||
This method checks if the input 'v' is a dictionary with specific keys | ||
('@module' and '@class'). If these keys are present, it reprocesses | ||
the input dictionary using MontyDecoder to deserialize it. | ||
|
||
Parameters | ||
---------- | ||
cls : Type[JobStoreDocument] | ||
The class this validator is applied to. | ||
v : Any | ||
The input value to validate. | ||
|
||
Returns | ||
------- | ||
Any | ||
The validated and potentially deserialized value. | ||
""" | ||
if isinstance(v, dict) and "@module" in v and "@class" in v: | ||
v = MontyDecoder().process_decoded(v) | ||
return v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def sample_data(): | ||
from jobflow.schemas.job_output_schema import JobStoreDocument | ||
|
||
return JobStoreDocument( | ||
uuid="abc123", | ||
index=1, | ||
output=None, | ||
completed_at=datetime.now().isoformat(), | ||
metadata={"key": "value"}, | ||
hosts=["host1", "host2"], | ||
name="my_job", | ||
) | ||
|
||
|
||
def test_job_store_document_model(sample_data): | ||
# Test creating model | ||
data = sample_data | ||
|
||
assert data.uuid == "abc123" | ||
assert data.index == 1 | ||
assert data.output is None | ||
assert datetime.fromisoformat(data.completed_at).hour == datetime.now().hour | ||
assert data.metadata == {"key": "value"} | ||
assert data.hosts == ["host1", "host2"] | ||
assert data.name == "my_job" | ||
|
||
|
||
def test_job_store_update(memory_jobstore, sample_data): | ||
# Storing document as a JobStoreDocument | ||
from jobflow.schemas.job_output_schema import JobStoreDocument | ||
|
||
d = { | ||
"index": 1, | ||
"uuid": "abc123", | ||
"metadata": {"key": "value"}, | ||
"hosts": ["host1", "host2"], | ||
"name": "my_job", | ||
"e": 6, | ||
"d": 4, | ||
} | ||
sample_data = JobStoreDocument(**d) | ||
memory_jobstore.update(sample_data) | ||
|
||
# Check document was inserted | ||
results = memory_jobstore.query_one(criteria={"hosts": {"$exists": 1}}) | ||
assert results["index"] == 1 | ||
assert results["uuid"] == "abc123" | ||
assert results["metadata"] == {"key": "value"} | ||
assert results["hosts"] == ["host1", "host2"] | ||
assert results["name"] == "my_job" | ||
assert "e" not in results | ||
assert "d" not in results | ||
|
||
# Further checks to see if two documents get inserted | ||
e = d.copy() | ||
e["uuid"] = "def456" | ||
new_data_e = JobStoreDocument(**e) | ||
f = d.copy() | ||
f["uuid"] = "ghi789" | ||
new_data_f = JobStoreDocument(**f) | ||
memory_jobstore.update([new_data_e, new_data_f]) | ||
|
||
# Check if document new_data_e is present in the store | ||
results = memory_jobstore.query_one(criteria={"uuid": "def456"}) | ||
assert results["index"] == 1 | ||
assert results["uuid"] == "def456" | ||
assert results["metadata"] == {"key": "value"} | ||
assert results["hosts"] == ["host1", "host2"] | ||
assert results["name"] == "my_job" | ||
assert "e" not in results | ||
assert "d" not in results | ||
|
||
# Check if document new_data_f is present in the store | ||
results = memory_jobstore.query_one(criteria={"uuid": "ghi789"}) | ||
assert results["index"] == 1 | ||
assert results["uuid"] == "ghi789" | ||
assert results["metadata"] == {"key": "value"} | ||
assert results["hosts"] == ["host1", "host2"] | ||
assert results["name"] == "my_job" | ||
assert "e" not in results | ||
assert "d" not in results |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @hrushikesh-s, please can you update this to Pydantic2 syntax, as discussed here: #425 (comment) (if it needs updating, if not, then just give me the go-ahead that I can merge).