Skip to content

Commit

Permalink
Merge pull request #17 from ihmpdcc/pr-16
Browse files Browse the repository at this point in the history
Pull request from @schwager-hsph merged along with another commit.
  • Loading branch information
victor73 committed Nov 19, 2015
2 parents 602bfa7 + 8f47f2f commit 33c26d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
cutlass 0.6

* Added convenience methods to make objects hashable, equal-able.
Contributed by [email protected].

- Victor <[email protected]> Thu, l9 Nov 2015 12:00:00 -0400

cutlass 0.5

* Fixed requirement for both 'part_of' and 'subset_of' linkages
Expand Down
28 changes: 28 additions & 0 deletions cutlass/Base.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,31 @@ def delete(self):
"Reason: %s" % visit_node_id, e.strerror)

return success


def __str__(self):
_id = "no ID" if not self._id else self._id[-8:]
name = None
for attr in ("_name", "_rand_subject_id", "_date", "_local_file"):
if hasattr(self, attr):
name = getattr(self, attr)
break
if name:
return "<{} `{}' ({})>".format(self.__class__.__name__, name, _id)
else:
return "<{} ({})>".format(self.__class__.__name__, _id)


__repr__ = __str__


def __hash__(self):
if not self._id:
raise TypeError("unhashable; must have ID: '{}')".format(str(self)))
return hash(self._id)


def __eq__(self, other):
if self._id and other._id:
return self._id == other._id
return False

0 comments on commit 33c26d9

Please sign in to comment.