GithubHelp home page GithubHelp logo

pydantic-gen's Introduction

Pydantic Schemas Generator

https://img.shields.io/pypi/pyversions/pydantic-gen https://img.shields.io/pypi/v/pydantic-gen https://img.shields.io/pypi/dw/pydantic-gen https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Flicht1stein%2Fpydantic-gen%2Fbadge&style=flat https://readthedocs.org/projects/pydantic-gen/badge/?version=latest https://img.shields.io/badge/Support-With_coffee!-Green

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.

Take a look at the official example from the Pydantic 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

Using pip:

pip install pydantic-gen

Using poetry:

poetry add 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 file in the example_output.py

  1. Or directly import the generated classed directly without saving:
generated.to_sys(module_name='generated_schemas')

After running generated.to_sys(module_name='generated_schemas' your generated code will be available for import:

import generated_schemas as gs

schema = gs.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 and uses pytest. To run:

pytest

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:

pydantic-gen's People

Contributors

dana1313 avatar licht1stein avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pydantic-gen's Issues

Import or runtime errors after fresh `pip install`.

When I try to install pydantic-gen via

pip install pydantic-gen

and then run

from pydantic_gen import SchemaGen

I get the error

ImportError: cannot import name 'soft_unicode' from 'markupsafe' (/tmp/venv/lib/python3.8/site-packages/markupsafe/__init__.py)

I can fix this by pinning pip install markupsafe==2.0.1, and after that I can import SchemaGen, but then I get the runtime error

BoxKeyError: "'Box' object has no attribute 'schemas'"

There is probably some way to downgrade box accordingly as well, but I did not go down that road.

Is there any known set of pinned dependencies that I can currently use to install pydantic-gen or even better do you see a way to update the project such that it cleanly installs with pip?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.