Skip to main content

Generate code for models, views, and urls based on Python type annotations.

Project description

Generate code for models, views, and urls based on Python type annotations. Powered by pydantic. Influenced by SQLModel.

reactant aims to be non-intrusive and disposable, but also to give usable and sensible code defaults.

reactant does not enforce a particular application structure. Instead, it adheres to the default/minimal/common structure of the supported frameworks, and it is up to the developer to make use of the generated code to fit it to their application. Contibutions are warmly welcomed if you believe a particular structure is widely used and can benefit from code generation.

Supported Frameworks

reactant currently generates code for the following:

Django REST (in Django's default project structure i.e. by apps)

  • models
  • views (class-based API views, filename=views_class.py)
  • views (function-based API views, filename=views_function.py)
  • serializers
  • urls (from class-based API views, filename=urls_class.py)
  • urls (from function-based API views, filename=urls_function.py)

SQLAlchemy

  • models in Declarative Mapping

Peewee

  • models

Installation

$ pip install reactant

Get Started

Create reactant models by inheriting from Reactant , and from choosing an ORM: DjangoORM, SQLAlchemyORM, PeeweeORM. The example below uses DjangoORM. Your choice of ORM will determine what code and files will be generated.

# generate.py

from reactant import Reactant, DjangoORM, Field, generate


class RocketEngine(Reactant, DjangoORM):
    name: str = Field(max_length=32, title="engine_name")
    manufacturer: str = Field(max_length=64)
    power_cycle: Optional[str] = Field("gas-generator", blank=True, max_length=32)
    thrust_weight_ratio: Optional[int] = None


class LaunchVehicle(Reactant, DjangoORM):
    name: str = Field(max_length=32)
    country: str = Field(blank=True, max_length=32)
    status: str
    total_launches: Optional[int]
    engine: str = Field(foreign_key="RocketEngine")

# Don't forget this block.
if __name__ == "__main__":
    generate()

Don't forget generate(). Run the code.

$ reactant generate.py

Running generate.py
Found 2 Django reactants.
Django models.py finished rendering.
Django views_class.py finished rendering.
Django serializers.py finished rendering.
Django urls_class.py finished rendering.
Success! Please check "reactant_products" directory.

BOOM! With just the above code, the models, views, serializers, and urls (the products, for Django atleast) are generated.

Development

The project uses Poetry to package and manage dependencies.

(venv)$ poetry install

Run tests.

pytest

License

MIT License. For more information and legal terms, see the LICENSE file.

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

reactant-0.1.0.tar.gz (8.5 kB view hashes)

Uploaded Source

Built Distribution

reactant-0.1.0-py3-none-any.whl (9.3 kB view hashes)

Uploaded Python 3

Supported by

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