本文整理汇总了Python中sqlalchemy.types.UserDefinedType方法的典型用法代码示例。如果您正苦于以下问题:Python types.UserDefinedType方法的具体用法?Python types.UserDefinedType怎么用?Python types.UserDefinedType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.types
的用法示例。
在下文中一共展示了types.UserDefinedType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: coerce_compared_value
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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
示例2: _type_affinity
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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__
示例3: _adapt_expression
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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
示例4: test_repr_custom_type_w_sqla_prefix
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
def test_repr_custom_type_w_sqla_prefix(self):
self.autogen_context.opts["user_module_prefix"] = None
class MyType(UserDefinedType):
pass
MyType.__module__ = "sqlalchemy_util.types"
type_ = MyType()
eq_ignore_whitespace(
autogenerate.render._repr_type(type_, self.autogen_context),
"sqlalchemy_util.types.MyType()",
)
示例5: test_repr_user_type_user_prefix_None
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
def test_repr_user_type_user_prefix_None(self):
class MyType(UserDefinedType):
def get_col_spec(self):
return "MYTYPE"
type_ = MyType()
self.autogen_context.opts["user_module_prefix"] = None
eq_ignore_whitespace(
autogenerate.render._repr_type(type_, self.autogen_context),
"tests.test_autogen_render.MyType()",
)
示例6: test_repr_user_type_user_prefix_present
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
def test_repr_user_type_user_prefix_present(self):
from sqlalchemy.types import UserDefinedType
class MyType(UserDefinedType):
def get_col_spec(self):
return "MYTYPE"
type_ = MyType()
self.autogen_context.opts["user_module_prefix"] = "user."
eq_ignore_whitespace(
autogenerate.render._repr_type(type_, self.autogen_context),
"user.MyType()",
)
示例7: _adapt_expression
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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)
示例8: coerce_compared_value
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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
示例9: special_types_table
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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
示例10: test_ret_type_custom
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
def test_ret_type_custom(self):
class MyType(types.UserDefinedType):
pass
col = column("x", HSTORE(text_type=MyType))
is_(col["foo"].type.__class__, MyType)
示例11: test_custom_astext_type
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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)
示例12: test_user_defined_dialect_specific_args
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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")
示例13: setup
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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")
示例14: test_bind_typing
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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
示例15: test_user_defined
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import UserDefinedType [as 别名]
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")