本文整理汇总了Python中sqlalchemy.types.Enum类的典型用法代码示例。如果您正苦于以下问题:Python Enum类的具体用法?Python Enum怎么用?Python Enum使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Enum类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DeclEnumType
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())
示例2: EnumType
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())
示例3: Enum
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)
示例4: EnumType
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: DeclEnumType
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())
示例6: DeclarativeEnumType
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())
示例7: DeclarativeEnumType
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())
示例8: DeclEnumType
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())
示例9: DeclEnumType
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())
示例10: EnumType
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)
示例11: downgrade
def downgrade():
enum_type = Enum(u'by', u'for', u'of', name='user_artwork_relationship_type')
enum_type.create(bind=op.get_bind())
op.add_column('user_artwork',
Column('relationship_type',
enum_type,
primary_key=True,
nullable=False,
server_default=op.inline_literal('by')))
op.execute('ALTER TABLE user_artwork DROP CONSTRAINT user_artwork_pkey')
op.execute('ALTER TABLE user_artwork ADD PRIMARY KEY (user_id, artwork_id, relationship_type)')
op.drop_index('ix_user_artwork_user_id')
op.drop_index('ix_user_artwork_artwork_id')
示例12: __init__
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)
示例13: __init__
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__)
)
示例14: __init__
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__)
)
示例15: __init__
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)