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
Release files for pydantic-spark 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydantic_spark-1.0.1.tar.gz | 4.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_spark-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.2 kB
Release files / pydantic_spark-1.0.1.tar.gz
| Download URL | pydantic_spark-1.0.1.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ddfa8ad977de941e240310b5c0a2aefbc33f92dd303f5a6d06e9bb4ba90959e8
|
|
BLAKE2b-256 checksum How to use checksums |
a3466c0e67c3b6fdd3cb9c11b2bca9644cf57ddfa1f1bd20a1687b7a9cf8e8f3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.12.0 Darwin/22.6.0
|
Release files / pydantic_spark-1.0.1-py3-none-any.whl
| Download URL | pydantic_spark-1.0.1-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
51900d2e273b1be232fc1e0fb7c2d259f179daa1edd25754e07e4f7653def32e
|
|
BLAKE2b-256 checksum How to use checksums |
a223b993e9b3901a9b2e143bf8c5da4419a671dfa921cd07202accf351be2bea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.12.0 Darwin/22.6.0
|