Skip to main content

Typed Python models (Pydantic v2) for the Civic Transparency PTag specification

Project description

Civic Transparency – Types

CI Status Docs: latest PyPI version Security: bandit Python: 3.12+ License: MIT

Typed Python models (Pydantic v2) for the Civic Transparency PTag schema.


Overview

This package provides strongly-typed Python models that correspond to the Civic Transparency specification. The types are automatically generated from canonical JSON Schema definitions, ensuring consistency and validation at runtime.

Key Features:

  • Type Safety: Full Pydantic v2 validation with IDE support
  • Schema Compliance: Generated directly from official JSON schemas
  • Privacy-First: Designed for aggregated, non-PII data exchange
  • Interoperability: JSON serialization/deserialization with validation

Available Types

Type Description Schema Source
PTagSeries Privacy-preserving time series data for civic topics series.schema.json
PTag Per-post metadata tags (bucketed, no PII) ptag.schema.json

See the API documentation for complete field definitions and examples.


Installation

pip install civic-transparency-py-ptag-types

Note: This package automatically includes the compatible civic-transparency-ptag-spec version as a dependency.


Quick Start

Basic Usage

from ci.transparency.ptag.types import PTagSeries, PTag

# Create a time series for civic data
series = PTagSeries(
    topic="#LocalElection",
    generated_at="2025-01-15T12:00:00Z",
    interval="minute",
    points=[]  # Add your aggregated data points here
)

# Validate and serialize
data = series.model_dump()  # JSON-compatible dict
json_str = series.model_dump_json(indent=2)  # Pretty JSON string

Loading and Validation

from ci.transparency.ptag.types import PTagSeries

# Load from existing data with validation
series = PTagSeries.model_validate(data_dict)
series = PTagSeries.model_validate_json(json_string)

# Handle validation errors
from pydantic import ValidationError
try:
    invalid_series = PTagSeries.model_validate(bad_data)
except ValidationError as e:
    print(f"Validation failed: {e}")

Working with PTags (Provenance Tags)

from ci.transparency.ptag.types import PTag

tag = PTag(
    acct_age_bucket="1-6m",
    acct_type="person",
    automation_flag="manual",
    post_kind="original",
    client_family="mobile",
    media_provenance="hash_only",
    dedup_hash="a1b2c3d4"
)

Validation and Schemas

Pydantic Validation

All models use Pydantic v2 for runtime validation:

  • Strict typing: Unknown fields are rejected
  • Format validation: ISO 8601 dates, patterns, enums
  • Range checking: Min/max values, string lengths
  • Nested validation: Complex object hierarchies

JSON Schema Validation (Optional)

For additional validation against the canonical schemas:

import json
from importlib.resources import files
from jsonschema import Draft202012Validator

# Get the official schema
schema_text = files("ci.transparency.ptag.spec.schemas").joinpath("series.schema.json").read_text()
schema = json.loads(schema_text)

# Validate your data
validator = Draft202012Validator(schema)
validator.validate(series.model_dump())

Versioning Strategy

The package automatically manages compatibility with the corresponding civic-transparency-ptag-spec version.


Integration Examples

FastAPI Integration

from fastapi import FastAPI
from ci.transparency.ptag.types import PTagSeries

app = FastAPI()

@app.post("/civic-data")
async def receive_series(series: PTagSeries) -> dict:
    # Automatic validation and parsing
    return {"received": series.topic, "points": len(series.points)}

File I/O

from pathlib import Path
from ci.transparency.ptag.types import PTagSeries

# Save to file
series_file = Path("data.json")
series_file.write_text(series.model_dump_json(indent=2))

# Load from file
loaded_series = PTagSeries.model_validate_json(series_file.read_text())

Performance Benchmarks

See the latest benchmark results in performance_results.md.


Development and Contributing

This is a generated types package - the source of truth is the civic-transparency-ptag-spec repository.

For Type Users

  • Report type-related issues here
  • Request documentation improvements
  • Share integration examples

For Schema Changes

  • Schema modifications should be made in the spec repository
  • Types are automatically regenerated when the spec changes

Local Development

See CONTRIBUTING.md for detailed guidelines.

Versioning

This specification follows semantic versioning. See CHANGELOG.md for version history.

License

MIT © Civic Interconnect

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

civic_transparency_ptag_types-0.2.5.tar.gz (82.8 kB view details)

Uploaded Source

Built Distribution

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

civic_transparency_ptag_types-0.2.5-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file civic_transparency_ptag_types-0.2.5.tar.gz.

File metadata

File hashes

Hashes for civic_transparency_ptag_types-0.2.5.tar.gz
Algorithm Hash digest
SHA256 d6819181b43961a07e557b8352da6ba432166f6ef7bfffdfa1f4b9f927f23b4b
MD5 93b3685adbf47e1107a2b0777e998493
BLAKE2b-256 8d5850a587ab425afe3e0ce2c3da0b95f08480771b1d9420d049c8565ea9bd0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for civic_transparency_ptag_types-0.2.5.tar.gz:

Publisher: release.yml on civic-interconnect/civic-transparency-py-ptag-types

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

File details

Details for the file civic_transparency_ptag_types-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for civic_transparency_ptag_types-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 664dd3cb952c55f71596ec3cf165b48227bf8d9b1187d12c930128fc7274f03e
MD5 fa448cd267ff0577ae05044abe2d761f
BLAKE2b-256 f8f9f94f50ca875da10f22d3706e5ac34759f2a57e9a43dfcf328c24b519aefb

See more details on using hashes here.

Provenance

The following attestation bundles were made for civic_transparency_ptag_types-0.2.5-py3-none-any.whl:

Publisher: release.yml on civic-interconnect/civic-transparency-py-ptag-types

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