moved caching attributes related code to LastfmBase.cachedProperty decorator

master
Abhinav Sarkar 2008-08-25 13:18:17 +00:00
parent 439674558a
commit 3bb7aca74f
7 changed files with 309 additions and 371 deletions

View File

@ -90,25 +90,23 @@ class Album(LastfmBase):
self._fillInfo() self._fillInfo()
return self.__stats return self.__stats
@property @LastfmBase.cachedProperty
def topTags(self): def topTags(self):
"""top tags for the album""" """top tags for the album"""
if self.__topTags is None: params = {'method': 'album.getinfo'}
params = {'method': 'album.getinfo'} if self.artist and self.name:
if self.artist and self.name: params.update({'artist': self.artist.name, 'album': self.name})
params.update({'artist': self.artist.name, 'album': self.name}) elif self.mbid:
elif self.mbid: params.update({'mbid': self.mbid})
params.update({'mbid': self.mbid}) data = self.__api._fetchData(params).find('album')
data = self.__api._fetchData(params).find('album') return [
self.__topTags = [ Tag(
Tag( self.__api,
self.__api, name = t.findtext('name'),
name = t.findtext('name'), url = t.findtext('url')
url = t.findtext('url') )
) for t in data.findall('toptags/tag')
for t in data.findall('toptags/tag') ]
]
return self.__topTags
@LastfmBase.topProperty("topTags") @LastfmBase.topProperty("topTags")
def topTag(self): def topTag(self):

View File

@ -42,10 +42,6 @@ class Artist(LastfmBase):
summary = bio.summary, summary = bio.summary,
content = bio.content content = bio.content
) )
self.__events = None
self.__topAlbums = None
self.__topTracks = None
self.__topFans = None
@property @property
def name(self): def name(self):
@ -151,100 +147,92 @@ class Artist(LastfmBase):
self._fillInfo() self._fillInfo()
return self.__bio return self.__bio
@property @LastfmBase.cachedProperty
def events(self): def events(self):
"""events for the artist""" """events for the artist"""
if self.__events is None: params = {'method': 'artist.getevents', 'artist': self.name}
params = {'method': 'artist.getevents', 'artist': self.name} data = self.__api._fetchData(params).find('events')
data = self.__api._fetchData(params).find('events')
self.__events = [ return [
Event.createFromData(self.__api, e) Event.createFromData(self.__api, e)
for e in data.findall('event') for e in data.findall('event')
] ]
return self.__events
@property @LastfmBase.cachedProperty
def topAlbums(self): def topAlbums(self):
"""top albums of the artist""" """top albums of the artist"""
if self.__topAlbums is None: params = {'method': 'artist.gettopalbums', 'artist': self.name}
params = {'method': 'artist.gettopalbums', 'artist': self.name} data = self.__api._fetchData(params).find('topalbums')
data = self.__api._fetchData(params).find('topalbums')
self.__topAlbums = [ return [
Album( Album(
self.__api, self.__api,
name = a.findtext('name'), name = a.findtext('name'),
artist = self, artist = self,
mbid = a.findtext('mbid'), mbid = a.findtext('mbid'),
url = a.findtext('url'), url = a.findtext('url'),
image = dict([(i.get('size'), i.text) for i in a.findall('image')]), image = dict([(i.get('size'), i.text) for i in a.findall('image')]),
stats = Stats( stats = Stats(
subject = a.findtext('name'), subject = a.findtext('name'),
playcount = int(a.findtext('playcount')), playcount = int(a.findtext('playcount')),
rank = int(a.attrib['rank']) rank = int(a.attrib['rank'])
) )
) )
for a in data.findall('album') for a in data.findall('album')
] ]
return self.__topAlbums
@LastfmBase.topProperty("topAlbums") @LastfmBase.topProperty("topAlbums")
def topAlbum(self): def topAlbum(self):
"""top album of the artist""" """top album of the artist"""
pass pass
@property @LastfmBase.cachedProperty
def topFans(self): def topFans(self):
"""top fans of the artist""" """top fans of the artist"""
if self.__topFans is None: params = {'method': 'artist.gettopfans', 'artist': self.name}
params = {'method': 'artist.gettopfans', 'artist': self.name} data = self.__api._fetchData(params).find('topfans')
data = self.__api._fetchData(params).find('topfans') return [
self.__topFans = [ User(
User( self.__api,
self.__api, name = u.findtext('name'),
name = u.findtext('name'), url = u.findtext('url'),
url = u.findtext('url'), image = dict([(i.get('size'), i.text) for i in u.findall('image')]),
image = dict([(i.get('size'), i.text) for i in u.findall('image')]), stats = Stats(
stats = Stats( subject = u.findtext('name'),
subject = u.findtext('name'), weight = int(u.findtext('weight'))
weight = int(u.findtext('weight')) )
) )
) for u in data.findall('user')
for u in data.findall('user') ]
]
return self.__topFans
@LastfmBase.topProperty("topFans") @LastfmBase.topProperty("topFans")
def topFan(self): def topFan(self):
"""top fan of the artist""" """top fan of the artist"""
pass pass
@property @LastfmBase.cachedProperty
def topTracks(self): def topTracks(self):
"""top tracks of the artist""" """top tracks of the artist"""
if self.__topTracks is None: params = {'method': 'artist.gettoptracks', 'artist': self.name}
params = {'method': 'artist.gettoptracks', 'artist': self.name} data = self.__api._fetchData(params).find('toptracks')
data = self.__api._fetchData(params).find('toptracks') return [
self.__topTracks = [ Track(
Track( self.__api,
self.__api, name = t.findtext('name'),
name = t.findtext('name'), artist = self,
artist = self, mbid = t.findtext('mbid'),
mbid = t.findtext('mbid'), stats = Stats(
stats = Stats( subject = t.findtext('name'),
subject = t.findtext('name'), playcount = int(t.findtext('playcount')),
playcount = int(t.findtext('playcount')), rank = int(t.attrib['rank'])
rank = int(t.attrib['rank']) ),
), streamable = (t.findtext('streamable') == '1'),
streamable = (t.findtext('streamable') == '1'), fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'),
fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'), image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
image = dict([(i.get('size'), i.text) for i in t.findall('image')]), )
) for t in data.findall('track')
for t in data.findall('track') ]
]
return self.__topTracks
@LastfmBase.topProperty("topTracks") @LastfmBase.topProperty("topTracks")
def topTrack(self): def topTrack(self):
"""topmost fan of the artist""" """topmost fan of the artist"""

