Skip to main content

django-data-shape

CI PyPI Python versions Django versions Docs Coverage Ruff License

A realistically shaped test database from Django models.

Declare the shape of your data -- cardinality, value skew, foreign-key fan-out as a distribution with a long tail, and where related rows physically sit -- then load it by COPY and ANALYZE it, so the query planner makes the same choices it will make in production.

It exists because a plan over ten rows is a lie, and because the loop it replaces is not merely smaller: uniform fan-out makes the planner always right, and generating children parent-by-parent clusters them perfectly, which flatters every index scan. A test database can be wrong in the flattering direction, and usually is.

Install

pip install django-data-shape[postgres]

Use

import datetime

from django_data_shape import Sequential, Shape, Skew, Table, Uniform, build

shape = Shape(
    Table(
        Order,
        rows=1_000_000,
        status=Skew({"complete": 0.98, "pending": 0.015, "cancelled": 0.005}),
        total=Uniform(0, 500, places=2),
        created_at=Sequential(
            datetime.datetime(2020, 1, 1, tzinfo=datetime.timezone.utc),
            datetime.timedelta(seconds=3),
        ),
    ),
    seed=1234,
)

build(shape)

build() generates the rows, loads them with COPY, moves the identity sequence past the keys it assigned, and runs ANALYZE so the planner can see the shape. It raises on any backend that is not PostgreSQL rather than degrading quietly.

Relations

Table(
    Order,
    rows=2_000_000,
    # A distribution, not a number: giving every parent ten children is the one
    # shape in which the planner is never wrong, because its n_distinct average
    # is then the truth.
    company=FanOut(Zipf(1.2), childless=0.35),
    ...
)

The parents can be rows this package built or rows your own code did -- their real keys are read, not assumed, so the ORM can own the small tables while this owns the large ones.

Status

Early. Single tables and the model graph. Derived fields, collections copied along a join, per-group invariants and template-database reuse come next.

Full documentation: https://artui.github.io/django-data-shape/

License

MIT

Download files

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

Source Distribution

django_data_shape-0.2.0.tar.gz (146.3 kB view details)

Uploaded Source

Built Distribution

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

django_data_shape-0.2.0-py3-none-any.whl (35.0 kB view details)

Uploaded Python 3

File details

Details for the file django_data_shape-0.2.0.tar.gz.

File metadata

  • Download URL: django_data_shape-0.2.0.tar.gz
  • Upload date:
  • Size: 146.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for django_data_shape-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9c2441e50fffc6d3dc2d9bba12d6ddfdc07a19910afdf254d64c2082fbcbd0a1
MD5 f1068b9b70613e1fdedfe064ea2e9034
BLAKE2b-256 a25a4bc06356e00b7991cc9d1c7bebcd598f58056d4a1c0196583be928cbf28a

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_data_shape-0.2.0.tar.gz:

Publisher: release.yml on Artui/django-data-shape

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

File details

Details for the file django_data_shape-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_data_shape-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50839bbe9b167547c178e6a2c9b0aeae5224a9d2c96b56d10a854088713ea6ac
MD5 4a98fe7bc8921d18a220b84962ecfe00
BLAKE2b-256 6ab646a0ca6a4df7801b830d3d047d328203890c0afdfbab6f72e378119480d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_data_shape-0.2.0-py3-none-any.whl:

Publisher: release.yml on Artui/django-data-shape

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

Release history Release notifications | RSS feed

0.21.0

2 files

0.20.0

2 files

0.19.0

2 files

0.18.1

2 files

0.18.0

2 files

0.17.1

2 files

0.17.0

2 files

0.16.0

2 files

0.15.0

2 files

0.14.0

2 files

0.13.0

2 files

0.12.0

2 files

0.11.0

2 files

0.10.0

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

This release

0.2.0 This release

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page