use @six.python_2_unicode_compatible

This commit is contained in:
Johnny Robeson 2016-06-24 05:50:14 -04:00
parent 364d32953e
commit b6678019ea
3 changed files with 20 additions and 14 deletions

View file

@ -292,6 +292,7 @@ class LazyClassProperty(object):
@total_ordering
@six.python_2_unicode_compatible
class Distance(object):
"""Keeps track of multiple distance penalties. Provides a single
weighted distance for all penalties as well as a weighted distance
@ -378,7 +379,9 @@ class Distance(object):
def __rsub__(self, other):
return other - self.distance
def __unicode__(self):
# Behave like a string
def __str__(self):
return "{0:.2f}".format(self.distance)
# Behave like a dict.

View file

@ -264,7 +264,7 @@ PF_KEY_DEFAULT = 'default'
# Exceptions.
@six.python_2_unicode_compatible
class FileOperationError(Exception):
"""Indicates an error when interacting with a file on disk.
Possibilities include an unsupported media type, a permissions
@ -278,7 +278,7 @@ class FileOperationError(Exception):
self.path = path
self.reason = reason
def __unicode__(self):
def text(self):
"""Get a string representing the error. Describes both the
underlying reason and the file path in question.
"""
@ -287,26 +287,30 @@ class FileOperationError(Exception):
six.text_type(self.reason)
)
def __str__(self):
return six.text_type(self).encode('utf8')
# define __str__ as text to avoid infinite loop on super() calls
# with @six.python_2_unicode_compatible
__str__ = text
@six.python_2_unicode_compatible
class ReadError(FileOperationError):
"""An error while reading a file (i.e. in `Item.read`).
"""
def __unicode__(self):
return u'error reading ' + super(ReadError, self).__unicode__()
def __str__(self):
return u'error reading ' + super(ReadError, self).text()
@six.python_2_unicode_compatible
class WriteError(FileOperationError):
"""An error while writing a file (i.e. in `Item.write`).
"""
def __unicode__(self):
return u'error writing ' + super(WriteError, self).__unicode__()
def __str__(self):
return u'error writing ' + super(WriteError, self).text()
# Item and Album model classes.
@six.python_2_unicode_compatible
class LibModel(dbcore.Model):
"""Shared concrete functionality for Items and Albums.
"""
@ -343,9 +347,6 @@ class LibModel(dbcore.Model):
return result
def __str__(self):
return format(self).encode('utf8')
def __unicode__(self):
return format(self)

View file

@ -197,8 +197,9 @@ class BeatportClient(object):
return response.json()['results']
@six.python_2_unicode_compatible
class BeatportRelease(BeatportObject):
def __unicode__(self):
def __str__(self):
if len(self.artists) < 4:
artist_str = ", ".join(x[1] for x in self.artists)
else:
@ -225,8 +226,9 @@ class BeatportRelease(BeatportObject):
data['slug'], data['id'])
@six.python_2_unicode_compatible
class BeatportTrack(BeatportObject):
def __unicode__(self):
def __str__(self):
artist_str = ", ".join(x[1] for x in self.artists)
return (u"<BeatportTrack: {0} - {1} ({2})>"
.format(artist_str, self.name, self.mix_name))