本文整理汇总了Python中sqlalchemy.types.Enum._set_table方法的典型用法代码示例。如果您正苦于以下问题:Python Enum._set_table方法的具体用法?Python Enum._set_table怎么用?Python Enum._set_table使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.types.Enum
的用法示例。
在下文中一共展示了Enum._set_table方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DeclarativeEnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class DeclarativeEnumType(SchemaType, TypeDecorator):
"""Column type usable in table column definitions."""
def __init__(self, enum, name=None):
self.enum = enum
enum_args = enum.__enum_args__.copy()
if name is not None:
enum_args['name'] = name
elif 'name' not in enum_args:
enum_args['name'] = 'ck_' + camelcase_to_underscore(enum.__name__)
self.impl = Enum(*enum.values(), **enum_args)
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
return DeclarativeEnumType(self.enum)
def process_bind_param(self, value, dialect):
if value is None: # pragma: no cover
return None
return value.value
def process_result_value(self, value, dialect):
if value is None: # pragma: no cover
return None
return self.enum.from_string(value.strip())
示例2: DeclEnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class DeclEnumType(SchemaType, TypeDecorator):
def __init__(self, enum):
self.enum = enum
self.impl = Enum(
*enum.values(),
name="ck%s" % re.sub(
'([A-Z])',
lambda m:"_" + m.group(1).lower(),
enum.__name__)
)
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
return DeclEnumType(self.enum)
def process_bind_param(self, value, dialect):
if value is None:
return None
return value.value
def process_result_value(self, value, dialect):
if value is None:
return None
return self.enum.from_string(value.strip())
示例3: EnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class EnumType(SchemaType, TypeDecorator):
def __init__(self, enum, name):
self.enum = enum
self.name = name
members = (member.value for member in enum)
kwargs = {'name': name}
self.impl = SAEnum(*members, **kwargs)
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
return EnumType(self.enum, self.name)
def process_bind_param(self, enum_instance, dialect):
if enum_instance is None:
return None
return enum_instance.value
def process_result_value(self, value, dialect):
if value is None:
return None
return self.enum(value)
示例4: EnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class EnumType(SchemaType, TypeDecorator):
# pylint: disable=W0223
def __init__(self, enum, *args, **kwargs):
self.enum = enum
self.impl = SQLAEnum(
*enum.values(),
name="ck%s" % re.sub(
"([A-Z])",
lambda m: "_" + m.group(1).lower(),
enum.__name__))
super(EnumType, self).__init__(*args, **kwargs)
def _set_table(self, table, column):
# pylint: disable=W0212
self.impl._set_table(table, column)
def copy(self):
return EnumType(self.enum)
def process_bind_param(self, value, dialect):
if value is None:
return None
return value.value
def process_result_value(self, value, dialect):
if value is None:
return None
return self.enum.from_string(value.strip())
示例5: EnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class EnumType(SchemaType, TypeDecorator):
def __init__(self, enum):
self.enum = enum
self.impl = SAEnum(
*[sym.value for sym in enum],
name="enum%s" % re.sub(
'([A-Z])',
lambda m: "_" + m.group(1).lower(),
enum.__name__)
)
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
return EnumType(self.enum)
def process_bind_param(self, value, dialect):
if value is None:
return None
if isinstance(value, str):
return value
return value.value
def process_result_value(self, value, dialect):
if value is None:
return None
return self.enum(value.strip())
示例6: Enum
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class Enum(SchemaType, TypeDecorator):
def __init__(self, enum):
name = re.sub('([A-Z])', lambda m:"_" + m.group(1).lower(),
enum.__name__)
self.enum = enum
self.impl = SaEnum(*enum.values(), name="ck%s" % name)
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
return Enum(self.enum)
def process_bind_param(self, value, dialect):
if value is None:
return None
return value.value
def process_result_value(self, value, dialect):
if value is None:
return None
return self.enum.from_string(value.strip())
def create(self, bind=None, checkfirst=False):
super(Enum, self).create(bind, checkfirst)
t = self.dialect_impl(bind.dialect)
if t.impl.__class__ is not self.__class__ and isinstance(t, SchemaType):
t.impl.create(bind=bind, checkfirst=checkfirst)
示例7: DeclEnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class DeclEnumType(SchemaType, TypeDecorator):
def __init__(self, enum, **kwargs):
super(DeclEnumType, self).__init__(**kwargs)
self.enum = enum
self.impl = Enum(
*enum.values(),
name="ck%s" % re.sub(
'([A-Z])',
lambda m:"_" + m.group(1).lower(),
enum.__name__)
)
def _set_table(self, column, table):
self.impl.name = "ck_%s_%s_%s" % (
'_'.join(table.schema.split('.')), table.name, self.impl.name[3:])
self.impl._set_table(column, table)
def copy(self, **kw):
return DeclEnumType(self.enum, **kw)
def process_bind_param(self, value, dialect):
if value is None:
return None
if isinstance(value, EnumSymbol):
return value.value
elif isinstance(value, (str, unicode)):
# Should not happen, but mask the error for now.
return value
def process_result_value(self, value, dialect):
if value is None:
return None
return self.enum.from_string(value.strip())
示例8: DeclEnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class DeclEnumType(SchemaType, TypeDecorator):
def __init__(self, enum):
self.enum = enum
# convert CamelCase to underscore_separated
name = re.sub('.([A-Z])', lambda m: '_' + m.group(1).lower(), enum.__name__).lower()
self.impl = Enum(*enum.values(), name=name)
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
return DeclEnumType(self.enum)
def process_bind_param(self, value, dialect):
if value is None:
return None
if isinstance(value, basestring):
return value
if value.cls_ != self.enum:
raise TypeError('Cannot use %r as bind parameter for column '
'of type %r' % (value, self.enum))
return value.value
def process_result_value(self, value, dialect):
if value is None:
return None
return self.enum.from_string(value.strip())
示例9: DeclarativeEnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class DeclarativeEnumType(SchemaType, TypeDecorator):
"""Column type usable in table column definitions."""
def __init__(self, enum):
self.enum = enum
constraint = 'ck{0}'.format(re.sub('([A-Z])',
lambda m: '_' + m.group(1).lower(),
enum.__name__))
self.impl = Enum(*enum.values(), name=constraint)
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
return DeclarativeEnumType(self.enum)
def process_bind_param(self, value, dialect):
if value is None: # pragma: no cover
return None
return value.value
def process_result_value(self, value, dialect):
if value is None: # pragma: no cover
return None
return self.enum.from_string(value.strip())
示例10: DeclEnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class DeclEnumType(SchemaType, TypeDecorator):
"""
DeclEnumType supports object instantiation in two different ways:
Passing in an enum:
This is to be used in the application code. It will pull enum values straight from
the DeclEnum object. A helper for this is available in DeclEnum.db_type()
Passing in a tuple with enum values:
In migrations the enum value list needs to be fix. It should not be pulled in from
the application code, otherwise later modifications of enum values could result in
those values being added in an earlier migration when re-running migrations from the
beginning. Therefore DeclEnum(enum_values=('one', 'two'), enum_name='MyEnum') should
be used.
"""
def __init__(self, enum=None, enum_values=None, enum_name=None):
self.enum = enum
self.enum_values = enum_values
self.enum_name = enum_name
if enum:
self.enum_values=enum.values()
self.enum_name=enum.__name__
self.impl = Enum(
*self.enum_values,
name="ck%s" % re.sub(
'([A-Z])',
lambda m: "_" + m.group(1).lower(),
self.enum_name)
)
def create(self, bind=None, checkfirst=False):
"""Issue CREATE ddl for this type, if applicable."""
super(DeclEnumType, self).create(bind, checkfirst)
t = self.dialect_impl(bind.dialect)
if t.impl.__class__ is not self.__class__ and isinstance(t, SchemaType):
t.impl.create(bind=bind, checkfirst=checkfirst)
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
if self.enum:
return DeclEnumType(self.enum)
else:
return DeclEnumType(enum_name=self.enum_name, enum_values=self.enum_values)
def process_bind_param(self, value, dialect):
if value is None:
return None
return value.value
def process_result_value(self, value, dialect):
if value is None:
return None
return self.enum.from_string(value.strip())
示例11: ASEnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class ASEnumType(SchemaType, TypeDecorator):
def __init__(self, enum, regex = None):
self.enum = enum
self.impl = Enum(
*enum.values(),
name="ck%s" % re.sub(
'([A-Z])',
lambda m:"_" + m.group(1).lower(),
enum.__name__)
)
self.regex = regex
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
return ASEnumType(self.enum)
def process_bind_param(self, value, dialect):
if value is None:
return None
if self.__checkValue(value):
return value.value
return None
def process_result_value(self, value, dialect):
if value is None:
return None
if self.__checkValue(self.enum.from_string(value.strip())):
return self.enum.from_string(value.strip())
return None
def __checkValue(self, value):
if self.regex is not None:
if (re.match(self.regex, value)):
return value
if value in self.enum:
return True
return False;
示例12: DeclEnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class DeclEnumType(SchemaType, TypeDecorator):
def __init__(self, enum):
self.enum = enum
self.impl = Enum(
*enum.values(),
name="ck%s" % re.sub(
'([A-Z])',
lambda m: "_" + m.group(1).lower(),
enum.__name__)
)
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self):
return DeclEnumType(self.enum)
def process_bind_param(self, value, dialect):
if value is None:
return None
return value.value
def process_result_value(self, value, dialect):
if value is None:
return None
return self.enum.from_string(value.strip())
def create(self, bind=None, checkfirst=False):
"""Issue CREATE ddl for this type, if applicable."""
super(DeclEnumType, self).create(bind, checkfirst)
t = self.dialect_impl(bind.dialect)
if t.impl.__class__ is not self.__class__ and isinstance(t, SchemaType):
t.impl.create(bind=bind, checkfirst=checkfirst)
def drop(self, bind=None, checkfirst=False):
"""Issue DROP ddl for this type, if applicable."""
super(DeclEnumType, self).drop(bind, checkfirst)
t = self.dialect_impl(bind.dialect)
if t.impl.__class__ is not self.__class__ and isinstance(t, SchemaType):
t.impl.drop(bind=bind, checkfirst=checkfirst)
示例13: DeclEnumType
# 需要导入模块: from sqlalchemy.types import Enum [as 别名]
# 或者: from sqlalchemy.types.Enum import _set_table [as 别名]
class DeclEnumType(SchemaType, TypeDecorator):
"""DeclEnum augmented so that it can persist to the database."""
def __init__(self, enum):
import re
self.enum = enum
self.impl = Enum(*enum.names(), name="ck%s" % re.sub(
'([A-Z])', lambda m: '_' + m.group(1).lower(), enum.__name__))
def _set_table(self, table, column):
self.impl._set_table(table, column)
def copy(self, **kw):
return DeclEnumType(self.enum)
def process_bind_param(self, value, dialect):
if isinstance(value, EnumSymbol):
value = value.name
return value
def process_result_value(self, value, dialect):
if value is not None:
return getattr(self.enum, value.strip())