Python sqlalchemy.types.UserDefinedType() Examples

The following are 30 code examples of sqlalchemy.types.UserDefinedType(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.types , or try the search function .
Example #1
Source File: type_api.py    From android_universal with MIT License 6 votes vote down vote up
def coerce_compared_value(self, op, value):
        """Suggest a type for a 'coerced' Python value in an expression.

        Default behavior for :class:`.UserDefinedType` is the
        same as that of :class:`.TypeDecorator`; by default it returns
        ``self``, assuming the compared value should be coerced into
        the same type as this one.  See
        :meth:`.TypeDecorator.coerce_compared_value` for more detail.

        .. versionchanged:: 0.8 :meth:`.UserDefinedType.coerce_compared_value`
           now returns ``self`` by default, rather than falling onto the
           more fundamental behavior of
           :meth:`.TypeEngine.coerce_compared_value`.

        """

        return self 
Example #2
Source File: type_api.py    From jbox with MIT License 6 votes vote down vote up
def coerce_compared_value(self, op, value):
        """Suggest a type for a 'coerced' Python value in an expression.

        Default behavior for :class:`.UserDefinedType` is the
        same as that of :class:`.TypeDecorator`; by default it returns
        ``self``, assuming the compared value should be coerced into
        the same type as this one.  See
        :meth:`.TypeDecorator.coerce_compared_value` for more detail.

        .. versionchanged:: 0.8 :meth:`.UserDefinedType.coerce_compared_value`
           now returns ``self`` by default, rather than falling onto the
           more fundamental behavior of
           :meth:`.TypeEngine.coerce_compared_value`.

        """

        return self 
Example #3
Source File: type_api.py    From moviegrabber with GNU General Public License v3.0 6 votes vote down vote up
def coerce_compared_value(self, op, value):
        """Suggest a type for a 'coerced' Python value in an expression.

        Default behavior for :class:`.UserDefinedType` is the
        same as that of :class:`.TypeDecorator`; by default it returns
        ``self``, assuming the compared value should be coerced into
        the same type as this one.  See
        :meth:`.TypeDecorator.coerce_compared_value` for more detail.

        .. versionchanged:: 0.8 :meth:`.UserDefinedType.coerce_compared_value`
           now returns ``self`` by default, rather than falling onto the
           more fundamental behavior of
           :meth:`.TypeEngine.coerce_compared_value`.

        """

        return self 
Example #4
Source File: type_api.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def coerce_compared_value(self, op, value):
        """Suggest a type for a 'coerced' Python value in an expression.

        Default behavior for :class:`.UserDefinedType` is the
        same as that of :class:`.TypeDecorator`; by default it returns
        ``self``, assuming the compared value should be coerced into
        the same type as this one.  See
        :meth:`.TypeDecorator.coerce_compared_value` for more detail.

        .. versionchanged:: 0.8 :meth:`.UserDefinedType.coerce_compared_value`
           now returns ``self`` by default, rather than falling onto the
           more fundamental behavior of
           :meth:`.TypeEngine.coerce_compared_value`.

        """

        return self 
Example #5
Source File: type_api.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def coerce_compared_value(self, op, value):
        """Suggest a type for a 'coerced' Python value in an expression.

        Default behavior for :class:`.UserDefinedType` is the
        same as that of :class:`.TypeDecorator`; by default it returns
        ``self``, assuming the compared value should be coerced into
        the same type as this one.  See
        :meth:`.TypeDecorator.coerce_compared_value` for more detail.

        .. versionchanged:: 0.8 :meth:`.UserDefinedType.coerce_compared_value`
           now returns ``self`` by default, rather than falling onto the
           more fundamental behavior of
           :meth:`.TypeEngine.coerce_compared_value`.

        """

        return self 
Example #6
Source File: type_api.py    From jarvis with GNU General Public License v2.0 6 votes vote down vote up
def coerce_compared_value(self, op, value):
        """Suggest a type for a 'coerced' Python value in an expression.

        Default behavior for :class:`.UserDefinedType` is the
        same as that of :class:`.TypeDecorator`; by default it returns
        ``self``, assuming the compared value should be coerced into
        the same type as this one.  See
        :meth:`.TypeDecorator.coerce_compared_value` for more detail.

        .. versionchanged:: 0.8 :meth:`.UserDefinedType.coerce_compared_value`
           now returns ``self`` by default, rather than falling onto the
           more fundamental behavior of
           :meth:`.TypeEngine.coerce_compared_value`.

        """

        return self 
Example #7
Source File: type_api.py    From planespotter with MIT License 6 votes vote down vote up
def coerce_compared_value(self, op, value):
        """Suggest a type for a 'coerced' Python value in an expression.

        Default behavior for :class:`.UserDefinedType` is the
        same as that of :class:`.TypeDecorator`; by default it returns
        ``self``, assuming the compared value should be coerced into
        the same type as this one.  See
        :meth:`.TypeDecorator.coerce_compared_value` for more detail.

        .. versionchanged:: 0.8 :meth:`.UserDefinedType.coerce_compared_value`
           now returns ``self`` by default, rather than falling onto the
           more fundamental behavior of
           :meth:`.TypeEngine.coerce_compared_value`.

        """

        return self 
Example #8
Source File: type_api.py    From pyRevit with GNU General Public License v3.0 6 votes vote down vote up
def coerce_compared_value(self, op, value):
        """Suggest a type for a 'coerced' Python value in an expression.

        Default behavior for :class:`.UserDefinedType` is the
        same as that of :class:`.TypeDecorator`; by default it returns
        ``self``, assuming the compared value should be coerced into
        the same type as this one.  See
        :meth:`.TypeDecorator.coerce_compared_value` for more detail.

        .. versionchanged:: 0.8 :meth:`.UserDefinedType.coerce_compared_value`
           now returns ``self`` by default, rather than falling onto the
           more fundamental behavior of
           :meth:`.TypeEngine.coerce_compared_value`.

        """

        return self 
Example #9
Source File: test_types.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_ret_type_custom(self):
        class MyType(types.UserDefinedType):
            pass

        col = column("x", HSTORE(text_type=MyType))

        is_(col["foo"].type.__class__, MyType) 
Example #10
Source File: type_api.py    From sqlalchemy with MIT License 5 votes vote down vote up
def _type_affinity(self):
        """Return a rudimental 'affinity' value expressing the general class
        of type."""

        typ = None
        for t in self.__class__.__mro__:
            if t in (TypeEngine, UserDefinedType):
                return typ
            elif issubclass(t, (TypeEngine, UserDefinedType)):
                typ = t
        else:
            return self.__class__ 
Example #11
Source File: test_types.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_custom_astext_type(self):
        class MyType(types.UserDefinedType):
            pass

        col = column("x", JSON(astext_type=MyType))

        is_(col["q"].astext.type.__class__, MyType)

        is_(col[("q", "p")].astext.type.__class__, MyType)

        is_(col["q"]["p"].astext.type.__class__, MyType) 
Example #12
Source File: test_types.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_user_defined_dialect_specific_args(self):
        class MyType(types.UserDefinedType):
            def __init__(self, foo="foo", **kwargs):
                super(MyType, self).__init__()
                self.foo = foo
                self.dialect_specific_args = kwargs

            def adapt(self, cls):
                return cls(foo=self.foo, **self.dialect_specific_args)

        t = MyType(bar="bar")
        a = t.dialect_impl(testing.db.dialect)
        eq_(a.foo, "foo")
        eq_(a.dialect_specific_args["bar"], "bar") 
Example #13
Source File: test_types.py    From sqlalchemy with MIT License 5 votes vote down vote up
def setup(self):
        class UTypeOne(types.UserDefinedType):
            def get_col_spec(self):
                return "UTYPEONE"

            def bind_processor(self, dialect):
                def process(value):
                    return value + "UONE"

                return process

        class UTypeTwo(types.UserDefinedType):
            def get_col_spec(self):
                return "UTYPETWO"

            def bind_processor(self, dialect):
                def process(value):
                    return value + "UTWO"

                return process

        class UTypeThree(types.UserDefinedType):
            def get_col_spec(self):
                return "UTYPETHREE"

        self.UTypeOne = UTypeOne
        self.UTypeTwo = UTypeTwo
        self.UTypeThree = UTypeThree
        self.variant = self.UTypeOne().with_variant(
            self.UTypeTwo(), "postgresql"
        )
        self.composite = self.variant.with_variant(self.UTypeThree(), "mysql") 
Example #14
Source File: test_types.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_bind_typing(self):
        from sqlalchemy.sql import column

        class MyFoobarType(types.UserDefinedType):
            pass

        class Foo(object):
            pass

        # unknown type + integer, right hand bind
        # coerces to given type
        expr = column("foo", MyFoobarType) + 5
        assert expr.right.type._type_affinity is MyFoobarType

        # untyped bind - it gets assigned MyFoobarType
        bp = bindparam("foo")
        expr = column("foo", MyFoobarType) + bp
        assert bp.type._type_affinity is types.NullType  # noqa
        assert expr.right.type._type_affinity is MyFoobarType

        expr = column("foo", MyFoobarType) + bindparam("foo", type_=Integer)
        assert expr.right.type._type_affinity is types.Integer

        # unknown type + unknown, right hand bind
        # coerces to the left
        expr = column("foo", MyFoobarType) + Foo()
        assert expr.right.type._type_affinity is MyFoobarType

        # including for non-commutative ops
        expr = column("foo", MyFoobarType) - Foo()
        assert expr.right.type._type_affinity is MyFoobarType

        expr = column("foo", MyFoobarType) - datetime.date(2010, 8, 25)
        assert expr.right.type._type_affinity is MyFoobarType 
Example #15
Source File: test_types.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_user_defined(self):
        """test that dialects pass the column through on DDL."""

        class MyType(types.UserDefinedType):
            def get_col_spec(self, **kw):
                return "FOOB %s" % kw["type_expression"].name

        m = MetaData()
        t = Table("t", m, Column("bar", MyType, nullable=False))
        self.assert_compile(ddl.CreateColumn(t.c.bar), "bar FOOB bar NOT NULL") 
Example #16
Source File: type_api.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def _type_affinity(self):
        """Return a rudimental 'affinity' value expressing the general class
        of type."""

        typ = None
        for t in self.__class__.__mro__:
            if t in (TypeEngine, UserDefinedType):
                return typ
            elif issubclass(t, (TypeEngine, UserDefinedType)):
                typ = t
        else:
            return self.__class__ 
Example #17
Source File: type_api.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def _adapt_expression(self, op, other_comparator):
            if hasattr(self.type, 'adapt_operator'):
                util.warn_deprecated(
                    "UserDefinedType.adapt_operator is deprecated.  Create "
                    "a UserDefinedType.Comparator subclass instead which "
                    "generates the desired expression constructs, given a "
                    "particular operator."
                )
                return self.type.adapt_operator(op), self.type
            else:
                return super(
                    UserDefinedType.Comparator, self
                )._adapt_expression(op, other_comparator) 
Example #18
Source File: type_api.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
def _type_affinity(self):
        """Return a rudimental 'affinity' value expressing the general class
        of type."""

        typ = None
        for t in self.__class__.__mro__:
            if t in (TypeEngine, UserDefinedType):
                return typ
            elif issubclass(t, (TypeEngine, UserDefinedType)):
                typ = t
        else:
            return self.__class__ 
Example #19
Source File: type_api.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
def _adapt_expression(self, op, other_comparator):
            if hasattr(self.type, 'adapt_operator'):
                util.warn_deprecated(
                    "UserDefinedType.adapt_operator is deprecated.  Create "
                     "a UserDefinedType.Comparator subclass instead which "
                     "generates the desired expression constructs, given a "
                     "particular operator."
                    )
                return self.type.adapt_operator(op), self.type
            else:
                return op, self.type 
Example #20
Source File: type_api.py    From android_universal with MIT License 5 votes vote down vote up
def _type_affinity(self):
        """Return a rudimental 'affinity' value expressing the general class
        of type."""

        typ = None
        for t in self.__class__.__mro__:
            if t in (TypeEngine, UserDefinedType):
                return typ
            elif issubclass(t, (TypeEngine, UserDefinedType)):
                typ = t
        else:
            return self.__class__ 
Example #21
Source File: type_api.py    From android_universal with MIT License 5 votes vote down vote up
def _adapt_expression(self, op, other_comparator):
            if hasattr(self.type, 'adapt_operator'):
                util.warn_deprecated(
                    "UserDefinedType.adapt_operator is deprecated.  Create "
                    "a UserDefinedType.Comparator subclass instead which "
                    "generates the desired expression constructs, given a "
                    "particular operator."
                )
                return self.type.adapt_operator(op), self.type
            else:
                return super(
                    UserDefinedType.Comparator, self
                )._adapt_expression(op, other_comparator) 
Example #22
Source File: test_types.py    From sqlalchemy with MIT License 5 votes vote down vote up
def special_types_table(self, metadata):

        # create these types so that we can issue
        # special SQL92 INTERVAL syntax
        class y2m(types.UserDefinedType, postgresql.INTERVAL):
            def get_col_spec(self):
                return "INTERVAL YEAR TO MONTH"

        class d2s(types.UserDefinedType, postgresql.INTERVAL):
            def get_col_spec(self):
                return "INTERVAL DAY TO SECOND"

        table = Table(
            "sometable",
            metadata,
            Column("id", postgresql.UUID, primary_key=True),
            Column("flag", postgresql.BIT),
            Column("bitstring", postgresql.BIT(4)),
            Column("addr", postgresql.INET),
            Column("addr2", postgresql.MACADDR),
            Column("price", postgresql.MONEY),
            Column("addr3", postgresql.CIDR),
            Column("doubleprec", postgresql.DOUBLE_PRECISION),
            Column("plain_interval", postgresql.INTERVAL),
            Column("year_interval", y2m()),
            Column("month_interval", d2s()),
            Column("precision_interval", postgresql.INTERVAL(precision=3)),
            Column("tsvector_document", postgresql.TSVECTOR),
        )

        return table 
Example #23
Source File: type_api.py    From sqlalchemy with MIT License 5 votes vote down vote up
def coerce_compared_value(self, op, value):
        """Suggest a type for a 'coerced' Python value in an expression.

        Default behavior for :class:`.UserDefinedType` is the
        same as that of :class:`.TypeDecorator`; by default it returns
        ``self``, assuming the compared value should be coerced into
        the same type as this one.  See
        :meth:`.TypeDecorator.coerce_compared_value` for more detail.

        """

        return self 
Example #24
Source File: type_api.py    From jbox with MIT License 5 votes vote down vote up
def _type_affinity(self):
        """Return a rudimental 'affinity' value expressing the general class
        of type."""

        typ = None
        for t in self.__class__.__mro__:
            if t in (TypeEngine, UserDefinedType):
                return typ
            elif issubclass(t, (TypeEngine, UserDefinedType)):
                typ = t
        else:
            return self.__class__ 
Example #25
Source File: type_api.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def _adapt_expression(self, op, other_comparator):
            if hasattr(self.type, 'adapt_operator'):
                util.warn_deprecated(
                    "UserDefinedType.adapt_operator is deprecated.  Create "
                    "a UserDefinedType.Comparator subclass instead which "
                    "generates the desired expression constructs, given a "
                    "particular operator."
                )
                return self.type.adapt_operator(op), self.type
            else:
                return op, self.type 
Example #26
Source File: type_api.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def _type_affinity(self):
        """Return a rudimental 'affinity' value expressing the general class
        of type."""

        typ = None
        for t in self.__class__.__mro__:
            if t in (TypeEngine, UserDefinedType):
                return typ
            elif issubclass(t, (TypeEngine, UserDefinedType)):
                typ = t
        else:
            return self.__class__ 
Example #27
Source File: type_api.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def _adapt_expression(self, op, other_comparator):
            if hasattr(self.type, 'adapt_operator'):
                util.warn_deprecated(
                    "UserDefinedType.adapt_operator is deprecated.  Create "
                    "a UserDefinedType.Comparator subclass instead which "
                    "generates the desired expression constructs, given a "
                    "particular operator."
                )
                return self.type.adapt_operator(op), self.type
            else:
                return op, self.type 
Example #28
Source File: type_api.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def _type_affinity(self):
        """Return a rudimental 'affinity' value expressing the general class
        of type."""

        typ = None
        for t in self.__class__.__mro__:
            if t in (TypeEngine, UserDefinedType):
                return typ
            elif issubclass(t, (TypeEngine, UserDefinedType)):
                typ = t
        else:
            return self.__class__ 
Example #29
Source File: type_api.py    From planespotter with MIT License 5 votes vote down vote up
def _adapt_expression(self, op, other_comparator):
            if hasattr(self.type, 'adapt_operator'):
                util.warn_deprecated(
                    "UserDefinedType.adapt_operator is deprecated.  Create "
                    "a UserDefinedType.Comparator subclass instead which "
                    "generates the desired expression constructs, given a "
                    "particular operator."
                )
                return self.type.adapt_operator(op), self.type
            else:
                return super(
                    UserDefinedType.Comparator, self
                )._adapt_expression(op, other_comparator) 
Example #30
Source File: type_api.py    From planespotter with MIT License 5 votes vote down vote up
def _type_affinity(self):
        """Return a rudimental 'affinity' value expressing the general class
        of type."""

        typ = None
        for t in self.__class__.__mro__:
            if t in (TypeEngine, UserDefinedType):
                return typ
            elif issubclass(t, (TypeEngine, UserDefinedType)):
                typ = t
        else:
            return self.__class__