Package: zunzun
Zunzun FrameworkZunzun is a Python framework that uses other libraries to implement its features. Ones of them are:injectorIt's used to Dependency Injection.clickFor creating commands line interface.blinkerProvides a fast dispatching system.SQLAlchemyThe Python SQL Toolkit and Object Relational Mapper.Create an applicationClone the example application fromhttps://github.com/aprezcuba24/zunzun_appCreate .env file base on .env.exampleConfigure database configurationYou can use vscode to open the application in a container.Star the application using this command.python manage.py core runserverOpen the browser in this urlhttp://localhost:8001/ControllerWe can create two types of controller, a function controller, or a class controller.Function controllerfrommainimportrouter@router.post("/")defregister():return"Register"Class controllerTo create a class controller we can use the following commandpython 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 fileapp/controllers/role.pyfromzunzunimportResponsefrommainimportrouterclassRoleController:@router.get('/role')defindex(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.frommainimportrouterfromapp.servicesimportPaypalService@router.post("/")defregister(paypal:PaypalService):paypal.call_a_method()return"Register"In a controller class, we can inject dependencies in the constructor or in any function.fromzunzunimportResponsefrommainimportrouterfromapp.servicesimportPaypalService,SomeServiceclassRoleController:def__init__(self,some_service:SomeService):self.some_service=some_service@router.get('/role')defindex(self,paypal:PaypalService):return"RoleController Index"CommandsCommands allow us to implement command line features. To do that we can use the following command.python manager.py maker command roleWhere "role" will be the name of the command. This command will create the following fileapp/commands/role.py.importclickfrominjectorimportsingleton,injectfromzunzunimportCommand@singletonclassroleCommand(Command):@injectdef__init__(self):super().__init__("role")self.add_option("--some-option")self.add_argument("some-argument")defhandle(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 manager.py app role "An argument value" --some-option="An option value"ListenerThe listener allows us to implement the Event-Dispatcher pattern. To create a new listener with its signal we can use the following command.python manager.py maker listener Role RoleWhere 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 fileapp/signals/role.pyListener fileapp/listeners/role.pyThe signal file will have this code.fromzunzunimportSignalfrominjectorimportsingleton@singletonclassRoleSignal(Signal):passThe listener file will have this code.frominjectorimportsingleton@singletonclassRoleListener:def__call__(self,sender,**kwargs):passServicesWe 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 manager.py maker service PaypalThe command will create the fileapp/services/paypal.pywith the following code.frominjectorimportsingleton,inject@singletonclassPaypalService:@injectdef__init__(self):passORMZunzun usesSQLAlchemyto 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 manager.py orm model_create RoleThe model will befromzunzunimportormfromsqlalchemyimportColumn,IntegerclassRole(orm.BaseModel):__tablename__="Role"id=Column(Integer,primary_key=True)The repository will befrominjectorimportsingletonfromzunzunimportormfromapp.modelimportRole@singletonclassRoleRepository(orm.BaseRepository):defnew(self,**kwargs):returnRole(**kwargs)Dependency injectionThe framework uses this pattern to manage dependencies. To know how you can use see the documentation oninject