def test_repr_partial(self, Article): """Representation of a basic model with selected fields.""" Article = generic_repr('id')(Article) article = Article(id=1, name=u'Foo') expected_repr = u'Article(id=1)' actual_repr = repr(article) assert actual_repr == expected_repr
def test_repr(self, Article): """Representation of a basic model.""" Article = generic_repr(Article) article = Article(id=1, name=u'Foo') if sys.version_info[0] == 2: expected_repr = u'Article(id=1, name=u\'Foo\')' elif sys.version_info[0] == 3: expected_repr = u'Article(id=1, name=\'Foo\')' else: raise AssertionError actual_repr = repr(article) assert actual_repr == expected_repr
def test_not_loaded(self, session, Article): """:py:func:`~sqlalchemy_utils.models.generic_repr` doesn't force execution of additional queries if some fields are not loaded and instead represents them as "<not loaded>". """ Article = generic_repr(Article) article = Article(name=u'Foo') session.add(article) session.commit() article = session.query(Article).options(sa.orm.defer('name')).one() actual_repr = repr(article) expected_repr = u'Article(id={}, name=<not loaded>)'.format(article.id) assert actual_repr == expected_repr
from pandas import DataFrame from sqlalchemy.ext.automap import automap_base from sqlalchemy import create_engine, MetaData from sqlalchemy_utils import generic_repr from sqlalchemy.orm import scoped_session, sessionmaker from thirdparty.sqlalchemy import name_for_scalar_relationship, pluralize_collection, camelize_classname m = MetaData() Base = generic_repr(automap_base(metadata=m)) def _repr_html_(self): from helpers.gui.namespace import HorizontalNamespace data = {k: v for k, v in self.__dict__.items() if not k.startswith('_')} namespace = HorizontalNamespace(data) namespace.__name__ = self.__class__.__name__ return namespace._repr_html_() Base._repr_html_ = _repr_html_ # ./data/drugcentral/download.sh # ./data/drugcentral/install.sh engine = create_engine( "postgresql+psycopg2://drugcentral:password@localhost/drugcentral") m.reflect(engine, views=True) # reflect the tables omop_relationship_doid_view = m.tables['omop_relationship_doid_view']