Skip to content

Commit

Permalink
Merge 418196d into 3417bc2
Browse files Browse the repository at this point in the history
  • Loading branch information
gfairchild committed Jun 22, 2015
2 parents 3417bc2 + 418196d commit 9a8ccfd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/isodate/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ def __repr__(self):
self.tdelta.days, self.tdelta.seconds,
self.tdelta.microseconds, self.years, self.months)

def __hash__(self):
'''
Return a hash of this instance so that it can be used in, for
example, dicts and sets.
'''
return hash((self.tdelta, self.months, self.years))

def __neg__(self):
"""
A simple unary minus.
Expand Down
19 changes: 18 additions & 1 deletion src/isodate/tests/test_duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,30 @@ def test_parseerror(self):

def test_repr(self):
'''
Test __repr__ and __str__ for Duration obqects.
Test __repr__ and __str__ for Duration objects.
'''
dur = Duration(10, 10, years=10, months=10)
self.assertEqual('10 years, 10 months, 10 days, 0:00:10', str(dur))
self.assertEqual('isodate.duration.Duration(10, 10, 0,'
' years=10, months=10)', repr(dur))

def test_hash(self):
'''
Test __hash__ for Duration objects.
'''
dur1 = Duration(10, 10, years=10, months=10)
dur2 = Duration(9, 9, years=9, months=9)
dur3 = Duration(10, 10, years=10, months=10)
self.assertNotEqual(hash(dur1), hash(dur2))
self.assertNotEqual(id(dur1), id(dur2))
self.assertEqual(hash(dur1), hash(dur3))
self.assertNotEqual(id(dur1), id(dur3))
durSet = set()
durSet.add(dur1)
durSet.add(dur2)
durSet.add(dur3)
self.assertEqual(len(durSet), 2)

def test_neg(self):
'''
Test __neg__ for Duration objects.
Expand Down

0 comments on commit 9a8ccfd

Please sign in to comment.