From 9d5d56ceafbd65bd7fa431cc74c058351c389cc8 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sun, 2 Mar 2014 19:34:55 +0200 Subject: [PATCH] Country and Venue now hashable, for #82 --- pylast.py | 9 ++++++++- test_pylast.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pylast.py b/pylast.py index 611c605..4d122a4 100644 --- a/pylast.py +++ b/pylast.py @@ -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) diff --git a/test_pylast.py b/test_pylast.py index 8a62980..3676ce1 100755 --- a/test_pylast.py +++ b/test_pylast.py @@ -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):