Converting pydantic classes to spark schemas
Project description
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-1.0.1.tar.gz
(4.8 kB
view details)
Built Distribution
File details
Details for the file pydantic_spark-1.0.1.tar.gz
.
File metadata
- Download URL: pydantic_spark-1.0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.12.0 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddfa8ad977de941e240310b5c0a2aefbc33f92dd303f5a6d06e9bb4ba90959e8 |
|
MD5 | 7be185afb8f29035993159d1a131b92d |
|
BLAKE2b-256 | a3466c0e67c3b6fdd3cb9c11b2bca9644cf57ddfa1f1bd20a1687b7a9cf8e8f3 |
Provenance
File details
Details for the file pydantic_spark-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: pydantic_spark-1.0.1-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51900d2e273b1be232fc1e0fb7c2d259f179daa1edd25754e07e4f7653def32e |
|
MD5 | 3a14f8131f30cc4ee42b47ee7c48e843 |
|
BLAKE2b-256 | a223b993e9b3901a9b2e143bf8c5da4419a671dfa921cd07202accf351be2bea |