Skip to main content

Code generator for pydantic schemas

Project description

Pydantic Schemas Generator

PyPI - Python Version Code style: black PyPI PyPI - Downloads Build Status

What this package does

This is a code generation package that converts YML definitions to Pydantic models (either python code or python objects).

What is Pydantic

Pydantic is a python library for data validation and settings management using python type annotations.

Here's an official example from the docs

Why generate schemas?

Normally you just program the schemas within your program, but there are several use cases when code generation makes a lot of sense:

  • You're programming several apps that use the same schema (think an API server and client library for it)
  • You're programming in more than one programming language

Getting started

Installation

pip install pydantic-gen

Usage

First you need to create a YAML file with your desired class schema. See example.yml file.

from pydantic_gen import SchemaGen

generated = SchemaGen('example.yml')

The code is now generated and stored in generated.code attribute. There are two ways to use the code:

1. Save it to a file, and use the file in your program.

generated.to_file('example_output.py')

You can inspect the resulting example_output.py

2. Import the generated classed directly without saving

generated.to_sys(module_name='generated_schemas')

After running .to_sys() module 'generated_schemas' will be added to sys.modules and become importable like a normal module:

from generated_schemas import GeneratedSchema1

schema = GeneratedSchema1(id=1)

Usage pattern

Recommended usage pattern is creating the yaml files needed for your projects and storing them in a separate repository, to achieve maximum consistency across all projects.

YAML-file structure

schemas - list of all schemas described

name - name of the generated class

props - list of properties of the class using python type annotation. Fields: name - field name, type - field type, optional - bool, if True the type will be wrapped in Optional, default - default value for the field.

config - list of config settings from Model Config of pydantic.

Testing

Project is fully covered by tests.

Packaging notice

This project uses the excellent poetry for packaging. Please read about it and let's all start using pyproject.toml files as a standard. Read more:

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-gen-0.3.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_gen-0.3.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-gen-0.3.2.tar.gz.

File metadata

  • Download URL: pydantic-gen-0.3.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.7 Linux/5.3.0-1020-azure

File hashes

Hashes for pydantic-gen-0.3.2.tar.gz
Algorithm Hash digest
SHA256 1215ca93b536ffa175a434db70d4770895ed9e608b2ad3329cf18f670e44192a
MD5 04739b233ab2a83ee46e690dbb12f03a
BLAKE2b-256 839759bd7267bfd1111bd0ec994365ad5765b5c36b37e49e0da42293fcb63dcd

See more details on using hashes here.

File details

Details for the file pydantic_gen-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: pydantic_gen-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.7 Linux/5.3.0-1020-azure

File hashes

Hashes for pydantic_gen-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 db5caa20f6a587bbe53566a312ec50e15f1af08d81717fd341657317db17beb7
MD5 93a87c146c00598a58a2087d990eb04e
BLAKE2b-256 6d667e0f46fe94e8e838b4377be8709fcb4264c9753d61b1cce7f0c1bf6eacba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page