DEV Community

Richard Devers
Richard Devers

Posted on • Updated on

Python libraries for quality programming

pydantic

Pydantic is a library owned by Samuel Colvin which is an incredible developper. Go read his code, you'll learn.

From the doc:

Data validation and settings management using python type annotations.
pydantic enforces type hints at runtime, and provides user friendly errors when data is invalid.
Define how data should be in pure, canonical python; validate it with pydantic.

Basically, Pydantic class are supercharged dataclass.

Data validation, on-fly complex custom validation and/or transformation, de/serialization with optional key, attribute alias , secret management and so more can be accomplished with it. Cherry on the cake, it's mypy and Pylance compatible.

Just have a look at their users (spoiler: The NSA, AWS, Microsoft etc..)

Your application will be way more robust, you'll deliver way faster and you'll have a significant gain in serenity.

It's an absolute must use, a master class.

fastapi

fastapi is another master class, this time by Sebastián Ramírez.

From the doc:

- Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
- Fast to code: Increase the speed to develop features by about 200% to 300%. *
- Fewer bugs: Reduce about 40% of human (developer) induced errors. *
- Intuitive: Great editor support. Completion everywhere. Less time debugging.
- Easy: Designed to be easy to use and learn. Less time reading docs.
- Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
- Robust: Get production-ready code. With automatic interactive documentation.
- Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.

Everything is said. Fully async, based on Pydantic (of course!), easy to test, fast and so on... By far the best REST API framework i have worked with.

typer

Another one by Sebastián Ramírez, typer

From the doc:

- Intuitive to write: Great editor support. Completion everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.
- Easy to use: It's easy to use for the final users. Automatic help, and automatic completion for all shells.
- Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
- Start simple: The simplest example adds only 2 lines of code to your app: 1 import, 1 function call.
- Grow large: Grow in complexity as much as you want, create arbitrarily complex trees of commands and groups of subcommands, with options and arguments.

Built on Pydantic, typer allow you to create simple CLI within minutes and complex one in a few hours.

Easy to test, easy to build, another great one to simplify your life.

httpx

In my opinion, httpx is the best http client so far. sync/async support and everything you'll ever need. Easy to use. A great One.

httpx tests are easy to do using respx. You can mock it very simply, side_effect management etc..

Bonus: datamodel-code-generator

Ok so pydantic help you create powerfull class models?

What if you don't even have to write them?

That's what datamodel-code-generator propose.

You put a swagger file in input, you get a python module with every swagger models as pydantic class in it as output! Magic!

Top comments (0)