本文整理汇总了Python中sqlalchemy.inspection.inspect方法的典型用法代码示例。如果您正苦于以下问题:Python inspection.inspect方法的具体用法?Python inspection.inspect怎么用?Python inspection.inspect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.inspection
的用法示例。
在下文中一共展示了inspection.inspect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_model_pks
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def _get_model_pks(model) -> 'Tuple[str, ...]':
"""
Get primary key field name.
Args:
model: SQLAlchemy model.
Returns:
Field name.
"""
model_pk_fields: 'Tuple[str]' = tuple(
(
cast(str, name)
for name, c in inspection.inspect(model).columns.items()
if c.primary_key
)
)
return model_pk_fields
示例2: _db_row_to_dict
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def _db_row_to_dict(row, remove_columns=False):
"""Converts a DB object to a dictionary."""
from sqlalchemy.inspection import inspect as sa_inspect
from sqlalchemy.ext.hybrid import hybrid_property
row_dict = collections.OrderedDict()
columns = row.__table__.columns.keys()
mapper = sa_inspect(row.__class__)
for key, item in mapper.all_orm_descriptors.items():
if isinstance(item, hybrid_property):
columns.append(key)
for col in columns:
if remove_columns and col in remove_columns:
continue
row_dict[col] = getattr(row, col)
return row_dict
示例3: _loadParams
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def _loadParams(self, newclass):
''' Loads all parameters from wtforms into a dictionary with
key, value = {'parameter_name': 'parent WTForm name'}.
Ignores hidden attributes and the Meta class
'''
model = newclass.Meta.model
schema = model.__table__.schema
tablename = model.__table__.name
mapper = sa_inspect(model)
for key, item in mapper.all_orm_descriptors.items():
if isinstance(item, (hybrid_property, hybrid_method)):
key = key
elif isinstance(item, InstrumentedAttribute):
key = item.key
else:
continue
lookupKeyName = schema + '.' + tablename + '.' + key
self._param_form_lookup[lookupKeyName] = newclass
self._paramtree[newclass.Meta.model.__name__][key]
示例4: sqlalchemy_to_pydantic
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def sqlalchemy_to_pydantic(
db_model: Type, *, exclude: Container[str] = []
) -> Type[BaseModel]:
"""
Mostly copied from https://github.com/tiangolo/pydantic-sqlalchemy
"""
mapper = inspect(db_model)
fields = {}
for attr in mapper.attrs:
if isinstance(attr, ColumnProperty):
if attr.columns:
column = attr.columns[0]
python_type = column.type.python_type
name = attr.key
if name in exclude:
continue
default = None
if column.default is None and not column.nullable:
default = ...
fields[name] = (python_type, default)
pydantic_model = create_model(
db_model.__name__, **fields # type: ignore
)
return pydantic_model
示例5: test_pep435_custom_sort_key
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def test_pep435_custom_sort_key(self):
s = Session()
a = self.classes.T3(id=self.three, value=1)
b = self.classes.T3(id=self.four, value=2)
s.add_all([a, b])
s.commit()
c = self.classes.T3(id=self.five, value=0)
s.add(c)
states = [o._sa_instance_state for o in [b, a, c]]
eq_(
_sort_states(inspect(self.classes.T3), states),
# pending come first, then "four" < "three"
[o._sa_instance_state for o in [c, b, a]],
)
示例6: _serialize
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def _serialize(self, value, attr, obj, **kwargs):
return inspect(value).identity
示例7: serialize
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def serialize(self, obj_type=None, relations=False):
attrs = inspect(self).attrs.keys()
obj_dict = {
attr: serialize_value(getattr(self, attr)) for attr in attrs
}
obj_dict["notification_type"] = obj_dict["type"]
obj_dict["type"] = obj_type or type(self).__name__
return obj_dict
示例8: serialize
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def serialize(self, obj_type=None, relations=False):
attrs = inspect(self).attrs.keys()
if relations:
obj_dict = {
attr: serialize_value(getattr(self, attr)) for attr in attrs
}
else:
obj_dict = {
attr: serialize_value(getattr(self, attr))
for attr in attrs
if not self.is_join(attr)
}
obj_dict["type"] = obj_type or type(self).__name__
return obj_dict
示例9: create
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def create(cls, **kwargs):
instance = cls(**kwargs)
# Populate all the unspecified columns with their defaults
for key, column in inspect(cls).columns.items():
if key not in kwargs and column.default is not None:
arg = column.default.arg
column_default = arg if callable(arg) else lambda: arg
setattr(instance, key, column_default(instance))
return instance
示例10: delete
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def delete(self, db_session=None):
db = db_session or inspect(self).session
db.delete(self)
示例11: fields
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def fields(self):
model = self.index.model
schema_fields = self._fields()
primary_keys = [key.name for key in inspect(model).primary_key]
schema = getattr(model, "__msearch_schema__", dict())
for field in self.index.searchable:
if '.' in field:
fields = field.split('.')
field_attr = getattr(
getattr(model, fields[0]).property.mapper.class_,
fields[1])
else:
field_attr = getattr(model, field)
if field in schema:
field_type = schema[field]
if isinstance(field_type, str):
schema_fields[field] = self.fields_map(field_type)
else:
schema_fields[field] = field_type
continue
if hasattr(field_attr, 'descriptor') and isinstance(
field_attr.descriptor, hybrid_property):
schema_fields[field] = self.fields_map("text")
continue
if field in primary_keys:
schema_fields[field] = self.fields_map("primary")
continue
field_type = field_attr.property.columns[0].type
schema_fields[field] = self.fields_map(field_type)
return schema_fields
示例12: __getattr__
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def __getattr__(cls, item):
if item == "__table__":
return inspect(cls).local_table
raise AttributeError(item)
示例13: has_sqlalchemy_fields
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def has_sqlalchemy_fields(base):
for p in base.__dict__.keys():
attr = base.__dict__[p]
if inspection.inspect(attr, raiseerr=False) is not None:
return True
return False
示例14: get_parent_value
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def get_parent_value(instance):
return inspection.inspect(instance).attrs.parent.loaded_value
示例15: to_dict
# 需要导入模块: from sqlalchemy import inspection [as 别名]
# 或者: from sqlalchemy.inspection import inspect [as 别名]
def to_dict(self):
return {c.key: getattr(self, c.key)
for c in inspect(self).mapper.column_attrs}