本文整理汇总了Python中sqlalchemy.ext.declarative.api.DeclarativeMeta方法的典型用法代码示例。如果您正苦于以下问题:Python api.DeclarativeMeta方法的具体用法?Python api.DeclarativeMeta怎么用?Python api.DeclarativeMeta使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.declarative.api
的用法示例。
在下文中一共展示了api.DeclarativeMeta方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_everything
# 需要导入模块: from sqlalchemy.ext.declarative import api [as 别名]
# 或者: from sqlalchemy.ext.declarative.api import DeclarativeMeta [as 别名]
def from_everything(everything, engine, limit=None):
"""
Construct a Prettytable from any kinds of sqlalchemy query.
"""
if isinstance(everything, Table):
return from_table(everything, engine, limit=limit)
if type(everything) is DeclarativeMeta:
return from_object(everything, engine, limit=limit)
if isinstance(everything, Query):
return from_query(everything, engine, limit=limit)
if isinstance(everything, Select):
return from_sql(everything, engine, limit=limit)
if isinstance(everything, ResultProxy):
return from_resultproxy(everything)
if isinstance(everything, list):
return from_data(everything)
示例2: test_base_class
# 需要导入模块: from sqlalchemy.ext.declarative import api [as 别名]
# 或者: from sqlalchemy.ext.declarative.api import DeclarativeMeta [as 别名]
def test_base_class(self):
assert isinstance(Base, DeclarativeMeta)
示例3: gen_items
# 需要导入模块: from sqlalchemy.ext.declarative import api [as 别名]
# 或者: from sqlalchemy.ext.declarative.api import DeclarativeMeta [as 别名]
def gen_items():
"""
创建 items
$ python gen.py gen_items
字段规则: 去除自增主键,非自增是需要的。
"""
from models import news
file_path = os.path.join(BASE_DIR, 'news/items.py')
model_list = [(k, v) for k, v in news.__dict__.items() if isinstance(v, DeclarativeMeta) and k != 'Base']
with open(file_path, b'w') as f:
f.write(b'# -*- coding: utf-8 -*-\n\n')
f.write(b'# Define here the models for your scraped items\n#\n')
f.write(b'# See documentation in:\n')
f.write(b'# http://doc.scrapy.org/en/latest/topics/items.html\n\n')
f.write(b'import scrapy\n')
for model_name, model_class in model_list:
result = model_class().to_dict()
table_name = model_class().__tablename__
model_pk = inspect(model_class).primary_key[0].name
f.write(b'\n\nclass %sItem(scrapy.Item):\n' % model_name)
f.write(b' """\n')
f.write(b' table_name: %s\n' % table_name)
f.write(b' primary_key: %s\n' % model_pk)
f.write(b' """\n')
for field_name in list(result.keys()):
if field_name in [model_pk, 'create_time', 'update_time']:
continue
f.write(b' %s = scrapy.Field()\n' % field_name)
示例4: to_alchemy_obj
# 需要导入模块: from sqlalchemy.ext.declarative import api [as 别名]
# 或者: from sqlalchemy.ext.declarative.api import DeclarativeMeta [as 别名]
def to_alchemy_obj(
self
) -> Tuple[DeclarativeMeta, Type[DeclarativeMeta]]:
raise NotImplementedError
示例5: create_resource
# 需要导入模块: from sqlalchemy.ext.declarative import api [as 别名]
# 或者: from sqlalchemy.ext.declarative.api import DeclarativeMeta [as 别名]
def create_resource(self, model, collection_name=None, expose_fields=None):
"""Produce a set of resource endpoints.
Arguments:
model: a model class derived from DeclarativeMeta.
Keyword Args:
collection_name: string name of collection. Passed through to
``collection_view_factory()``
expose_fields: set of field names to be exposed. Passed through to
``collection_view_factory()``
"""
# Find the primary key column from the model and use as 'id_col_name'
try:
keycols = sqlalchemy.inspect(model).primary_key
except sqlalchemy.exc.NoInspectionAvailable:
# Trying to inspect the declarative_base() raises this exception. We
# don't want to add it to the API.
return
# Only deal with one primary key column.
if len(keycols) > 1:
raise Exception(
'Model {} has more than one primary key.'.format(
model.__name__
)
)
if not hasattr(model, '__pyramid_jsonapi__'):
model.__pyramid_jsonapi__ = {}
if 'id_col_name' not in model.__pyramid_jsonapi__:
model.__pyramid_jsonapi__['id_col_name'] = keycols[0].name
# Create a view class for use in the various add_view() calls below.
view = self.collection_view_factory(model,
collection_name or
getattr(
model, '__pyramid_jsonapi__', {}
).get('collection_name') or
sqlalchemy.inspect(model).tables[-1].name,
expose_fields=expose_fields)
self.view_classes[model] = view
view.default_limit = int(self.settings.paging_default_limit)
view.max_limit = int(self.settings.paging_max_limit)
self.endpoint_data.add_routes_views(view)