Skip to main content

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.

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

Method Returns Description
as_dicts() list[dict[str, str]] All rows as dicts
as_models(model) list[Model] Rows as Pydantic 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() str JSON string
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

Zero dependencies

behave-tables has no required dependencies. as_models() works with stdlib dataclasses out of the box. Install pydantic optionally for validation.

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

behave_tables-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

behave_tables-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file behave_tables-0.1.0.tar.gz.

File metadata

  • Download URL: behave_tables-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for behave_tables-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f5759ca78161e3728a54ea6e748945c880f0d0774e2f11d8c73e6ab2a7c4fb4c
MD5 cb2acdadd69a59201b7f220503ad6fa7
BLAKE2b-256 12b9208531e4317ed33d8ebb99e8ea02b88bcbb194b2abbc32ecc64e3c33d40e

See more details on using hashes here.

Provenance

The following attestation bundles were made for behave_tables-0.1.0.tar.gz:

Publisher: release.yml on MathiasPaulenko/behave-tables

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file behave_tables-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: behave_tables-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for behave_tables-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0c13b05e09e9a3f6ad03d2dc467b4f02902c2ada0b89364d0827f491b07f12e
MD5 c9eac6034f343fe535cbb55c43996f3e
BLAKE2b-256 f98f0bed2be34f4f7b7af122077b91d57ac68fa6ff56d288d1eefe1eed1aa01e

See more details on using hashes here.

Provenance

The following attestation bundles were made for behave_tables-0.1.0-py3-none-any.whl:

Publisher: release.yml on MathiasPaulenko/behave-tables

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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