View File

@ -54,6 +54,20 @@ class LastfmBase(object):
return property(fget = wrapper, doc = func.__doc__) return property(fget = wrapper, doc = func.__doc__)
return decorator return decorator
@staticmethod
def cachedProperty(func):
frame = sys._getframe(1)
classname = frame.f_code.co_name
funcName = func.func_code.co_name
attributeName = "_%s__%s" % (classname, funcName)
def wrapper(ob):
cacheAttribute = getattr(ob, attributeName, None)
if cacheAttribute is None:
setattr(ob, attributeName, func(ob))
return cacheAttribute
return property(fget = wrapper, doc = func.__doc__)
def __gt__(self, other): def __gt__(self, other):
return not (self.__lt__(other) or self.__eq(other)) return not (self.__lt__(other) or self.__eq(other))
@ -66,3 +80,5 @@ class LastfmBase(object):
def __le__(self, other): def __le__(self, other):
return not self.__gt__(other) return not self.__gt__(other)
import sys

View File

@ -151,7 +151,6 @@ class Location(LastfmBase):
self.__latitude = latitude self.__latitude = latitude
self.__longitude = longitude self.__longitude = longitude
self.__timezone = timezone self.__timezone = timezone
self.__events = None
@property @property
def name(self): def name(self):
@ -198,13 +197,11 @@ class Location(LastfmBase):
page = None): page = None):
return Geo.getEvents(self.__api, self.name, distance, page).matches return Geo.getEvents(self.__api, self.name, distance, page).matches
@property @LastfmBase.cachedProperty
def events(self): def events(self):
"""events taking place at/around the location""" """events taking place at/around the location"""
if self.__events is None: return self.getEvents()
self.__events = self.getEvents()
return self.__events
@staticmethod @staticmethod
def hashFunc(*args, **kwds): def hashFunc(*args, **kwds):
try: try:
@ -247,33 +244,27 @@ class Country(LastfmBase):
raise LastfmError("api reference must be supplied as an argument") raise LastfmError("api reference must be supplied as an argument")
self.__api = api self.__api = api
self.__name = name self.__name = name
self.__topArtists = None
self.__topTracks = None
@property @property
def name(self): def name(self):
"""name of the country""" """name of the country"""
return self.__name return self.__name
@property @LastfmBase.cachedProperty
def topArtists(self): def topArtists(self):
"""top artists of the country""" """top artists of the country"""
if self.__topArtists is None: return Geo.getTopArtists(self.__api, self.name)
self.__topArtists = Geo.getTopArtists(self.__api, self.name)
return self.__topArtists
@LastfmBase.topProperty("topArtists") @LastfmBase.topProperty("topArtists")
def topArtist(self): def topArtist(self):
"""top artist of the country""" """top artist of the country"""
pass pass
@property @LastfmBase.cachedProperty
def topTracks(self): def topTracks(self):
"""top tracks of the country""" """top tracks of the country"""
if self.__topTracks is None: return Geo.getTopTracks(self.__api, self.name)
self.__topTracks = Geo.getTopTracks(self.__api, self.name)
return self.__topTracks
@LastfmBase.topProperty("topTracks") @LastfmBase.topProperty("topTracks")
def topTrack(self): def topTrack(self):
"""top track of the country""" """top track of the country"""

