當前位置: 首頁>>代碼示例>>Python>>正文


Python types.Enum方法代碼示例

本文整理匯總了Python中sqlalchemy.types.Enum方法的典型用法代碼示例。如果您正苦於以下問題:Python types.Enum方法的具體用法?Python types.Enum怎麽用?Python types.Enum使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sqlalchemy.types的用法示例。


在下文中一共展示了types.Enum方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: print_create_table

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def print_create_table(tables):
    app.config.from_object('config.default')
    database.init_app(app)

    engine = database.session.get_bind()

    for class_name in tables:
        cls = get_class(class_name)

        for c in cls.__table__.columns:
            if not isinstance(c.type, Enum):
                continue
            t = c.type
            sql = str(CreateEnumType(t).compile(engine))
            click.echo(sql.strip() + ';')

        for index in cls.__table__.indexes:
            sql = str(CreateIndex(index).compile(engine))
            click.echo(sql.strip() + ';')

        sql = str(CreateTable(cls.__table__).compile(engine))
        click.echo(sql.strip() + ';') 
開發者ID:EdwardBetts,項目名稱:osm-wikidata,代碼行數:24,代碼來源:cli.py

示例2: test_should_enum_convert_enum

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_should_enum_convert_enum():
    field = get_field(types.Enum(enum.Enum("TwoNumbers", ("one", "two"))))
    field_type = field.type()
    assert isinstance(field_type, graphene.Enum)
    assert field_type._meta.name == "TwoNumbers"
    assert hasattr(field_type, "ONE")
    assert not hasattr(field_type, "one")
    assert hasattr(field_type, "TWO")
    assert not hasattr(field_type, "two")

    field = get_field(types.Enum("one", "two", name="two_numbers"))
    field_type = field.type()
    assert isinstance(field_type, graphene.Enum)
    assert field_type._meta.name == "TwoNumbers"
    assert hasattr(field_type, "ONE")
    assert not hasattr(field_type, "one")
    assert hasattr(field_type, "TWO")
    assert not hasattr(field_type, "two") 
開發者ID:graphql-python,項目名稱:graphene-sqlalchemy,代碼行數:20,代碼來源:test_converter.py

