我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用sqlalchemy.ext.declarative.as_declarative()。
def as_declarative(**kw): """ Class decorator for :func:`.declarative_base`. Provides a syntactical shortcut to the ``cls`` argument sent to :func:`.declarative_base`, allowing the base class to be converted in-place to a "declarative" base:: from sqlalchemy.ext.declarative import as_declarative @as_declarative() class Base(object): @declared_attr def __tablename__(cls): return cls.__name__.lower() id = Column(Integer, primary_key=True) class MyMappedClass(Base): # ... All keyword arguments passed to :func:`.as_declarative` are passed along to :func:`.declarative_base`. .. versionadded:: 0.8.3 .. seealso:: :func:`.declarative_base` """ def decorate(cls): kw['cls'] = cls kw['name'] = cls.__name__ return declarative_base(**kw) return decorate
def repr_entity(entity: object) -> str: """Make a representation string for the given ``entity`` object. If the class specified ``__repr_columns__`` it prints these attributes instead of its primary keys. .. code-block:: from sqlalchemy.ext.declarative import as_declarative from ormeasy.sqlalchemy import repr_entity @as_declarative() class Base: def __repr__(self): return repr_entity(self) class Song(Base): __tablename__ = 'song' __repr_columns__ = 'name', 'genre' id = Column(Integer, primary_key=True) name = Column(Unicode) genre = Column(Unicode) print(repr(Song(name='hello', genre='brit-pop'))) # it prints `<Song name=hello, genre=brit-pop>` .. seealso:: ``as_declarative`` http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/api.html#sqlalchemy.ext.declarative.as_declarative :param entity: an object to make a representation string :return: a representation string :rtype: :class:`str` """ # noqa cls = type(entity) mod = cls.__module__ name = ('' if mod == '__main__ ' else mod + '.') + cls.__qualname__ try: columns = entity.__repr_columns__ except AttributeError: columns = cls.__mapper__.primary_key names = (column if isinstance(column, str) else column.name for column in columns) pairs = ((name, getattr(entity, name)) for name in names if hasattr(entity, name)) args = ' '.join(k + '=' + repr(v) for k, v in pairs) return '<{0} {1}>'.format(name, args)