muh6mm3d / pylast

Automatically exported from code.google.com/p/pylast
Apache License 2.0
0 stars 0 forks source link

__hash__ should be defined for Artist, Album etc. #25

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please type example code that produces the issue:

import pylast
network = pylast.get_lastfm_network(api_key = "(KEY)", api_secret = "(KEY)")
# Both rask00 and radio_head247 have RADIOHEAD as top artist
radiohead_1 = network.get_user("rask00").get_top_artists()[0].item
radiohead_2 = network.get_user("radio_head247").get_top_artists()[0].item
# Therefore the following statement correctly returns TRUE
radiohead_1 == radiohead_2
# But the following statement does not work correctly:
set([radiohead_1, radiohead_2])

What is the expected output? What do you see instead?

It is supposed to show set([Radiohead]), while it shows set([Radiohead, 
Radiohead])

What version of pyLast is this?

0.4.13

Please provide any additional information below.

The solution to this bug is to add a function __hash__ to the class Artist and 
the other related 
classes. The reason is explained here: 
http://wiki.python.org/moin/DictionaryKeys
Since __eq__ and __ne__ are already defined in Artist using the artist's name, 
then the solution is 
to add the following code to the class Artist:

    def __hash__(self):
        return hash(self.get_name().lower())

and do the same on the other related classes (Album, etc...)

Original issue reported on code.google.com by claud...@gmail.com on 6 Oct 2009 at 2:11

GoogleCodeExporter commented 9 years ago
fixed in r212

Original comment by amr.hassan on 6 Oct 2009 at 2:29