Skip to main content

Usefull tools for setting/getting datas from SQLAlchemy models

Project description

Provide tools for sqlalchemy models :

  • inspection

  • export (csv, excel, py3o context)

  • import

requires:

openpyxl py3o colanderalchemy SQLAlchemy

Export

from sqla_inspect.csv import SqlaCsvExporter

exporter = SqlaCsvExporter(Model)
for row in Model.query():
    exporter.add_row(row)
exporter.render()

Customization

Globally

You can globally set formatters through which value of a specific type will be passed before export.

from sqla_inspect.export import FORMATTERS_REGISTRY
FORMATTERS_REGISTRY.add_formatter(
    sqlalchemy.Boolean,
    lambda val: 'Y' and val or 'N'
)

All booleans will be converted to ‘Y’ or ‘N’. If you want to do this formatting only for a specific export, add a key (the key as configured in the exporter class, csv/excel/py3o for provided exporters)

from sqla_inspect.export import FORMATTERS_REGISTRY
FORMATTERS_REGISTRY.add_formatter(
    sqlalchemy.Boolean,
    lambda val: 'Y' and val or 'N',
    'csv'
)

You can globally blacklist some fields to avoid exporting them

from sqla_inspect.export import BLACKLISTED_KEYS
BLACKLISTED_KEYS = ('_acl', 'password')

Per Column

You can customize columns informations:

  • The header label through the ‘label’ key

  • The way a relationship is exported through the ‘related_key’ (the attribute on the related object)

  • The way the datas is formatted providing a formatter under the ‘formatter’ key

  • Exclude a column setting the ‘exclude’ key

All this keys can be set at differente levels:

If you want to customize the header labels, you can provide informations in the export/csv key

class Model(Base):
    attr1 = Column(
        Integer,
        info={'export': {'csv': {'label': u'My custom header'}}}
    )

If not set it will look one level higher in the export key

class Model(Base):
    attr1 = Column(
        Integer,
        info={'export': {'label': u'My custom header'}}
    )

If not set, only in the case of labels, it will look into the colanderalchemy ‘title’ attribute.

The same things can be done with the excel.SqlaXlsExporter class (that shares the export dict with the SqlaCsvExporter.

LIMITATIONS

Relationship that point to lists are not handled.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqla_inspect-0.1.1.tar.gz (16.1 kB view details)

Uploaded Source

File details

Details for the file sqla_inspect-0.1.1.tar.gz.

File metadata

File hashes

Hashes for sqla_inspect-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2d84a83398f36c907e6a317bfdc15a3e9186820aa265f9759e7bbb3c86f1885b
MD5 55dfb46e728b71d3e3a93fbc4749582b
BLAKE2b-256 c191fa9bc32b89b362f4a5df7053252bc81a29592fa25d1df47980a82ac7923d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page