Skip to content
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

Access project from job #808

Merged
merged 11 commits into from
Aug 30, 2022
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Version 2

Added
+++++

- ``H5Store`` related errors are now included in the public API (#775).
- Users can now access the project which a job belongs to from the job object.

Changed
+++++++
Expand Down
4 changes: 4 additions & 0 deletions contributors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,8 @@ contributors:
family-names: Takada
given-names: Kody
affiliation: "University of Michigan"
-
family-names: Kadar
given-names: Alain
affiliation: "University of Michigan"
...
1 change: 1 addition & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ The Job class
Job.move
Job.open
Job.path
Job.project
Job.remove
Job.reset
Job.sp
Expand Down
11 changes: 11 additions & 0 deletions signac/contrib/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,17 @@ def data(self, new_data):
"""
self.stores[self.KEY_DATA] = new_data

@property
def project(self):
"""Get the project that contains this job.

Returns
-------
signac.Project
Returns the project containing this job.
"""
return self._project

def init(self, force=False):
"""Initialize the job's workspace directory.

Expand Down
17 changes: 17 additions & 0 deletions tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,23 @@ def test_deepcopy(self):
copied_job.sp.a = 3
assert copied_job in self.project

def test_project_access_from_job(self):
job = self.project.open_job({"a": 0}).init()
assert isinstance(job.project, signac.Project)
assert job in job.project
assert job.project.path == self._tmp_pr

def test_custom_project_access_from_job(self):
# Test a custom project subclass to ensure compatibility with signac-flow's FlowProject
class CustomProject(signac.Project):
pass

custom_project = CustomProject.get_project(self._tmp_pr)
job = custom_project.open_job({"a": 0}).init()
assert isinstance(job.project, CustomProject)
assert job in job.project
assert job.project.path == self._tmp_pr


class TestJobSpInterface(TestJobBase):
def test_interface_read_only(self):
Expand Down