允许在相关模块中进行增量依赖注入的微框架。

incremental-module-loader的Python项目详细描述


增量模块加载器

为模块提出注入模式。每个模块都是一个简单的可调用模块,它将其他命名模块作为参数。它非常简单,只有不到40条代码语句,并且允许很大的灵活性,同时仍然删除了我们必须在每个项目上编写的频繁样板。在

这个模块由我们目前正在开发的SpinHub应用程序(https://www.spinhub.ca)使用。它是我们基于模块/服务的体系结构的主要构建块。在

用例1:模块化flask应用程序

您有一个auth服务、一个数据库服务和一个blueprint creator函数。如何将所有这些连接在一起,同时保持良好的关注点分离并允许注入模拟服务?让我们看看:

fromincremental_module_loaderimportIncrementalModuleLoaderfromflaskimportrequest,jsonifyclassRequestParsingModule(object):defget_authorization_header(self):returnrequest.headers.get('Authorization',None)classMyAuthModule(object):def__init__(self,config,request_parser):self.url=config['AUTH_URL']self.request_parser=request_parserdefget_user_id_from_token(self):header=self.request_parser.get_authorization_header()pass# Extract user id from header token...classMyDatabaseModule(object):def__init__(self,config,auth):self.db_uri=config['SQLALCHEMY_DATABASE_URI']self.auth=authdefget_user(self):user_id=self.auth.get_user_id_from_token()pass# Retrieve user data from database...# Note the name of the arguments here:defroute_app(app,config,database):@app.route('/userinfo')defuserinfo():# This triggers the auth verification from the header extracted from the request parseruser=database.get_user()returnjsonify(user.dump())# ...defcreate_app():app=# (...) Standard app creation with config loadingmodule_loader=IncrementalModuleLoader()# First init the modules with already created objects:module_loader.update(app=app,config=app.config)# Then load in order ("incrementally")# Make sure to specify a CALLABLE object (function or class)# The module_loader will call the parameter with the modules it is asking as parameters.module_loader.load(request_parser=RequestParsingModule)module_loader.load(auth=MyAuthModule)module_loader.load(database=MyDatabaseModule)# Can be loaded anonymously, returns nothing.module_loader.load(route_app)# There you have it !

创造者

由Tack Verification创建,这家公司致力于降低与硬件系统验证相关的运营成本。 https://www.tackv.ca

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java datepickerdialog主题灯在三星J7 Core中未正确显示   未调用已更改函数上的java工作管理器GetWorkInfo ByLiveId数据   java如何为XSL转换提供参数?   java IntelliJ中的fileTemplates文件夹是什么?   java IntelliJ是否自动导入ADOxx依赖项?   Java中具有自签名证书的PHP SoapClient   用于多个setOnClickListener的java Android If语句   java Storm群集配置   Java中的数据库三层连接   java应用程序关闭后如何保存整数的值?   中断超过阈值的线程的java最佳实践   JavaSpringBatch是获取CSV中总记录数的最简单方法?   安卓 null不能强制转换为非null类型的java。util。我的应用程序中的HashMap   java Eclipse扩展和声明性服务   java为什么在试图打印字符串中的最后一个数组索引时显示错误