SQLAlchemy utilities for normalising / denormalising table data, useful for CSV
Project description
sqlalchemy-csv-normalise
SQLAlchemy utilities for normalising / denormalising table data, useful for CSV
Where a table is normalised to have “lookup tables” of values referred to by e.g. a numeric foreign-key ID, these functions will enable extraction of the data (or conversely, loading from such) with the looked-up values substituted in. Among other things, this allows more human-friendly data editing in e.g. a spreadsheet.
Example:
from sqlalchemy_csv_normalise import denormalise_prepare q, col_names = denormalise_prepare(db.session, table) filename = table_to_filename(table) with open(filename, 'w', newline='') as csv_file: csv_file_writer = csv.writer(csv_file) csv_file_writer.writerow(col_names) csv_file_writer.writerows(q.all()) from sqlalchemy_csv_normalise import renormalise_prepare, empty_deleter,\ type_coercer row_maker = renormalise_prepare(db.session, table) row_cleaner = empty_deleter(table) row_coercer = type_coercer(table) filename = table_to_filename(table) with open(filename, newline='') as csv_file: for d in csv.DictReader(csv_file): row = row_coercer(row_cleaner(row_maker(d))) db.session.add(table(**row)) db.session.commit()
Free software: MIT license
Documentation: https://sqlalchemy-csv-normalise.readthedocs.io.
Features
denormalise_prepare(session, table, colname_tidier)
Returns SQLAlchemy query, and the column-names it will return. The query will denormalise any foreign keys (FKs) if they refer to a table with a unique column that is not its primary key.
The names of any FK columns will have _id taken off the end as a simple heuristic. Override this by providing a colname_tidier.
empty_deleter(table)
Returns function that returns given dict minus empty strings for nullable columns. Useful because CSV has no way to record NULL.
type_coercer(table)
Returns function that given a row dict will coerce values. Works on dates and booleans.
renormalise_prepare(session, table, colname_tidier)
Returns function that will renormalise given dictionary Does the inverse of denormalise_prepare.
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
History
0.1.1 (2020-02-01)
First release on PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sqlalchemy-csv-normalise-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3427a26b6b3ed2ff06c1b5d887f4a93542add5af0461a5104676ccf050856fa |
|
MD5 | 1c98f3d5f5ffad0351962aea16eab93f |
|
BLAKE2b-256 | 9ba63f4c2b7796ab6d8d2777779d3125f54612ac43f80de01612ad9f68a5d4ca |
Hashes for sqlalchemy_csv_normalise-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6091ceb7781d9a4bb1a0fef6fa281b752467fba3dc5ed4835c6992c23cb2f787 |
|
MD5 | e1183bc27c5b4d4015984351c8ca7d87 |
|
BLAKE2b-256 | 36fade1723f48bd565e4a354a54b8a911757de7293228c128e088d339a2b6e9b |