Convenience wrapper for DB API and csv.DictReader rows, and similar.
Project description
brunns-row
Convenience wrapper for DB API and csv.DictReader rows, and similar, inspired by Greg Stein's lovely dtuple module.
Setup
Install with pip:
pip install brunns-row
(As usual, use of a venv or virtualenv is recommended.)
Usage
The basic approach is to create a wrapper object from some kind of description - typically a
DBAPI cursor's
description, or a
csv.DictReader's
fieldnames attribute - then to use the wrapper's
wrap(row)
method to wrap each row. wrap(row)
returns an object from which you can access the row's fields as
attributes. A couple of simple examples:
DB API
cursor = conn.cursor()
cursor.execute("SELECT kind, rating FROM sausages ORDER BY rating DESC;")
wrapper = RowWrapper(cursor.description)
rows = [wrapper.wrap(row) for row in cursor.fetchall()]
for row in rows:
print(row.kind, row.rating)
csv.DictReader
reader = csv.DictReader(csv_file)
wrapper = RowWrapper(reader.fieldnames)
rows = [wrapper.wrap(row) for row in reader]
for row in rows:
print(row.kind, row.rating)
Attributes names are simply the column names where possible, converted to valid identifiers where necessary by replacing invalid characters with "_"s, prefixing any leading numerics with "a_", and de-duplicating where necessary by adding numeric suffixes.
Developing
Requires tox. Run make precommit
tells you if you're OK to commit. For more options, run:
make help
Releasing
Requires hub, setuptools, wheel and twine. To release n.n.n
:
version="n.n.n"
git commit -am"Release $version" && git push # If not already all pushed, which it should be.
hub release create $version -m"Release $version"
python3 setup.py sdist bdist_wheel
twine upload dist/*$version*
Quick version:
version="n.n.n"
git commit -am"Release $version" && git push && hub release create $version -m"Release $version" && python setup.py sdist bdist_wheel && twine upload dist/*$version*
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
Built Distribution
Hashes for brunns_row-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 680bb033770e14c854ed91c7221bfb526a6c01ac997cd252fe50c4a4b16f4d61 |
|
MD5 | 8a354dd2890ac206aa42712cc2b5d18d |
|
BLAKE2b-256 | 2a7eb3386953822ac067824e7af79550f0488535810791faee5046394edb596e |