diff --git a/CHANGES b/CHANGES index 8d664e2..9e4d131 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,10 @@ +cutlass 0.6 + + * Added convenience methods to make objects hashable, equal-able. + Contributed by schwager-hsph@github.com. + + - Victor Thu, l9 Nov 2015 12:00:00 -0400 + cutlass 0.5 * Fixed requirement for both 'part_of' and 'subset_of' linkages diff --git a/cutlass/Base.py b/cutlass/Base.py index 9c90962..5ad1c6f 100644 --- a/cutlass/Base.py +++ b/cutlass/Base.py @@ -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