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


Python declarative.declarative_base方法代碼示例

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


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

示例1: test_temporary_table

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def test_temporary_table(self):
        test_data = u'Hello, World!'
        expected = DataFrame({'spam': [test_data]})
        Base = declarative.declarative_base()

        class Temporary(Base):
            __tablename__ = 'temp_test'
            __table_args__ = {'prefixes': ['TEMPORARY']}
            id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
            spam = sqlalchemy.Column(sqlalchemy.Unicode(30), nullable=False)

        Session = sa_session.sessionmaker(bind=self.conn)
        session = Session()
        with session.transaction:
            conn = session.connection()
            Temporary.__table__.create(conn)
            session.add(Temporary(spam=test_data))
            session.flush()
            df = sql.read_sql_query(
                sql=sqlalchemy.select([Temporary.spam]),
                con=conn,
            )

        tm.assert_frame_equal(df, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:26,代碼來源:test_sql.py

示例2: _with_register_classes

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def _with_register_classes(cls, fn):
        cls_registry = cls.classes

        class FindFixtureDeclarative(DeclarativeMeta):
            def __init__(cls, classname, bases, dict_):
                cls_registry[classname] = cls
                return DeclarativeMeta.__init__(
                    cls, classname, bases, dict_)

        class DeclarativeBasic(object):
            __table_cls__ = schema.Table

        _DeclBase = declarative_base(metadata=cls.metadata,
                                     metaclass=FindFixtureDeclarative,
                                     cls=DeclarativeBasic)
        cls.DeclarativeBasic = _DeclBase
        fn()

        if cls.metadata.tables and cls.run_create_tables:
            cls.metadata.create_all(config.db) 
開發者ID:jpush,項目名稱:jbox,代碼行數:22,代碼來源:fixtures.py

示例3: upgrade

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def upgrade():
    """Apply Set conn_type as non-nullable"""

    Base = declarative_base()

    class Connection(Base):
        __tablename__ = "connection"

        id = sa.Column(sa.Integer(), primary_key=True)
        conn_id = sa.Column(sa.String(250))
        conn_type = sa.Column(sa.String(500))

    # Generate run type for existing records
    connection = op.get_bind()
    sessionmaker = sa.orm.sessionmaker()
    session = sessionmaker(bind=connection)

    # imap_default was missing it's type, let's fix that up
    session.query(Connection).filter_by(conn_id="imap_default", conn_type=None).update(
        {Connection.conn_type: "imap"}, synchronize_session=False
    )
    session.commit()

    with op.batch_alter_table("connection", schema=None) as batch_op:
        batch_op.alter_column("conn_type", existing_type=sa.VARCHAR(length=500), nullable=False) 
開發者ID:apache,項目名稱:airflow,代碼行數:27,代碼來源:8f966b9c467a_set_conn_type_as_non_nullable.py

示例4: test_no_inflect

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def test_no_inflect(metadata):
    Table("simple_items", metadata, Column("id", INTEGER, primary_key=True))

    assert (
        generate_code(metadata, noinflect=True)
        == """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class SimpleItems(Base):
    __tablename__ = 'simple_items'

    id = Column(Integer, primary_key=True)
"""
    ) 
開發者ID:thomaxxl,項目名稱:safrs,代碼行數:22,代碼來源:test_codegen.py

示例5: test_table_kwargs

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def test_table_kwargs(metadata):
    Table("simple_items", metadata, Column("id", INTEGER, primary_key=True), schema="testschema")

    assert (
        generate_code(metadata)
        == """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class SimpleItem(Base):
    __tablename__ = 'simple_items'
    __table_args__ = {'schema': 'testschema'}

    id = Column(Integer, primary_key=True)
"""
    ) 
開發者ID:thomaxxl,項目名稱:safrs,代碼行數:23,代碼來源:test_codegen.py

示例6: test_server_default_double_quotes

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def test_server_default_double_quotes(metadata):
    Table("simple", metadata, Column("id", INTEGER, primary_key=True, server_default=text('nextval("foo")')))

    assert (
        generate_code(metadata)
        == """\
# coding: utf-8
from sqlalchemy import Column, Integer, text
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class Simple(Base):
    __tablename__ = 'simple'

    id = Column(Integer, primary_key=True, server_default=text("nextval(\\"foo\\")"))
"""
    ) 
開發者ID:thomaxxl,項目名稱:safrs,代碼行數:22,代碼來源:test_codegen.py

示例7: test_pascal

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def test_pascal(metadata):
    Table("CustomerAPIPreference", metadata, Column("id", INTEGER, primary_key=True))

    assert (
        generate_code(metadata)
        == """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class CustomerAPIPreference(Base):
    __tablename__ = 'CustomerAPIPreference'

    id = Column(Integer, primary_key=True)
"""
    ) 
開發者ID:thomaxxl,項目名稱:safrs,代碼行數:22,代碼來源:test_codegen.py

示例8: test_underscore

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def test_underscore(metadata):
    Table("customer_api_preference", metadata, Column("id", INTEGER, primary_key=True))

    assert (
        generate_code(metadata)
        == """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class CustomerApiPreference(Base):
    __tablename__ = 'customer_api_preference'

    id = Column(Integer, primary_key=True)
"""
    ) 
開發者ID:thomaxxl,項目名稱:safrs,代碼行數:22,代碼來源:test_codegen.py

示例9: test_pascal_multiple_underscore

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def test_pascal_multiple_underscore(metadata):
    Table("customer_API__Preference", metadata, Column("id", INTEGER, primary_key=True))

    assert (
        generate_code(metadata)
        == """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class CustomerAPIPreference(Base):
    __tablename__ = 'customer_API__Preference'

    id = Column(Integer, primary_key=True)
"""
    ) 
開發者ID:thomaxxl,項目名稱:safrs,代碼行數:22,代碼來源:test_codegen.py

示例10: test_metadata_column

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def test_metadata_column(metadata):
    Table("simple", metadata, Column("id", INTEGER, primary_key=True), Column("metadata", VARCHAR))

    assert (
        generate_code(metadata)
        == """\
# coding: utf-8
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class Simple(Base):
    __tablename__ = 'simple'

    id = Column(Integer, primary_key=True)
    metadata_ = Column('metadata', String)
"""
    ) 
開發者ID:thomaxxl,項目名稱:safrs,代碼行數:23,代碼來源:test_codegen.py

示例11: test_mysql_timestamp

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def test_mysql_timestamp(metadata):
    Table("simple", metadata, Column("id", INTEGER, primary_key=True), Column("timestamp", mysql.TIMESTAMP))

    assert (
        generate_code(metadata)
        == """\
# coding: utf-8
from sqlalchemy import Column, Integer, TIMESTAMP
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class Simple(Base):
    __tablename__ = 'simple'

    id = Column(Integer, primary_key=True)
    timestamp = Column(TIMESTAMP)
"""
    ) 
開發者ID:thomaxxl,項目名稱:safrs,代碼行數:23,代碼來源:test_codegen.py

示例12: calculateAverageTemperatures

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def calculateAverageTemperatures():
    Base = declarative_base()
    Session = sessionmaker(bind=op.get_bind())

    class Sample(Base):
        __tablename__ = 'sample'
        id = sa.Column(sa.Integer, name="id", primary_key=True)
        moveId = sa.Column(sa.Integer, name="move_id", nullable=False)
        temperature = sa.Column(sa.Float, name='temperature')

    class Move(Base):
        __tablename__ = 'move'
        id = sa.Column(sa.Integer, name="id", primary_key=True)
        temperature_avg = sa.Column(sa.Float, name='temperature_avg')

    session = Session()
    averageTemperatures = dict(session.query(Sample.moveId, func.avg(Sample.temperature)).group_by(Sample.moveId).filter(Sample.temperature > 0).all())

    for move in session.query(Move):
        if move.id in averageTemperatures:
            move.temperature_avg = averageTemperatures[move.id]

    session.commit() 
開發者ID:bwaldvogel,項目名稱:openmoves,代碼行數:25,代碼來源:004.py

示例13: model_base

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def model_base():
    """Override this fixture to return declarative base of your model

    http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/api.html

    .. code-block:: python

        from sqlalchemy.ext.declarative import declarative_base

        class Base(object):
            pass

        DeclarativeBase = declarative_base(cls=Base)

        class User(DeclarativeBase):
            __tablename__ = "users"

            id = Column(Integer, primary_key=True)

        @pytest.fixture(scope='session')
        def model_base():
            return DeclarativeBase
    """
    raise NotImplementedError("Fixture `model_base` has to be overwritten") 
開發者ID:nameko,項目名稱:nameko-sqlalchemy,代碼行數:26,代碼來源:pytest_fixtures.py

示例14: init_database

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def init_database():

    Base = declarative_base()

    class Model(Base):
        __tablename__ = 'models'
        TS_name = Column(String(250), nullable=False,primary_key=True)
        TS_winner_name = Column(String(250), nullable=False)
        TS_model = Column(LargeBinary())
        TS_model_params = Column(String(250))
        TS_metric = Column(Numeric)
        TS_update = Column('TS_update', DATETIME, index=False, nullable=False,primary_key=True,default=datetime.datetime.utcnow)

    class TS(Base):
        __tablename__ = 'timeseries'
        TS_name = Column(String(250), nullable=False,primary_key=True)
        TS_data = Column(Text())
        TS_update = Column('TS_update', DATETIME, index=False, nullable=False,primary_key=True,default=datetime.datetime.utcnow)


    DB_NAME = 'sqlite:///Timecop_modelsv1.db'
    engine = create_engine(DB_NAME)
    #self.__db.echo = True
    Base.metadata.create_all(engine) 
開發者ID:BBVA,項目名稱:timecop,代碼行數:26,代碼來源:BBDD.py

示例15: _get_declarative_base

# 需要導入模塊: from sqlalchemy.ext import declarative [as 別名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 別名]
def _get_declarative_base():
    try:
        if FORCE_AVOID_INTERNAL_PACKAGES:
            # TODO(DATAPIPE-1506|abrar): Currently we have
            # force_avoid_internal_packages as a means of simulating an absence
            # of a yelp's internal package. And all references
            # of force_avoid_internal_packages have to be removed from
            # schematizer after we have completely ready for open source.
            raise ImportError
        from yelp_conn.session import declarative_base
    except ImportError:
        from sqlalchemy.ext.declarative import declarative_base
    return declarative_base()


# The common declarative base used by every data model. 
開發者ID:Yelp,項目名稱:schematizer,代碼行數:18,代碼來源:database.py


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