🔥Data validation in a Python script with the pydantic library
This lightweight library allows you to validate data and manage settings using Python annotations by applying type hints at runtime. The library shows errors if the data is invalid. It is enough to determine what the data should be in pure, canonical Python and check it with pydantic.
To be fair, pydantic is primarily a parsing library, not a validator. The library benefits from types and limits of the output model rather than the input. However, while model validation is not core pydantic, it can be used for this.
The main methods for defining objects in pydantic models are derived classes from the BaseModel base class. You can set up models as data types in strongly typed sizes. Untrusted data can be passed into models, and after parsing and checking pydantic parameters, that the fields of the resulting model instance will match the field types of the files in the models.
The library plays well with any IDE and is very fast: part of a large library is compiled with Cython. Pydantic performs complex data structure validation using recursive models, and allows you to define the characteristics of data types through the use of a decorator with parsing and validating input data.
It is noteworthy that this project with likely source code uses many companies and products, including FastAPI, Jupyter, Microsoft, AWS, Uber, etc. You can try it right now by importing it into your Python script, first find it yourself through the pip project manager, and then its base class is BaseModel:
pip install pydantic
from pydantic import BaseModel
https://pydantic-docs.helpmanual.io/
Post #516
563
- 👍 2