View File

@ -24,11 +24,7 @@ class Tag(LastfmBase):
subject = self, subject = self,
count = stats.count count = stats.count
) )
self.__similar = None
self.__topAlbums = None
self.__topArtists = None
self.__topTracks = None
@property @property
def name(self): def name(self):
"""name of the tag""" """name of the tag"""
@ -48,121 +44,113 @@ class Tag(LastfmBase):
def stats(self): def stats(self):
return self.__stats return self.__stats
@property @LastfmBase.cachedProperty
def similar(self): def similar(self):
"""tags similar to this tag""" """tags similar to this tag"""
if self.__similar is None: params = {'method': 'tag.getsimilar', 'tag': self.name}
params = {'method': 'tag.getsimilar', 'tag': self.name} data = self.__api._fetchData(params).find('similartags')
data = self.__api._fetchData(params).find('similartags') return [
self.__similar = [ Tag(
Tag( self.__api,
self.__api, name = t.findtext('name'),
name = t.findtext('name'), url = t.findtext('url'),
url = t.findtext('url'), streamable = (t.findtext('streamable') == "1"),
streamable = (t.findtext('streamable') == "1"), )
) for t in data.findall('tag')
for t in data.findall('tag') ]
]
return self.__similar
@LastfmBase.topProperty("similar") @LastfmBase.topProperty("similar")
def mostSimilar(self): def mostSimilar(self):
"""most similar tag to this tag""" """most similar tag to this tag"""
pass pass
@property @LastfmBase.cachedProperty
def topAlbums(self): def topAlbums(self):
"""top albums for the tag""" """top albums for the tag"""
if self.__topAlbums is None: params = {'method': 'tag.gettopalbums', 'tag': self.name}
params = {'method': 'tag.gettopalbums', 'tag': self.name} data = self.__api._fetchData(params).find('topalbums')
data = self.__api._fetchData(params).find('topalbums') return [
self.__topAlbums = [ Album(
Album( self.__api,
name = a.findtext('name'),
artist = Artist(
self.__api, self.__api,
name = a.findtext('name'), name = a.findtext('artist/name'),
artist = Artist( mbid = a.findtext('artist/mbid'),
self.__api, url = a.findtext('artist/url'),
name = a.findtext('artist/name'), ),
mbid = a.findtext('artist/mbid'), mbid = a.findtext('mbid'),
url = a.findtext('artist/url'), url = a.findtext('url'),
), image = dict([(i.get('size'), i.text) for i in a.findall('image')]),
mbid = a.findtext('mbid'), stats = Stats(
url = a.findtext('url'), subject = a.findtext('name'),
image = dict([(i.get('size'), i.text) for i in a.findall('image')]), tagcount = a.findtext('tagcount') and int(a.findtext('tagcount')) or None,
stats = Stats( rank = a.attrib['rank'].strip() and int(a.attrib['rank']) or None
subject = a.findtext('name'), )
tagcount = a.findtext('tagcount') and int(a.findtext('tagcount')) or None, )
rank = a.attrib['rank'].strip() and int(a.attrib['rank']) or None for a in data.findall('album')
) ]
)
for a in data.findall('album')
]
return self.__topAlbums
@LastfmBase.topProperty("topAlbums") @LastfmBase.topProperty("topAlbums")
def topAlbum(self): def topAlbum(self):
"""top album for the tag""" """top album for the tag"""
pass pass
@property @LastfmBase.cachedProperty
def topArtists(self): def topArtists(self):
"""top artists for the tag""" """top artists for the tag"""
if self.__topArtists is None: params = {'method': 'tag.gettopartists', 'tag': self.name}
params = {'method': 'tag.gettopartists', 'tag': self.name} data = self.__api._fetchData(params).find('topartists')
data = self.__api._fetchData(params).find('topartists') return [
self.__topArtists = [ Artist(
Artist( self.__api,
self.__api, name = a.findtext('name'),
name = a.findtext('name'), mbid = a.findtext('mbid'),
mbid = a.findtext('mbid'), stats = Stats(
stats = Stats( subject = a.findtext('name'),
subject = a.findtext('name'), rank = a.attrib['rank'].strip() and int(a.attrib['rank']) or None,
rank = a.attrib['rank'].strip() and int(a.attrib['rank']) or None, tagcount = a.findtext('tagcount') and int(a.findtext('tagcount')) or None
tagcount = a.findtext('tagcount') and int(a.findtext('tagcount')) or None ),
), url = a.findtext('url'),
url = a.findtext('url'), streamable = (a.findtext('streamable') == "1"),
streamable = (a.findtext('streamable') == "1"), image = dict([(i.get('size'), i.text) for i in a.findall('image')]),
image = dict([(i.get('size'), i.text) for i in a.findall('image')]), )
) for a in data.findall('artist')
for a in data.findall('artist') ]
]
return self.__topArtists
@LastfmBase.topProperty("topArtists") @LastfmBase.topProperty("topArtists")
def topArtist(self): def topArtist(self):
"""top artist for the tag""" """top artist for the tag"""
pass pass
@property @LastfmBase.cachedProperty
def topTracks(self): def topTracks(self):
"""top tracks for the tag""" """top tracks for the tag"""
if self.__topTracks is None: params = {'method': 'tag.gettoptracks', 'tag': self.name}
params = {'method': 'tag.gettoptracks', 'tag': self.name} data = self.__api._fetchData(params).find('toptracks')
data = self.__api._fetchData(params).find('toptracks') return [
self.__topTracks = [ Track(
Track( self.__api,
name = t.findtext('name'),
artist = Artist(
self.__api, self.__api,
name = t.findtext('name'), name = t.findtext('artist/name'),
artist = Artist( mbid = t.findtext('artist/mbid'),
self.__api, url = t.findtext('artist/url'),
name = t.findtext('artist/name'), ),
mbid = t.findtext('artist/mbid'), mbid = t.findtext('mbid'),
url = t.findtext('artist/url'), stats = Stats(
), subject = t.findtext('name'),
mbid = t.findtext('mbid'), rank = t.attrib['rank'].strip() and int(t.attrib['rank']) or None,
stats = Stats( tagcount = t.findtext('tagcount') and int(t.findtext('tagcount')) or None
subject = t.findtext('name'), ),
rank = t.attrib['rank'].strip() and int(t.attrib['rank']) or None, streamable = (t.findtext('streamable') == '1'),
tagcount = t.findtext('tagcount') and int(t.findtext('tagcount')) or None fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'),
), image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
streamable = (t.findtext('streamable') == '1'), )
fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'), for t in data.findall('track')
image = dict([(i.get('size'), i.text) for i in t.findall('image')]), ]
)
for t in data.findall('track')
]
return self.__topTracks
@LastfmBase.topProperty("topTracks") @LastfmBase.topProperty("topTracks")
def topTrack(self): def topTrack(self):
"""top track for the tag""" """top track for the tag"""

