Country and Venue now hashable, for #82

This commit is contained in:
hugovk 2014-03-02 19:34:55 +02:00
parent d0c80c8b4e
commit 9d5d56ceaf
2 changed files with 36 additions and 2 deletions

View file

@ -1011,8 +1011,11 @@ class _BaseObject(object):
return {}
def __hash__(self):
# Convert any ints (or whatever) into strings
values = map(str, self._get_params().values())
return hash(self.network) + \
hash(str(type(self)) + "".join(list(self._get_params().keys()) + list(self._get_params().values())).lower())
hash(str(type(self)) + "".join(list(self._get_params().keys()) + list(values)).lower())
class _Taggable(object):
"""Common functions for classes with tags."""
@ -1885,6 +1888,8 @@ class Country(_BaseObject):
name = None
__hash__ = _BaseObject.__hash__
def __init__(self, name, network):
_BaseObject.__init__(self, network)
@ -3577,6 +3582,8 @@ class Venue(_BaseObject):
location = None
url = None
__hash__ = _BaseObject.__hash__
def __init__(self, id, network, venue_element=None):
_BaseObject.__init__(self, network)

View file

@ -387,6 +387,19 @@ class TestPyLast(unittest.TestCase):
self.assertEqual(len(artists), 1)
def test_country_is_hashable(self):
# Arrange
country = pylast.Country("Italy", self.network)
countries = set()
# Act
countries.add(country)
# Assert
self.assertIsNotNone(country)
self.assertEqual(len(countries), 1)
def test_event_is_hashable(self):
# Arrange
user = self.network.get_user("RJ")
@ -441,9 +454,23 @@ class TestPyLast(unittest.TestCase):
self.assertEqual(len(users), 1)
def test_venue_is_hashable(self):
# Arrange
venue_id = "8778225" # Last.fm office
venue = pylast.Venue(venue_id, self.network)
venues = set()
# Act
venues.add(venue)
# Assert
self.assertIsNotNone(venue)
self.assertEqual(len(venues), 1)
if __name__ == '__main__':
# For quick testing of a single-case (eg. test = "test_scrobble")
# For quick testing of a single case (eg. test = "test_scrobble")
test = ""
if test is not None and len(test):