Join Login Create a Request

What is Pydantic

|
What is Pydantic

Python sucks at complex nested data structures. One way of dealing with that is through dictionaries. However this also has problems, you can't know the data expected in the passed dictionary and prevent typos because any string is a valid key. Imagine you're pulling a list of articles from a WordPress API, when manipulating such data it's easy to misspell some JSON object value so you need to create a model that you can use to parse the JSON value and then use that model during manipulation, you get the model right, then everything will fit into place. To achieve this in python you use a parsing and validation library pydantic.

Pydantic is a library used for data parsing and validation using python type annotations. It enforces type hints at runtime and provides user-friendly errors when data has errors.

Requirements

Install pydantic via pip or pipenv

pip install pydantic
# or
pipenv install pydantic

You're taking your data from an API endpoint, I'll assume you're pulling articles from a secondary website or API.

[
  {
    "id": 1,
    "title": "The United States Will Closely Follow Zimbabwe’s March 26 By-elections",
    "published": "Feb. 17, 2022, 9:41 a.m.",
    "category": "Politics",
    "author": "John Doe",
    "tags": ["US", "Elections", "Zimbabwe"]
  },
  {
    "id": 2,
    "title": "List Of Petroleum Licensees As Of 31 December 2021 - ZERA",
    "published": "Feb. 17, 2022, 9:15 a.m.",
    "category": "Business",
    "author": "John Doe",
    "tags": ["Petroleum", "Licenses"]
  },
  {
    "id": 3,
    "title": "RBZ Moves To Control Money Supply To Stabilise The Exchange Rate",
    "published": "Feb. 17, 2022, 8:55 a.m.	",
    "category": "Business",
    "author": "John Doe",
    "tags": ["RBZ", "Exchange Rate"]
  }
]

Create a pydantic model

# models.py
import datetime
from decimal import Decimal
from typing import List, NewType, Optional
from pydantic import BaseModel, Field

ArticleId = NewType("ArticleId", int)
class Article(BaseModel):
  id: ArticleId
  title: str
  category: str
  author: str
  published: datetime.date
  tags: Optional[List[str]] = None

Use the pydantic model for input parsing

Pydantic has a special method parse_obj_as used to parse JSON into a model. So we start by calling an API call which in turn returns a list of articles, then we map that list of articles into our Article pydantic model.

Now you can manipulate the article object with proper error catching and hinting capabilities introduced by pydantic.


Tags.




Recent Posts

How to use robot.txt on a Django Website
How to use robot.txt on a Django Website
How to add a robots.txt file on a Django website so that you..
Read More
Top sites for remote jobs
Top sites for remote jobs
Where to find remote jobs for software developers in 2022...
Read More
Project ideas for a software developer portfolio
Project ideas for a software developer portfolio
Projects which can strengthen your portfolio as a software d..
Read More
My case for WordPress
My case for WordPress
WordPress strengths and weaknesses. When you should use Word..
Read More