View File

@ -41,10 +41,7 @@ class Track(LastfmBase):
self.__fullTrack = fullTrack self.__fullTrack = fullTrack
self.__playedOn = playedOn self.__playedOn = playedOn
self.__lovedOn = lovedOn self.__lovedOn = lovedOn
self.__similar = None
self.__topFans = None
self.__topTags = None
@property @property
def name(self): def name(self):
"""name of the track""" """name of the track"""
@ -114,100 +111,94 @@ class Track(LastfmBase):
params.update({'mbid': mbid}) params.update({'mbid': mbid})
return params return params
@property @LastfmBase.cachedProperty
def similar(self): def similar(self):
"""tracks similar to this track""" """tracks similar to this track"""
if self.__similar is None: params = self.__checkParams(
params = self.__checkParams( {'method': 'track.getsimilar'},
{'method': 'track.getsimilar'}, self.artist.name,
self.artist.name, self.name,
self.name, self.mbid
self.mbid )
) data = self.__api._fetchData(params).find('similartracks')
data = self.__api._fetchData(params).find('similartracks') return [
self.__similar = [ Track(
Track( self.__api,
self.__api, name = t.findtext('name'),
name = t.findtext('name'), artist = Artist(
artist = Artist( self.__api,
self.__api, name = t.findtext('artist/name'),
name = t.findtext('artist/name'), mbid = t.findtext('artist/mbid'),
mbid = t.findtext('artist/mbid'), url = t.findtext('artist/url')
url = t.findtext('artist/url') ),
), mbid = t.findtext('mbid'),
mbid = t.findtext('mbid'), stats = Stats(
stats = Stats( subject = t.findtext('name'),
subject = t.findtext('name'), match = float(t.findtext('match'))
match = float(t.findtext('match')) ),
), streamable = (t.findtext('streamable') == '1'),
streamable = (t.findtext('streamable') == '1'), fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'),
fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'), image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
image = dict([(i.get('size'), i.text) for i in t.findall('image')]), )
) for t in data.findall('track')
for t in data.findall('track') ]
]
return self.__similar
@LastfmBase.topProperty("similar") @LastfmBase.topProperty("similar")
def mostSimilar(self): def mostSimilar(self):
"""track most similar to this track""" """track most similar to this track"""
pass pass
@property @LastfmBase.cachedProperty
def topFans(self): def topFans(self):
"""top fans of the track""" """top fans of the track"""
if self.__topFans is None: params = self.__checkParams(
params = self.__checkParams( {'method': 'track.gettopfans'},
{'method': 'track.gettopfans'}, self.artist.name,
self.artist.name, self.name,
self.name, self.mbid
self.mbid )
) data = self.__api._fetchData(params).find('topfans')
data = self.__api._fetchData(params).find('topfans') return [
self.__topFans = [ User(
User( self.__api,
self.__api, name = u.findtext('name'),
name = u.findtext('name'), url = u.findtext('url'),
url = u.findtext('url'), image = dict([(i.get('size'), i.text) for i in u.findall('image')]),
image = dict([(i.get('size'), i.text) for i in u.findall('image')]), stats = Stats(
stats = Stats( subject = u.findtext('name'),
subject = u.findtext('name'), weight = int(u.findtext('weight'))
weight = int(u.findtext('weight'))
)
) )
for u in data.findall('user') )
] for u in data.findall('user')
return self.__topFans ]
@LastfmBase.topProperty("topFans") @LastfmBase.topProperty("topFans")
def topFan(self): def topFan(self):
"""topmost fan of the track""" """topmost fan of the track"""
pass pass
@property @LastfmBase.cachedProperty
def topTags(self): def topTags(self):
"""top tags for the track""" """top tags for the track"""
if self.__topTags is None: params = self.__checkParams(
params = self.__checkParams( {'method': 'track.gettoptags'},
{'method': 'track.gettoptags'}, self.artist.name,
self.artist.name, self.name,
self.name, self.mbid
self.mbid )
) data = self.__api._fetchData(params).find('toptags')
data = self.__api._fetchData(params).find('toptags') return [
self.__topTags = [ Tag(
Tag( self.__api,
self.__api, name = t.findtext('name'),
name = t.findtext('name'), url = t.findtext('url'),
url = t.findtext('url'), stats = Stats(
stats = Stats( subject = t.findtext('name'),
subject = t.findtext('name'), count = int(t.findtext('count')),
count = int(t.findtext('count')),
)
) )
for t in data.findall('tag') )
] for t in data.findall('tag')
return self.__topTags ]
@LastfmBase.topProperty("topTags") @LastfmBase.topProperty("topTags")
def topTag(self): def topTag(self):

