Skip to main content

SQLAlchemy type to store standard enum.Enum value

Project description

https://badge.fury.io/py/SQLAlchemy-Enum34.svg? https://travis-ci.org/spoqa/sqlalchemy-enum34.svg?branch=master

This package provides a SQLAlchemy type to store values of standard enum.Enum (which became a part of standard library since Python 3.4). Its internal representation is equivalent to SQLAlchemy’s built-in sqlalchemy.types.Enum, but its Python representation is not a str but enum.Enum.

Note that this works on Python 2.6 as well as 3.4, the latest version of Python, through enum34 package.

The following example shows how enum-typed columns can be declared:

import enum

from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_enum34 import EnumType

Base = declarative_base()

class Color(enum.Enum):
    black = 'black'
    white = 'white'
    navy = 'navy'
    red = 'red'

class Size(enum.Enum):
    small = 'S'
    medium = 'M'
    large = 'L'
    xlarge = 'XL'

class Shirt(Base):
    id = Column(Integer, primary_key=True)
    color = Column(EnumType(Color), nullable=False)
    size = Column(EnumType(Size, name='shirt_size'), nullable=False)

And the following REPL session shows how these columns work:

>>> shirt = session.query(Shirt).filter(Shift.color == Color.navy).first()
>>> shirt.color
<Color.navy: 'navy'>
>>> shirt.size
<Size.large: 'large'>

Written by Hong Minhee at Spoqa, and distributed under MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SQLAlchemy-Enum34-1.0.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

SQLAlchemy_Enum34-1.0.0-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file SQLAlchemy-Enum34-1.0.0.tar.gz.

File metadata

File hashes

Hashes for SQLAlchemy-Enum34-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3a75ebf67c797290743c033569c5dd2d5c246b6139ab9cd3477121624cb50439
MD5 c17de83d2a018374f4330dd93c160037
BLAKE2b-256 6be928807f8420c0ee01b4e3a127abb30df3d3b70d53251bfb978d8608208ffa

See more details on using hashes here.

File details

Details for the file SQLAlchemy_Enum34-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for SQLAlchemy_Enum34-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ac20252a4771f34032f6fa03d11cccf6f809bb9aa1a992750b80d866be1f1b11
MD5 e0ae3c9f0efc0588643191cdc36b8aeb
BLAKE2b-256 851691e08cd6e9bdf4be27694592bf628909360af954dedf0fa5ffb81b886c9c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page