Skip to content

Commit

Permalink
Fix and improve repr of Project, Job, and JobsCursor. (#193)
Browse files Browse the repository at this point in the history
* Fix and improve repr of Project, Job, and JobsCursor.

Extend unit tests to check for identify of `eval(repr(x))` of affected
classes.

* Add missing imports to unit tests.

* Emulate signac shell banner for Project html representation.

* Update changelog.txt
  • Loading branch information
csadorf committed Jul 18, 2019
1 parent 34ac45c commit ce8562a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ next
- Fix issue with incorrect detection of dict-like files managed with the ``DictManager`` class (e.g. ``job.stores``) (#203).
- Fix issue causing a failure of the automatic conversion of valid key types (#168, #205).
- Improve the 'dots in keys' error message to make it easier to fix related issues (#170, #205).
- Update the ``__repr__`` and ``__repr_html__`` implementations of the ``Project`, ``Job``, and ``JobsCursor`` classes (#193).


[1.1.0] -- 2019-05-19
Expand Down
2 changes: 1 addition & 1 deletion signac/contrib/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __str__(self):

def __repr__(self):
return "{}(project={}, statepoint={})".format(
self.__class__.__module__ + '.' + self.__class__.__name__,
self.__class__.__name__,
repr(self._project), self._statepoint)

def __eq__(self, other):
Expand Down
30 changes: 19 additions & 11 deletions signac/contrib/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,18 @@ def __str__(self):
return str(self.get_id())

def __repr__(self):
return "{type}({{'project': '{id}', 'project_dir': '{rd}',"\
" 'workspace_dir': '{wd}'}})".format(
type=self.__class__.__module__ + '.' + self.__class__.__name__,
id=self.get_id(), rd=self.root_directory(), wd=self.workspace())
return "{type}.get_project('{root}')".format(
type=self.__class__.__name__,
root=self.root_directory())

def _repr_html_(self):
return repr(self) + self.find_jobs()._repr_html_jobs()
return "<p>" + \
'<strong>Project:</strong> {}<br>'.format(self.get_id()) + \
"<strong>Root:</strong> {}<br>".format(self.root_directory()) + \
"<strong>Workspace:</strong> {}<br>".format(self.workspace()) + \
"<strong>Size:</strong> {}".format(len(self)) + \
"</p>" + \
self.find_jobs()._repr_html_jobs()

def __eq__(self, other):
return repr(self) == repr(other)
Expand Down Expand Up @@ -1597,6 +1602,10 @@ def __init__(self, project, filter, doc_filter):
# next() method for this class.
self._next_iter = None

def __eq__(self, other):
return self._project == other._project and self._filter == other._filter\
and self._doc_filter == other._doc_filter

def __len__(self):
# Highly performance critical code path!!
if self._filter or self._doc_filter:
Expand Down Expand Up @@ -1789,12 +1798,11 @@ def _export_sp_and_doc(job):
orient='index').infer_objects()

def __repr__(self):
return "{type}({{'project': '{project}', 'filter': '{filter}',"\
" 'docfilter': '{doc_filter}'}})".format(
type=self.__class__.__module__ + '.' + self.__class__.__name__,
project=self._project,
filter=self._filter,
doc_filter=self._doc_filter)
return '{type}(project={project}, filter={filter}, doc_filter={doc_filter})'.format(
type=self.__class__.__name__,
project=repr(self._project),
filter=repr(self._filter),
doc_filter=repr(self._doc_filter))

def _repr_html_jobs(self):
html = ''
Expand Down
2 changes: 2 additions & 0 deletions tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import signac.contrib
import signac.common.config
from signac import Project
from signac.contrib.job import Job
from signac.common import six
from signac.errors import DestinationExistsError
from signac.errors import JobsCorruptedError
Expand Down
6 changes: 4 additions & 2 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from signac.contrib.errors import JobsCorruptedError
from signac.contrib.errors import WorkspaceError
from signac.contrib.errors import StatepointParsingError
from signac.contrib.project import JobsCursor, Project

from test_job import BaseJobTest

Expand Down Expand Up @@ -1338,7 +1339,7 @@ def call_repr_methods(self):
with self.subTest(type='str'):
str(self.project)
with self.subTest(type='repr'):
repr(self.project)
self.assertEqual(eval(repr(self.project)), self.project)
with self.subTest(type='html'):
for use_pandas in (True, False):
type(self.project)._use_pandas_for_html_repr = use_pandas
Expand All @@ -1353,7 +1354,8 @@ def call_repr_methods(self):
with self.subTest(type='str'):
str(self.project.find_jobs(filter_))
with self.subTest(type='repr'):
repr(self.project.find_jobs(filter_))
q = self.project.find_jobs(filter_)
self.assertEqual(eval(repr(q)), q)
with self.subTest(type='html'):
for use_pandas in (True, False):
type(self.project)._use_pandas_for_html_repr = use_pandas
Expand Down

0 comments on commit ce8562a

Please sign in to comment.