These immutable data models are used for all data transfer within the Mopidy backends and between the backends and the MPD frontend. All fields are optional and immutable. In other words, they can only be set through the class constructor during instance creation. Additionally fields are type checked.
If you want to modify a model, use the replace() method. It accepts keyword arguments for the parts of the model you want to change, and copies the rest of the data from the model you call it on. Example:
>>> from mopidy.models import Track
>>> track1 = Track(name='Christmas Carol', length=171)
>>> track1
Track(artists=[], length=171, name='Christmas Carol')
>>> track2 = track1.replace(length=37)
>>> track2
Track(artists=[], length=37, name='Christmas Carol')
>>> track1
Track(artists=[], length=171, name='Christmas Carol')
Model to represent URI references with a human friendly name and type attached. This is intended for use a lightweight object “free” of metadata that can be passed around instead of using full blown models.
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
A tracklist track. Wraps a regular track and it’s tracklist ID.
The use of TlTrack allows the same track to appear multiple times in the tracklist.
This class also accepts it’s parameters as positional arguments. Both arguments must be provided, and they must appear in the order they are listed here.
This class also supports iteration, so your extract its values like this:
(tlid, track) = tl_track
Parameters: |
|
---|
Superclass for immutable objects whose fields can only be modified via the constructor.
This version of this class has been retained to avoid breaking any clients relying on it’s behavior. Internally in Mopidy we now use ValidatedImmutableObject for type safety and it’s much smaller memory footprint.
Parameters: | kwargs (any) – kwargs to set as fields on the object |
---|
Replace the fields in the model and return a new instance
Examples:
# Returns a track with a new name
Track(name='foo').replace(name='bar')
# Return an album with a new number of tracks
Album(num_tracks=2).replace(num_tracks=5)
Parameters: | kwargs (any) – kwargs to set as fields on the object |
---|---|
Return type: | instance of the model with replaced fields |
Superclass for immutable objects whose fields can only be modified via the constructor. Fields should be Field instances to ensure type safety in our models.
Note that since these models can not be changed, we heavily memoize them to save memory. So constructing a class with the same arguments twice will give you the same instance twice.
Replace the fields in the model and return a new instance
Examples:
# Returns a track with a new name
Track(name='foo').replace(name='bar')
# Return an album with a new number of tracks
Album(num_tracks=2).replace(num_tracks=5)
Note that internally we memoize heavily to keep memory usage down given our overly repetitive data structures. So you might get an existing instance if it contains the same values.
Parameters: | kwargs (any) – kwargs to set as fields on the object |
---|---|
Return type: | instance of the model with replaced fields |
Automatically deserialize Mopidy models from JSON.
Usage:
>>> import json
>>> json.loads(
... '{"a_track": {"__model__": "Track", "name": "name"}}',
... object_hook=model_json_decoder)
{u'a_track': Track(artists=[], name=u'name')}
Automatically serialize Mopidy models to JSON.
Usage:
>>> import json
>>> json.dumps({'a_track': Track(name='name')}, cls=ModelJSONEncoder)
'{"a_track": {"__model__": "Track", "name": "name"}}'
Base field for use in ValidatedImmutableObject. These fields are responsible for type checking and other data sanitation in our models.
For simplicity fields use the Python descriptor protocol to store the values in the instance dictionary. Also note that fields are mutable if the object they are attached to allow it.
Default values will be validated with the exception of None.
Parameters: |
|
---|
Specialized Field which is wired up for bytes and unicode.
Parameters: | default – default value for field |
---|
Field for storing ASCII values such as GUIDs or other identifiers.
Values will be interned.
Parameters: | default – default value for field |
---|
Field for storing URIs
Values will be interned, currently not validated.
Parameters: | default – default value for field |
---|
Field for storing ISO 8601 dates as a string.
Supported formats are YYYY-MM-DD, YYYY-MM and YYYY, currently not validated.
Parameters: | default – default value for field |
---|