Пример #1
0
def test_mro():
    class Foo(object):
        def __init__(self, data):
            self.data = data

    class Bar(Foo):
        pass

    @adapter(Foo, str)
    def to_string(obj, to_cls):
        return 'Adapted to string'

    bar = Bar('My string')

    s = adapt.adapt(bar, str)

    assert s == 'Adapted to string'
    assert s == 'Adapted to string'

    class Zip(object):
        def __init__(self, data):
            self.data = data

    class Zam(Zip):
        pass

    @adapter(Foo, Zip)
    def from_foo_to_zip(obj, to_cls):
        return 'Adapted to zip'

    s = adapt.adapt(bar, Zam)

    assert s == 'Adapted to zip'
Пример #2
0
def test_mro():
    class Foo(object):
        def __init__(self, data):
            self.data = data

    class Bar(Foo):
        pass

    @adapter(Foo, str)
    def to_string(obj, to_cls):
        return 'Adapted to string'

    bar = Bar('My string')

    s = adapt.adapt(bar, str)

    assert s == 'Adapted to string'
    assert s == 'Adapted to string'

    class Zip(object):
        def __init__(self, data):
            self.data = data

    class Zam(Zip):
        pass

    @adapter(Foo, Zip)
    def from_foo_to_zip(obj, to_cls):
        return 'Adapted to zip'

    s = adapt.adapt(bar, Zam)

    assert s == 'Adapted to zip'
Пример #3
0
    def adapt(self, value):
        """
        Convert the `value` to the `self.type` for this :class:`Field`
        """
        if value is None or value is Empty:
            return value

        if isinstance(value, self.type):
            return value
        elif hasattr(value, '__adapt__'):
            # Use an object's own adapter to adapt.
            try:
                return value.__adapt__(self.type)
            except TypeError:
                pass

        if hasattr(self.type, '__adapt__'):
            # Try using the type's adapter
            return self.type.__adapt__(value)

        if self.__class__.__adapters__:
            # Use a registered adapter
            adapter = self.__class__.__adapters__.get(type(value), None)
            if adapter:
                return adapter(value)

        try:
            # Use generic adapters
            return adapt(value, self.type)
        except AdaptError:
            pass

        raise TypeError('Could not adapt %r to %r' % (value, self.type))
Пример #4
0
    def adapt(self, value):
        """
        Convert the `value` to the `self.type` for this :class:`Field`
        """
        if value is None or value is Empty:
            return value

        if isinstance(value, self.type):
            return value
        elif hasattr(value, '__adapt__'):
            # Use an object's own adapter to adapt.
            try:
                return value.__adapt__(self.type)
            except TypeError as e:
                pass

        if hasattr(self.type, '__adapt__'):
            # Try using the type's adapter
            return self.type.__adapt__(value)

        if self.__class__.__adapters__:
            # Use a registered adapter
            adapter = self.__class__.__adapters__.get(type(value), None)
            if adapter:
                return adapter(value)

        try:
            # Use generic adapters
            return adapt(value, self.type)
        except AdaptError:
            pass

        raise TypeError('Could not adapt %r to %r' % (value, self.type))
Пример #5
0
 def adapt_all(cls, obj):
     return (adapt(i, cls) for i in obj)
Пример #6
0
 def adapt(cls, obj):
     return adapt(obj, cls)
Пример #7
0
 def adapt_all(cls, obj):
     return (adapt(i, cls) for i in obj)
Пример #8
0
 def adapt(cls, obj):
     return adapt(obj, cls)