Skip to main content

Converting pydantic classes to spark schemas

Project description

Python package codecov PyPI version CodeQL

pydantic-spark

This library can convert a pydantic class to a spark schema or generate python code from a spark schema.

Install

pip install pydantic-spark

Pydantic class to spark schema

import json
from typing import Optional

from pydantic_spark.base import SparkBase

class TestModel(SparkBase):
    key1: str
    key2: int
    key2: Optional[str]

schema_dict: dict = TestModel.spark_schema()
print(json.dumps(schema_dict))

Coerce type

Pydantic-spark provides a coerce_type option that allows type coercion. When applied to a field, pydantic-spark converts the column's data type to the specified coercion type.

import json
from pydantic import Field
from pydantic_spark.base import SparkBase, CoerceType

class TestModel(SparkBase):
    key1: str = Field(extra_json_schema={"coerce_type": CoerceType.integer})

schema_dict: dict = TestModel.spark_schema()
print(json.dumps(schema_dict))

Install for developers

Install package
  • Requirement: Poetry 1.*
poetry install
Run unit tests
pytest
coverage run -m pytest  # with coverage
# or (depends on your local env) 
poetry run pytest
poetry run coverage run -m pytest  # with coverage
Run linting

The linting is checked in the github workflow. To fix and review issues run this:

black .   # Auto fix all issues
isort .   # Auto fix all issues
pflake .  # Only display issues, fixing is manual

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

pydantic_spark-0.3.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

pydantic_spark-0.3.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_spark-0.3.0.tar.gz.

File metadata

  • Download URL: pydantic_spark-0.3.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.12.0 Darwin/22.6.0

File hashes

Hashes for pydantic_spark-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6f2f1dbed58696d664b15f9d0c0a56bdbd605242e6e518994ffc9fcb1f6a65a6
MD5 43a44df75f5941fb6835fe9b9a52269a
BLAKE2b-256 3a79a5f3d767ae797f5ae5e42a41ed31b908330fa0d27ce08ef9312b9cac4d3b

See more details on using hashes here.

File details

Details for the file pydantic_spark-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_spark-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.12.0 Darwin/22.6.0

File hashes

Hashes for pydantic_spark-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 939b21eadd1322521a0c77a098274c3571df29f34091d3106bb44abe0a0b50c0
MD5 09088df3589d0ec6a5b7422dbbea3f6f
BLAKE2b-256 8ecec7ac514b72a8b658398aba9f38bf3d28f2c27ae69eed1b0b21ac08e831c9

See more details on using hashes here.

Supported by

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