Python 3.4 Enum type

227 views
Skip to first unread message

Ian Kelly

unread,
Aug 6, 2013, 6:12:37 PM8/6/13
to sqlal...@googlegroups.com
Since Python 3.4 is adding support for enums to the standard library,
I wrote a TypeDecorator for it:

import sqlalchemy.types as types


class PythonEnum(types.TypeDecorator):

impl = types.Enum

def __init__(self, enum_class, **kw):
super().__init__(*(m.name for m in enum_class), **kw)
self._enum_class = enum_class

def process_bind_param(self, value, dialect):
return value.name

def process_result_value(self, value, dialect):
return self._enum_class[value]

@property
def python_type(self):
return self._enum_class


Comments welcome. Is this something that should be added to sqlalchemy?

Michael Bayer

unread,
Aug 6, 2013, 7:06:46 PM8/6/13
to sqlal...@googlegroups.com
there's a lot of flavors of python enum and I recall the standard library one rubs me the wrong way a bit. I'd prefer for sqlalchemy.Enum to have some easy way to accommodate any Python-side enum using some kind of convention, obviously one that's compat with the std lib enum.
> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

signature.asc
Reply all
Reply to author
Forward
0 new messages