View File

@ -25,15 +25,6 @@ class User(LastfmBase):
match = stats.match, match = stats.match,
weight = stats.weight weight = stats.weight
) )
self.__events = None
self.__pastEvents = None
self.__friends = None
self.__neighbours = None
self.__lovedTracks = None
self.__topAlbums = None
self.__topArtists = None
self.__topTracks = None
self.__topTags = None
self.__lirary = User.Library(api, self) self.__lirary = User.Library(api, self)
@property @property
@ -56,18 +47,16 @@ class User(LastfmBase):
"""stats for the user""" """stats for the user"""
return self.__stats return self.__stats
@property @LastfmBase.cachedProperty
def events(self): def events(self):
if self.__events is None: params = {'method': 'user.getevents', 'user': self.name}
params = {'method': 'user.getevents', 'user': self.name} data = self.__api._fetchData(params).find('events')
data = self.__api._fetchData(params).find('events')
self.__events = [ return [
Event.createFromData(self.__api, e) Event.createFromData(self.__api, e)
for e in data.findall('event') for e in data.findall('event')
] ]
return self.__events
def getPastEvents(self, def getPastEvents(self,
page = None, page = None,
limit = None): limit = None):
@ -83,11 +72,9 @@ class User(LastfmBase):
for e in data.findall('event') for e in data.findall('event')
] ]
@property @LastfmBase.cachedProperty
def pastEvents(self): def pastEvents(self):
if self.__pastEvents is None: return self.getPastEvents()
self.__pastEvents = self.getPastEvents()
return self.__pastEvents
def getFriends(self, def getFriends(self,
limit = None): limit = None):
@ -106,13 +93,11 @@ class User(LastfmBase):
] ]
@property @LastfmBase.cachedProperty
def friends(self): def friends(self):
"""friends of the user""" """friends of the user"""
if self.__friends is None: return self.getFriends()
self.__friends = self.getFriends()
return self.__friends
def getNeighbours(self, limit = None): def getNeighbours(self, limit = None):
params = {'method': 'user.getneighbours', 'user': self.name} params = {'method': 'user.getneighbours', 'user': self.name}
if limit is not None: if limit is not None:
@ -132,12 +117,10 @@ class User(LastfmBase):
for u in data.findall('user') for u in data.findall('user')
] ]
@property @LastfmBase.cachedProperty
def neighbours(self): def neighbours(self):
"""neighbours of the user""" """neighbours of the user"""
if self.__neighbours is None: return self.getNeighbours()
self.__neighbours = self.getNeighbours()
return self.__neighbours
@LastfmBase.topProperty("neighbours") @LastfmBase.topProperty("neighbours")
def nearestNeighbour(self): def nearestNeighbour(self):
@ -149,34 +132,32 @@ class User(LastfmBase):
"""playlists of the user""" """playlists of the user"""
pass pass
@property @LastfmBase.cachedProperty
def lovedTracks(self): def lovedTracks(self):
if self.__lovedTracks is None: params = {'method': 'user.getlovedtracks', 'user': self.name}
params = {'method': 'user.getlovedtracks', 'user': self.name} data = self.__api._fetchData(params).find('lovedtracks')
data = self.__api._fetchData(params).find('lovedtracks') return [
self.__lovedTracks = [ Track(
Track( self.__api,
name = t.findtext('name'),
artist = Artist(
self.__api, self.__api,
name = t.findtext('name'), name = t.findtext('artist/name'),
artist = Artist( mbid = t.findtext('artist/mbid'),
self.__api, url = t.findtext('artist/url'),
name = t.findtext('artist/name'), ),
mbid = t.findtext('artist/mbid'), mbid = t.findtext('mbid'),
url = t.findtext('artist/url'), image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
), lovedOn = datetime(*(
mbid = t.findtext('mbid'), time.strptime(
image = dict([(i.get('size'), i.text) for i in t.findall('image')]), t.findtext('date').strip(),
lovedOn = datetime(*( '%d %b %Y, %H:%M'
time.strptime( )[0:6])
t.findtext('date').strip(),
'%d %b %Y, %H:%M'
)[0:6])
)
) )
for t in data.findall('track') )
] for t in data.findall('track')
return self.__lovedTracks ]
def getRecentTracks(self, limit = None): def getRecentTracks(self, limit = None):
params = {'method': 'user.getrecenttracks', 'user': self.name} params = {'method': 'user.getrecenttracks', 'user': self.name}
data = self.__api._fetchData(params, no_cache = True).find('recenttracks') data = self.__api._fetchData(params, no_cache = True).find('recenttracks')
@ -251,13 +232,11 @@ class User(LastfmBase):
for a in data.findall('album') for a in data.findall('album')
] ]
@property @LastfmBase.cachedProperty
def topAlbums(self): def topAlbums(self):
"""overall top albums of the user""" """overall top albums of the user"""
if self.__topAlbums is None: return self.getTopAlbums()
self.__topAlbums = self.getTopAlbums()
return self.__topAlbums
@LastfmBase.topProperty("topAlbums") @LastfmBase.topProperty("topAlbums")
def topAlbum(self): def topAlbum(self):
"""overall top most album of the user""" """overall top most album of the user"""
@ -286,13 +265,11 @@ class User(LastfmBase):
for a in data.findall('artist') for a in data.findall('artist')
] ]
@property @LastfmBase.cachedProperty
def topArtists(self): def topArtists(self):
"""top artists of the user""" """top artists of the user"""
if self.__topArtists is None: return self.getTopArtists()
self.__topArtists = self.getTopArtists()
return self.__topArtists
@LastfmBase.topProperty("topArtists") @LastfmBase.topProperty("topArtists")
def topArtist(self): def topArtist(self):
"""top artist of the user""" """top artist of the user"""
@ -326,13 +303,11 @@ class User(LastfmBase):
for t in data.findall('track') for t in data.findall('track')
] ]
@property @LastfmBase.cachedProperty
def topTracks(self): def topTracks(self):
"""top tracks of the user""" """top tracks of the user"""
if self.__topTracks is None: return self.getTopTracks()
self.__topTracks = self.getTopTracks()
return self.__topTracks
@LastfmBase.topProperty("topTracks") @LastfmBase.topProperty("topTracks")
def topTrack(self): def topTrack(self):
"""top track of the user""" """top track of the user"""
@ -356,13 +331,11 @@ class User(LastfmBase):
for t in data.findall('tag') for t in data.findall('tag')
] ]
@property @LastfmBase.cachedProperty
def topTags(self): def topTags(self):
"""top tags of the user""" """top tags of the user"""
if self.__topTags is None: return self.getTopTags()
self.__topTags = self.getTopTags()
return self.__topTags
@LastfmBase.topProperty("topTags") @LastfmBase.topProperty("topTags")
def topTag(self): def topTag(self):
"""top tag of the user""" """top tag of the user"""
@ -423,12 +396,10 @@ class User(LastfmBase):
return "<lastfm.User: %s>" % self.name return "<lastfm.User: %s>" % self.name
class Library(object): class Library(object):
"""A class representing the music library of the user."""
def __init__(self, api, user): def __init__(self, api, user):
self.__api = api self.__api = api
self.__user = user self.__user = user
self.__albums = None
self.__artists = None
self.__tracks = None
@property @property
def user(self): def user(self):
@ -465,12 +436,10 @@ class User(LastfmBase):
] ]
} }
@property @LastfmBase.cachedProperty
def albums(self): def albums(self):
if self.__albums is None: return self.getAlbums()['albums']
self.__albums = self.getAlbums()['albums']
return self.__albums
def getArtists(self, def getArtists(self,
limit = None, limit = None,
page = None): page = None):
@ -498,11 +467,9 @@ class User(LastfmBase):
] ]
} }
@property @LastfmBase.cachedProperty
def artists(self): def artists(self):
if self.__artists is None: return self.getArtists()['artists']
self.__artists = self.getArtists()['artists']
return self.__artists
def getTracks(self, def getTracks(self,
limit = None, limit = None,
@ -537,12 +504,10 @@ class User(LastfmBase):
] ]
} }
@property @LastfmBase.cachedProperty
def tracks(self): def tracks(self):
if self.__tracks is None: return self.getTracks()['tracks']
self.__tracks = self.getTracks()['tracks']
return self.__tracks
def _fetchData(self, params, limit, page): def _fetchData(self, params, limit, page):
params .update({'user': self.user.name}) params .update({'user': self.user.name})
if limit is not None: if limit is not None:
@ -571,3 +536,4 @@ from track import Track
#extract self.__* property as decorator #extract self.__* property as decorator
#write depaginations #write depaginations
#write exceptions #write exceptions
#argument type checking