示例3: test_create_table

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_create_table(self, connection):
        metadata = self.metadata
        t1 = Table(
            "table",
            metadata,
            Column("id", Integer, primary_key=True),
            Column(
                "value", Enum("one", "two", "three", name="onetwothreetype")
            ),
        )
        t1.create(connection)
        t1.create(connection, checkfirst=True)  # check the create
        connection.execute(t1.insert(), value="two")
        connection.execute(t1.insert(), value="three")
        connection.execute(t1.insert(), value="three")
        eq_(
            connection.execute(t1.select().order_by(t1.c.id)).fetchall(),
            [(1, "two"), (2, "three"), (3, "three")],
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:21,代碼來源:test_types.py

示例4: test_generate_multiple

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_generate_multiple(self):
        """Test that the same enum twice only generates once
        for the create_all() call, without using checkfirst.

        A 'memo' collection held by the DDL runner
        now handles this.

        """
        metadata = self.metadata

        e1 = Enum("one", "two", "three", name="myenum")
        Table("e1", metadata, Column("c1", e1))

        Table("e2", metadata, Column("c1", e1))

        metadata.create_all(checkfirst=False)
        metadata.drop_all(checkfirst=False)
        assert "myenum" not in [
            e["name"] for e in inspect(testing.db).get_enums()
        ] 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:22,代碼來源:test_types.py

示例5: test_generate_alone_on_metadata

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_generate_alone_on_metadata(self):
        """Test that the same enum twice only generates once
        for the create_all() call, without using checkfirst.

        A 'memo' collection held by the DDL runner
        now handles this.

        """
        metadata = self.metadata

        Enum("one", "two", "three", name="myenum", metadata=self.metadata)

        metadata.create_all(checkfirst=False)
        assert "myenum" in [e["name"] for e in inspect(testing.db).get_enums()]
        metadata.drop_all(checkfirst=False)
        assert "myenum" not in [
            e["name"] for e in inspect(testing.db).get_enums()
        ] 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:20,代碼來源:test_types.py

示例6: test_generate_multiple_on_metadata

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_generate_multiple_on_metadata(self):
        metadata = self.metadata

        e1 = Enum("one", "two", "three", name="myenum", metadata=metadata)

        t1 = Table("e1", metadata, Column("c1", e1))

        t2 = Table("e2", metadata, Column("c1", e1))

        metadata.create_all(checkfirst=False)
        assert "myenum" in [e["name"] for e in inspect(testing.db).get_enums()]
        metadata.drop_all(checkfirst=False)
        assert "myenum" not in [
            e["name"] for e in inspect(testing.db).get_enums()
        ]

        e1.create()  # creates ENUM
        t1.create()  # does not create ENUM
        t2.create()  # does not create ENUM 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:21,代碼來源:test_types.py

示例7: test_remain_on_table_metadata_wide

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_remain_on_table_metadata_wide(self):
        metadata = self.metadata

        e1 = Enum("one", "two", "three", name="myenum", metadata=metadata)
        table = Table("e1", metadata, Column("c1", e1))

        # need checkfirst here, otherwise enum will not be created
        assert_raises_message(
            sa.exc.ProgrammingError,
            '.*type "myenum" does not exist',
            table.create,
        )
        table.create(checkfirst=True)
        table.drop()
        table.create(checkfirst=True)
        table.drop()
        assert "myenum" in [e["name"] for e in inspect(testing.db).get_enums()]
        metadata.drop_all()
        assert "myenum" not in [
            e["name"] for e in inspect(testing.db).get_enums()
        ] 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:23,代碼來源:test_types.py

示例8: test_standalone_enum

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_standalone_enum(self):
        metadata = MetaData(testing.db)
        etype = Enum(
            "four", "five", "six", name="fourfivesixtype", metadata=metadata
        )
        etype.create()
        try:
            assert testing.db.dialect.has_type(testing.db, "fourfivesixtype")
        finally:
            etype.drop()
            assert not testing.db.dialect.has_type(
                testing.db, "fourfivesixtype"
            )
        metadata.create_all()
        try:
            assert testing.db.dialect.has_type(testing.db, "fourfivesixtype")
        finally:
            metadata.drop_all()
            assert not testing.db.dialect.has_type(
                testing.db, "fourfivesixtype"
            ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:23,代碼來源:test_types.py

示例9: test_reflection

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_reflection(self):
        metadata = self.metadata
        etype = Enum(
            "four", "five", "six", name="fourfivesixtype", metadata=metadata
        )
        Table(
            "table",
            metadata,
            Column("id", Integer, primary_key=True),
            Column(
                "value", Enum("one", "two", "three", name="onetwothreetype")
            ),
            Column("value2", etype),
        )
        metadata.create_all()
        m2 = MetaData(testing.db)
        t2 = Table("table", m2, autoload=True)
        eq_(t2.c.value.type.enums, ["one", "two", "three"])
        eq_(t2.c.value.type.name, "onetwothreetype")
        eq_(t2.c.value2.type.enums, ["four", "five", "six"])
        eq_(t2.c.value2.type.name, "fourfivesixtype") 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:23,代碼來源:test_types.py

示例10: test_array_plus_native_enum_create

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_array_plus_native_enum_create(self):
        m = MetaData()
        t = Table(
            "t",
            m,
            Column(
                "data_1",
                self.ARRAY(postgresql.ENUM("a", "b", "c", name="my_enum_1")),
            ),
            Column(
                "data_2",
                self.ARRAY(types.Enum("a", "b", "c", name="my_enum_2")),
            ),
        )

        t.create(testing.db)
        eq_(
            set(e["name"] for e in inspect(testing.db).get_enums()),
            set(["my_enum_1", "my_enum_2"]),
        )
        t.drop(testing.db)
        eq_(inspect(testing.db).get_enums(), []) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:24,代碼來源:test_types.py

示例11: test_enum_compile

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_enum_compile(self):
        e1 = Enum("x", "y", "z", name="somename")
        t1 = Table("sometable", MetaData(), Column("somecolumn", e1))
        self.assert_compile(
            schema.CreateTable(t1),
            "CREATE TABLE sometable (somecolumn " "ENUM('x','y','z'))",
        )
        t1 = Table(
            "sometable",
            MetaData(),
            Column(
                "somecolumn",
                Enum("x", "y", "z", native_enum=False, create_constraint=True),
            ),
        )
        self.assert_compile(
            schema.CreateTable(t1),
            "CREATE TABLE sometable (somecolumn "
            "VARCHAR(1), CHECK (somecolumn IN ('x', "
            "'y', 'z')))",
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:23,代碼來源:test_types.py

示例12: test_broken_enum_returns_blanks

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_broken_enum_returns_blanks(self):
        t = Table(
            "enum_missing",
            self.metadata,
            Column("id", Integer, primary_key=True),
            Column("e1", sqltypes.Enum("one", "two", "three")),
            Column("e2", mysql.ENUM("one", "two", "three")),
        )
        t.create()

        with testing.db.connect() as conn:
            conn.execute(
                t.insert(), {"e1": "nonexistent", "e2": "nonexistent"}
            )
            conn.execute(t.insert(), {"e1": "", "e2": ""})
            conn.execute(t.insert(), {"e1": "two", "e2": "two"})
            conn.execute(t.insert(), {"e1": None, "e2": None})

            eq_(
                conn.execute(
                    select([t.c.e1, t.c.e2]).order_by(t.c.id)
                ).fetchall(),
                [("", ""), ("", ""), ("two", "two"), (None, None)],
            ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:26,代碼來源:test_types.py

示例13: test_python_type

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_python_type(self):
        eq_(types.Integer().python_type, int)
        eq_(types.Numeric().python_type, decimal.Decimal)
        eq_(types.Numeric(asdecimal=False).python_type, float)
        eq_(types.LargeBinary().python_type, util.binary_type)
        eq_(types.Float().python_type, float)
        eq_(types.Interval().python_type, datetime.timedelta)
        eq_(types.Date().python_type, datetime.date)
        eq_(types.DateTime().python_type, datetime.datetime)
        eq_(types.String().python_type, str)
        eq_(types.Unicode().python_type, util.text_type)
        eq_(types.Enum("one", "two", "three").python_type, str)

        assert_raises(
            NotImplementedError, lambda: types.TypeEngine().python_type
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:18,代碼來源:test_types.py

示例14: test_pep435_custom_sort_key

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_pep435_custom_sort_key(self):
        one, two, a_member, b_member = (
            self.one,
            self.two,
            self.a_member,
            self.b_member,
        )

        def sort_enum_key_value(value):
            return str(value.value)

        typ = Enum(self.SomeEnum, sort_key_function=sort_enum_key_value)
        is_(typ.sort_key_function, sort_enum_key_value)

        eq_(
            sorted([two, one, a_member, b_member], key=typ.sort_key_function),
            [one, two, a_member, b_member],
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:20,代碼來源:test_types.py

示例15: test_enum_constraint_type_doesnt_double

# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import Enum [as 別名]
def test_enum_constraint_type_doesnt_double(self):
        m1 = MetaData()

        t1 = Table(
            "x",
            m1,
            Column("flag", Enum("a", "b", "c", create_constraint=True)),
        )
        eq_(
            len([c for c in t1.constraints if isinstance(c, CheckConstraint)]),
            1,
        )
        m2 = MetaData()
        t2 = t1.to_metadata(m2)

        eq_(
            len([c for c in t2.constraints if isinstance(c, CheckConstraint)]),
            1,
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:21,代碼來源:test_metadata.py


注:本文中的sqlalchemy.types.Enum方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。