models: Add Identifer type which interns values.
This gives some moderate memory saving since the values are used in multiple places.
This commit is contained in:
parent
168c10448b
commit
08fd99ffdb
@ -75,6 +75,11 @@ class String(Field):
|
|||||||
super(String, self).__init__(type=basestring, default=default)
|
super(String, self).__init__(type=basestring, default=default)
|
||||||
|
|
||||||
|
|
||||||
|
class Identifier(String):
|
||||||
|
def validate(self, value):
|
||||||
|
return intern(str(super(Identifier, self).validate(value)))
|
||||||
|
|
||||||
|
|
||||||
class Integer(Field):
|
class Integer(Field):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -290,7 +295,7 @@ class Ref(ImmutableObject):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#: The object URI. Read-only.
|
#: The object URI. Read-only.
|
||||||
uri = String()
|
uri = Identifier()
|
||||||
|
|
||||||
#: The object name. Read-only.
|
#: The object name. Read-only.
|
||||||
name = String()
|
name = String()
|
||||||
@ -354,7 +359,7 @@ class Image(ImmutableObject):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#: The image URI. Read-only.
|
#: The image URI. Read-only.
|
||||||
uri = String()
|
uri = Identifier()
|
||||||
|
|
||||||
#: Optional width of the image or :class:`None`. Read-only.
|
#: Optional width of the image or :class:`None`. Read-only.
|
||||||
width = Integer(min=0)
|
width = Integer(min=0)
|
||||||
@ -375,13 +380,13 @@ class Artist(ImmutableObject):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#: The artist URI. Read-only.
|
#: The artist URI. Read-only.
|
||||||
uri = String()
|
uri = Identifier()
|
||||||
|
|
||||||
#: The artist name. Read-only.
|
#: The artist name. Read-only.
|
||||||
name = String()
|
name = String()
|
||||||
|
|
||||||
#: The MusicBrainz ID of the artist. Read-only.
|
#: The MusicBrainz ID of the artist. Read-only.
|
||||||
musicbrainz_id = String()
|
musicbrainz_id = Identifier()
|
||||||
|
|
||||||
|
|
||||||
class Album(ImmutableObject):
|
class Album(ImmutableObject):
|
||||||
@ -406,7 +411,7 @@ class Album(ImmutableObject):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#: The album URI. Read-only.
|
#: The album URI. Read-only.
|
||||||
uri = String()
|
uri = Identifier()
|
||||||
|
|
||||||
#: The album name. Read-only.
|
#: The album name. Read-only.
|
||||||
name = String()
|
name = String()
|
||||||
@ -424,7 +429,7 @@ class Album(ImmutableObject):
|
|||||||
date = String() # TODO: add date type
|
date = String() # TODO: add date type
|
||||||
|
|
||||||
#: The MusicBrainz ID of the album. Read-only.
|
#: The MusicBrainz ID of the album. Read-only.
|
||||||
musicbrainz_id = String()
|
musicbrainz_id = Identifier()
|
||||||
|
|
||||||
#: The album image URIs. Read-only.
|
#: The album image URIs. Read-only.
|
||||||
images = Collection(type=basestring, container=frozenset)
|
images = Collection(type=basestring, container=frozenset)
|
||||||
@ -469,7 +474,7 @@ class Track(ImmutableObject):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#: The track URI. Read-only.
|
#: The track URI. Read-only.
|
||||||
uri = String()
|
uri = Identifier()
|
||||||
|
|
||||||
#: The track name. Read-only.
|
#: The track name. Read-only.
|
||||||
name = String()
|
name = String()
|
||||||
@ -508,7 +513,7 @@ class Track(ImmutableObject):
|
|||||||
comment = String()
|
comment = String()
|
||||||
|
|
||||||
#: The MusicBrainz ID of the track. Read-only.
|
#: The MusicBrainz ID of the track. Read-only.
|
||||||
musicbrainz_id = String()
|
musicbrainz_id = Identifier()
|
||||||
|
|
||||||
#: Integer representing when the track was last modified. Exact meaning
|
#: Integer representing when the track was last modified. Exact meaning
|
||||||
#: depends on source of track. For local files this is the modification
|
#: depends on source of track. For local files this is the modification
|
||||||
@ -571,7 +576,7 @@ class Playlist(ImmutableObject):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#: The playlist URI. Read-only.
|
#: The playlist URI. Read-only.
|
||||||
uri = String()
|
uri = Identifier()
|
||||||
|
|
||||||
#: The playlist name. Read-only.
|
#: The playlist name. Read-only.
|
||||||
name = String()
|
name = String()
|
||||||
@ -607,7 +612,7 @@ class SearchResult(ImmutableObject):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# The search result URI. Read-only.
|
# The search result URI. Read-only.
|
||||||
uri = String()
|
uri = Identifier()
|
||||||
|
|
||||||
# The tracks matching the search query. Read-only.
|
# The tracks matching the search query. Read-only.
|
||||||
tracks = Collection(type=Track, container=tuple)
|
tracks = Collection(type=Track, container=tuple)
|
||||||
|
|||||||
@ -86,7 +86,7 @@ class RefTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_repr_without_results(self):
|
def test_repr_without_results(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"Ref(name=u'foo', type=u'artist', uri=u'uri')",
|
"Ref(name=u'foo', type=u'artist', uri='uri')",
|
||||||
repr(Ref(uri='uri', name='foo', type='artist')))
|
repr(Ref(uri='uri', name='foo', type='artist')))
|
||||||
|
|
||||||
def test_serialize_without_results(self):
|
def test_serialize_without_results(self):
|
||||||
@ -200,7 +200,7 @@ class ArtistTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"Artist(name=u'name', uri=u'uri')",
|
"Artist(name=u'name', uri='uri')",
|
||||||
repr(Artist(uri='uri', name='name')))
|
repr(Artist(uri='uri', name='name')))
|
||||||
|
|
||||||
def test_serialize(self):
|
def test_serialize(self):
|
||||||
@ -365,12 +365,12 @@ class AlbumTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_repr_without_artists(self):
|
def test_repr_without_artists(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"Album(name=u'name', uri=u'uri')",
|
"Album(name=u'name', uri='uri')",
|
||||||
repr(Album(uri='uri', name='name')))
|
repr(Album(uri='uri', name='name')))
|
||||||
|
|
||||||
def test_repr_with_artists(self):
|
def test_repr_with_artists(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"Album(artists=[Artist(name=u'foo')], name=u'name', uri=u'uri')",
|
"Album(artists=[Artist(name=u'foo')], name=u'name', uri='uri')",
|
||||||
repr(Album(uri='uri', name='name', artists=[Artist(name='foo')])))
|
repr(Album(uri='uri', name='name', artists=[Artist(name='foo')])))
|
||||||
|
|
||||||
def test_serialize_without_artists(self):
|
def test_serialize_without_artists(self):
|
||||||
@ -609,12 +609,12 @@ class TrackTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_repr_without_artists(self):
|
def test_repr_without_artists(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"Track(name=u'name', uri=u'uri')",
|
"Track(name=u'name', uri='uri')",
|
||||||
repr(Track(uri='uri', name='name')))
|
repr(Track(uri='uri', name='name')))
|
||||||
|
|
||||||
def test_repr_with_artists(self):
|
def test_repr_with_artists(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"Track(artists=[Artist(name=u'foo')], name=u'name', uri=u'uri')",
|
"Track(artists=[Artist(name=u'foo')], name=u'name', uri='uri')",
|
||||||
repr(Track(uri='uri', name='name', artists=[Artist(name='foo')])))
|
repr(Track(uri='uri', name='name', artists=[Artist(name='foo')])))
|
||||||
|
|
||||||
def test_serialize_without_artists(self):
|
def test_serialize_without_artists(self):
|
||||||
@ -844,7 +844,7 @@ class TlTrackTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"TlTrack(tlid=123, track=Track(uri=u'uri'))",
|
"TlTrack(tlid=123, track=Track(uri='uri'))",
|
||||||
repr(TlTrack(tlid=123, track=Track(uri='uri'))))
|
repr(TlTrack(tlid=123, track=Track(uri='uri'))))
|
||||||
|
|
||||||
def test_serialize(self):
|
def test_serialize(self):
|
||||||
@ -977,12 +977,12 @@ class PlaylistTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_repr_without_tracks(self):
|
def test_repr_without_tracks(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"Playlist(name=u'name', uri=u'uri')",
|
"Playlist(name=u'name', uri='uri')",
|
||||||
repr(Playlist(uri='uri', name='name')))
|
repr(Playlist(uri='uri', name='name')))
|
||||||
|
|
||||||
def test_repr_with_tracks(self):
|
def test_repr_with_tracks(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"Playlist(name=u'name', tracks=[Track(name=u'foo')], uri=u'uri')",
|
"Playlist(name=u'name', tracks=[Track(name=u'foo')], uri='uri')",
|
||||||
repr(Playlist(uri='uri', name='name', tracks=[Track(name='foo')])))
|
repr(Playlist(uri='uri', name='name', tracks=[Track(name='foo')])))
|
||||||
|
|
||||||
def test_serialize_without_tracks(self):
|
def test_serialize_without_tracks(self):
|
||||||
@ -1114,7 +1114,7 @@ class SearchResultTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_repr_without_results(self):
|
def test_repr_without_results(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"SearchResult(uri=u'uri')",
|
"SearchResult(uri='uri')",
|
||||||
repr(SearchResult(uri='uri')))
|
repr(SearchResult(uri='uri')))
|
||||||
|
|
||||||
def test_serialize_without_results(self):
|
def test_serialize_without_results(self):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user