Implement __hash__ method for custom classes (close #45)
Implement a reasonable __hash__ method() for the classes MCGAlbum and MCGTrack and use a separate id for referencing albums.
This commit is contained in:
parent
e318194274
commit
670d50cefd
3 changed files with 55 additions and 50 deletions
|
|
@ -12,9 +12,8 @@ import sys
|
|||
import threading
|
||||
import urllib.request
|
||||
|
||||
from hashlib import md5
|
||||
|
||||
from mcg.utils import SortOrder
|
||||
from mcg.utils import Utils
|
||||
|
||||
|
||||
|
||||
|
|
@ -519,7 +518,7 @@ class Client(Base):
|
|||
self._logger.debug("track: %r", track)
|
||||
# Album
|
||||
album = self._extract_album(song, lookup=False)
|
||||
if len(self._playlist) == 0 or self._playlist[len(self._playlist)-1].get_hash() != album.get_hash():
|
||||
if len(self._playlist) == 0 or self._playlist[len(self._playlist)-1] != album:
|
||||
self._playlist.append(album)
|
||||
else:
|
||||
album = self._playlist[len(self._playlist)-1]
|
||||
|
|
@ -736,13 +735,13 @@ class Client(Base):
|
|||
album = None
|
||||
if 'album' not in song:
|
||||
song['album'] = MCGAlbum.DEFAULT_ALBUM
|
||||
hash = MCGAlbum.hash(song['album'])
|
||||
if lookup and hash in self._albums.keys():
|
||||
album = self._albums[hash]
|
||||
id = Utils.generate_id(song['album'])
|
||||
if lookup and id in self._albums.keys():
|
||||
album = self._albums[id]
|
||||
else:
|
||||
album = MCGAlbum(song['album'], self._host, self._image_dir)
|
||||
if lookup:
|
||||
self._albums[hash] = album
|
||||
self._albums[id] = album
|
||||
return album
|
||||
|
||||
|
||||
|
|
@ -822,11 +821,19 @@ class MCGAlbum:
|
|||
self._length = 0
|
||||
self._cover = None
|
||||
self._cover_searched = False
|
||||
self._set_hash()
|
||||
self._id = Utils.generate_id(title)
|
||||
|
||||
|
||||
def __eq__(self, other):
|
||||
return self._hash == other.get_hash()
|
||||
return (other and self.get_id() == other.get_id())
|
||||
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self._title)
|
||||
|
||||
|
||||
def get_id(self):
|
||||
return self._id
|
||||
|
||||
|
||||
def get_artists(self):
|
||||
|
|
@ -889,16 +896,6 @@ class MCGAlbum:
|
|||
return self._cover
|
||||
|
||||
|
||||
def hash(title):
|
||||
if type(title) is list:
|
||||
title = title[0]
|
||||
return md5(title.encode('utf-8')).hexdigest()
|
||||
|
||||
|
||||
def get_hash(self):
|
||||
return self._hash
|
||||
|
||||
|
||||
def filter(self, filter_string):
|
||||
if len(filter_string) == 0:
|
||||
return True
|
||||
|
|
@ -951,10 +948,6 @@ class MCGAlbum:
|
|||
return 1
|
||||
|
||||
|
||||
def _set_hash(self):
|
||||
self._hash = MCGAlbum.hash(self._title)
|
||||
|
||||
|
||||
def _find_cover(self):
|
||||
names = list(MCGAlbum._FILE_NAMES)
|
||||
names.append(self._title)
|
||||
|
|
@ -1028,6 +1021,10 @@ class MCGTrack:
|
|||
return self._file == other.get_file()
|
||||
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self._file)
|
||||
|
||||
|
||||
def get_artists(self):
|
||||
if self._albumartists:
|
||||
return [artist for artist in self._artists if artist not in self._albumartists]
|
||||
|
|
@ -1161,7 +1158,7 @@ class MCGCache():
|
|||
|
||||
|
||||
def create_filename(self, album):
|
||||
return os.path.join(self._dirname, '-'.join([album.get_hash()]))
|
||||
return os.path.join(self._dirname, '-'.join([album.get_id()]))
|
||||
|
||||
|
||||
def _read_size(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue