Add str coercion to path classes

This commit is contained in:
Konsta Vesterinen
2014-02-18 13:53:08 +02:00
parent 73632cf9cb
commit 41483c8d38
5 changed files with 17 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
from sqlalchemy.orm.attributes import InstrumentedAttribute
from .utils import str_coercible
@str_coercible
class Path(object):
def __init__(self, path, separator='.'):
if isinstance(path, Path):
@@ -34,6 +36,9 @@ class Path(object):
def __ne__(self, other):
return not (self == other)
def __unicode__(self):
return self.path
def get_attr(mixed, attr):
if isinstance(mixed, InstrumentedAttribute):
@@ -45,6 +50,7 @@ def get_attr(mixed, attr):
return getattr(mixed, attr)
@str_coercible
class AttrPath(object):
def __init__(self, class_, path):
self.class_ = class_
@@ -107,3 +113,6 @@ class AttrPath(object):
def __ne__(self, other):
return not (self == other)
def __unicode__(self):
return str(self.path)

View File

@@ -5,7 +5,7 @@ except ImportError:
# Python 2.6 port
from total_ordering import total_ordering
from sqlalchemy_utils import i18n
from .utils import str_coercible
from sqlalchemy_utils.utils import str_coercible
@str_coercible

View File

@@ -1,6 +1,6 @@
import six
from .utils import str_coercible
from sqlalchemy_utils.utils import str_coercible
from .weekday import WeekDay

View File

@@ -133,6 +133,12 @@ class TestPath(object):
assert path[0] == 's'
assert path[1] == 's2'
def test_str(self):
assert str(Path('s.s2')) == 's.s2'
def test_unicode(self):
assert unicode(Path('s.s2')) == u's.s2'
def test_getitem_with_slice(self):
path = Path('s.s2.s3')
assert path[1:] == Path('s2.s3')