Skip to main content

A python framework to create api applications.

Project description

Zunzun Framework

Zunzun is a Python framework that uses other libraries to implement its features. Ones of them are:

  • injector It's used to Dependency Injection.
  • click For creating commands line interface.
  • blinker Provides a fast dispatching system.
  • SQLAlchemy The Python SQL Toolkit and Object Relational Mapper.

Create an application

Controller

We can create two types of controller, a function controller, or a class controller.

Function controller

from main import router


@router.post("/")
def register():
    return "Register"

Class controller

To create a class controller we can use the following command

python zunzun.py maker controller Role --route="/role"

Where "Role" will be the name of the controller and "/role" the path to access the controller.

The command will generate the file app/controllers/role.py

from zunzun import Response
from main import router

class RoleController:
    @router.get('/role')
    def index(self):
        return "RoleController Index"

In the class controller or in the function controller we can inject dependencies. For example, if we have a service named "PaypalService" we can inject it, with the following code.

from main import router
from app.services import PaypalService


@router.post("/")
def register(paypal: PaypalService):
    paypal.call_a_method()
    return "Register"

In a function class, we can inject dependencies in the constructor or in any function.

from zunzun import Response
from main import router
from app.services import PaypalService, SomeService

class RoleController:
    def __init__(self, some_service: SomeService):
        self.some_service = some_service

    @router.get('/role')
    def index(self, paypal: PaypalService):
        return "RoleController Index"

Commands

Commands allow us to implement command line features. To do that we can use the following command.

python zunzun.py maker command role

Where "role" will be the name of the command. This command will create the following file app/commands/role.py.

import click
from injector import singleton, inject
from zunzun import Command


@singleton
class roleCommand(Command):
    @inject
    def __init__(self):
        super().__init__("role")
        self.add_option("--some-option")
        self.add_argument("some-argument")

    def handle(self, some_option, some_argument):
        click.echo(
            f"roleCommand [some_argument: {some_argument}] [some_option: {some_option}]"
        )

To use the new command we can type the following in the console.

python zunzun.py app role "An argument value" --some-option="An option value"

Listener

The listener allows us to implement the Event-Dispatcher pattern. To create a new listener with its signal we can use the following command.

python zunzun.py maker listener Role Role

Where the first word "Role" will be the listener name and the second will be the signal name. The command will generate the following files:

  • Signal file app/signals/role.py
  • Listener file app/listeners/role.py

The signal file will have this code.

from zunzun import Signal
from injector import singleton


@singleton
class RoleSignal(Signal):
    pass

The listener file will have this code.

from injector import singleton


@singleton
class RoleListener:
    def __call__(self, sender, **kwargs):
        pass

Services

We can create classes to implement any logic that we need. For example to create a service to integrate Paypal we can use the following command.

python zunzun.py maker service Paypal

The command will create the file app/services/paypal.py with the following code.

from injector import singleton, inject


@singleton
class PaypalService:
    @inject
    def __init__(self):
        pass

ORM

Zunzun uses SQLAlchemy to implement the ORM features. The framework uses two type of classes.

  • The model represents a single row in the database.
  • The repository is a class to implement the queries to the database.

To create the model and its repository we can use the following command.

python zunzun.py orm model_create Role

The model will be

from zunzun import orm
from sqlalchemy import Column, Integer


class Role(orm.BaseModel):
    __tablename__ = "Role"
    id = Column(Integer, primary_key=True)

The repository will be

from injector import singleton
from zunzun import orm
from app.model import Role


@singleton
class RoleRepository(orm.BaseRepository):
    def new(self, **kwargs):
        return Role(**kwargs)

Dependency injection

The framework uses this pattern to manage dependencies. To know how you can use see the documentation on inject

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

zunzun-0.0.1.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

zunzun-0.0.1-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file zunzun-0.0.1.tar.gz.

File metadata

  • Download URL: zunzun-0.0.1.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

File hashes

Hashes for zunzun-0.0.1.tar.gz
Algorithm Hash digest
SHA256 811411369fc5bf5f9434ed7f30096318a0c6c152d919448fcfb319f4d83e717e
MD5 b8ba935da131a75025c3babde829c807
BLAKE2b-256 ad7b1f9b0f778c3084ec60edfe933d90ffa0a3dd158a820d20350620464deba6

See more details on using hashes here.

File details

Details for the file zunzun-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: zunzun-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

File hashes

Hashes for zunzun-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 792bb676d007dcf00e64a698418ded7fa88b3761112a8547a321729756abd03c
MD5 8164be6358c9e99f49b7ca81bf4488d9
BLAKE2b-256 da966845eff6ae7502a9b13eaf1ead81f22925cd0ac80a6b006b6c31b6e14fea

See more details on using hashes here.

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