Polished API for Behave Data Tables — as_dicts, as_models, transpose, to_csv, to_json
Project description
behave-tables
Polished API for Behave Data Tables — convert to dicts, Pydantic models, CSV & JSON. Zero deps, type-safe, Python 3.11+.
Why?
Behave's Table requires manual iteration: for row in table.rows: name = row["name"]. No conversion to dicts, models, CSV, or JSON. Every project reimplements helpers.
Install
pip install behave-tables
# Optional: pydantic for as_models() with validation
pip install behave-tables[pydantic]
Quick start
from behave_tables import wrap
# In a step definition
@then("the users should be")
def step_impl(context):
table = wrap(context.table)
# As dicts
users = table.as_dicts()
# [{"name": "Alice", "age": "30"}, {"name": "Bob", "age": "25"}]
# As models (Pydantic or dataclass)
users = table.as_models(User)
# Column extraction
names = table.column("name") # ["Alice", "Bob"]
# Find first matching row
alice = table.find_row(name="Alice") # {"name": "Alice", "age": "30"}
# Export
csv_str = table.to_csv()
json_str = table.to_json()
# Transpose
transposed = table.transpose()
# Iterate as dicts
for row in table:
print(row["name"])
# Index
table[0] # {"name": "Alice", "age": "30"}
len(table) # 2
API
wrap(table)
Wrap a behave.model.Table (or any table-like object) with TableWrapper.
TableWrapper
| Method | Returns | Description |
|---|---|---|
as_dicts() |
list[dict[str, str]] |
All rows as dicts (copies) |
as_models(model) |
list[Model] |
Rows as Pydantic v2 or dataclass instances |
column(name) |
list[str] |
Values for a single column |
find_row(**filters) |
dict[str, str] | None |
First row matching all filters |
validate_columns(*expected) |
None |
Raise ColumnMismatchError if columns missing |
transpose() |
TableWrapper |
New wrapper with rows and columns swapped |
to_csv() |
str |
CSV string |
to_json(indent=2) |
str |
JSON string (list of objects) |
headers |
list[str] |
Column names |
__iter__ |
Iterator[dict] |
Iterate rows as dicts |
__getitem__(i) |
dict[str, str] |
Row at index as dict |
__len__ |
int |
Number of rows |
ColumnMismatchError
Raised by validate_columns() when expected columns are missing. Subclass of ValueError.
from behave_tables import wrap, ColumnMismatchError
table = wrap(context.table)
try:
table.validate_columns("name", "email")
except ColumnMismatchError as e:
print(e.missing) # ["email"]
Examples
With dataclasses
from dataclasses import dataclass
@dataclass
class User:
name: str
age: int
@then("the users should be")
def step_impl(context):
users = wrap(context.table).as_models(User)
assert users[0].name == "Alice"
With Pydantic v2
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
@then("the users should be")
def step_impl(context):
users = wrap(context.table).as_models(User)
# Pydantic validates and coerces types automatically
assert users[0].age == 30 # int, not str
Validate columns
@then("the user list should be")
def step_impl(context):
table = wrap(context.table)
table.validate_columns("name", "email", "role")
Transpose
@then("the data should be")
def step_impl(context):
table = wrap(context.table)
transposed = table.transpose()
# Original headers become the first column ("_column")
# Original row indices become the new headers ("0", "1", ...)
Export
@then("the report is generated")
def step_impl(context):
table = wrap(context.table)
csv_output = table.to_csv()
json_output = table.to_json(indent=4)
Zero dependencies
behave-tables has no required dependencies. as_models() works with stdlib dataclasses out of the box. Install pydantic optionally for validation and type coercion.
Development
make dev # install with dev dependencies
make test # run tests
make test-cov # run tests with coverage
make lint # check code style
make lint-fix # auto-fix lint issues
See CONTRIBUTING.md for details.
License
MIT
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file behave_tables-1.0.0.tar.gz.
File metadata
- Download URL: behave_tables-1.0.0.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ceb913501ee1c6a5dc4b0a98c389e0ef29260c89048811e9c95c8291157cbd3
|
|
| MD5 |
b0e547bdddbf867deef7018fb03fc289
|
|
| BLAKE2b-256 |
117e1e7ab840d948d2e9da60ee0bcfd5415aac017bc15535861584e8be72289f
|
Provenance
The following attestation bundles were made for behave_tables-1.0.0.tar.gz:
Publisher:
release.yml on MathiasPaulenko/behave-tables
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
behave_tables-1.0.0.tar.gz -
Subject digest:
0ceb913501ee1c6a5dc4b0a98c389e0ef29260c89048811e9c95c8291157cbd3 - Sigstore transparency entry: 2158419135
- Sigstore integration time:
-
Permalink:
MathiasPaulenko/behave-tables@95b1e6c48974fcd29b2c7a1473e502f2953247a3 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/MathiasPaulenko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@95b1e6c48974fcd29b2c7a1473e502f2953247a3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file behave_tables-1.0.0-py3-none-any.whl.
File metadata
- Download URL: behave_tables-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8acbc902a2caeb3094a653346ffd141309bff37adcc604e07206db6d4fa61d61
|
|
| MD5 |
91003c58bdfbe89e5039438150d4e9d0
|
|
| BLAKE2b-256 |
0d5833189b0fd5318d5b789dfe02181d60a7786a5c09a6c77881a0d409833358
|
Provenance
The following attestation bundles were made for behave_tables-1.0.0-py3-none-any.whl:
Publisher:
release.yml on MathiasPaulenko/behave-tables
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
behave_tables-1.0.0-py3-none-any.whl -
Subject digest:
8acbc902a2caeb3094a653346ffd141309bff37adcc604e07206db6d4fa61d61 - Sigstore transparency entry: 2158419163
- Sigstore integration time:
-
Permalink:
MathiasPaulenko/behave-tables@95b1e6c48974fcd29b2c7a1473e502f2953247a3 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/MathiasPaulenko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@95b1e6c48974fcd29b2c7a1473e502f2953247a3 -
Trigger Event:
push
-
Statement type: