Injector - Python依赖注入框架技术文档
2024-12-20 22:37:09作者:韦蓉瑛
1. 安装指南
安装方式
您可以通过PyPI(Python软件包索引)使用pip来安装Injector。请运行以下命令:
pip install injector
确保您的Python环境版本为CPython 3.8+ 或 PyPy 3实现Python 3.8+。
2. 项目的使用说明
Injector 是一个Python依赖注入框架,旨在通过自动和传递性地提供依赖来简化大型应用程序的编码工作。它鼓励通过模块使用良好分隔的代码。
快速示例
以下是使用Injector的一个快速示例:
from injector import Injector, inject
class Inner:
def __init__(self):
self.forty_two = 42
class Outer:
@inject
def __init__(self, inner: Inner):
self.inner = inner
injector = Injector()
outer = injector.get(Outer)
print(outer.inner.forty_two) # 输出 42
完整示例
以下是一个完整的示例,展示Injector的使用方法:
from injector import Module, provider, Injector, inject, singleton
import sqlite3
class RequestHandler:
@inject
def __init__(self, db: sqlite3.Connection):
self._db = db
def get(self):
cursor = self._db.cursor()
cursor.execute('SELECT key, value FROM data ORDER by key')
return cursor.fetchall()
class Configuration:
def __init__(self, connection_string):
self.connection_string = connection_string
def configure_for_testing(binder):
configuration = Configuration(':memory:')
binder.bind(Configuration, to=configuration, scope=singleton)
class DatabaseModule(Module):
@singleton
@provider
def provide_sqlite_connection(self, configuration: Configuration) -> sqlite3.Connection:
conn = sqlite3.connect(configuration.connection_string)
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS data (key PRIMARY KEY, value)')
cursor.execute('INSERT OR REPLACE INTO data VALUES ("hello", "world")')
return conn
injector = Injector([configure_for_testing, DatabaseModule()])
handler = injector.get(RequestHandler)
print(tuple(map(str, handler.get()[0]))) # 输出 ('hello', 'world')
# 验证单例
print(injector.get(Configuration) is injector.get(Configuration)) # 输出 True
print(injector.get(sqlite3.Connection) is injector.get(sqlite3.Connection)) # 输出 True
3. 项目API使用文档
@inject:用于标记类的构造函数,自动注入所需的依赖。Injector:用于创建和配置依赖注入环境。Injector.get:从Injector获取特定类型的实例。Injector.create_object:创建一个对象,自动注入其依赖。Module:定义一组绑定和提供者。provider:用于定义提供者函数,该函数负责生成依赖对象。singleton:用于指定绑定对象的生命周期为单例。
4. 项目安装方式
请参考上文“安装指南”中的步骤进行安装。
通过上述技术文档,您应该能够了解如何安装和使用Injector依赖注入框架。在大型项目中使用它可以帮助您更好地组织代码,提高代码的可维护性和扩展性。
登录后查看全文
热门项目推荐
相关项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin08
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
537
3.75 K
暂无简介
Dart
773
191
Ascend Extension for PyTorch
Python
343
406
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.34 K
755
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.07 K
97
React Native鸿蒙化仓库
JavaScript
303
355
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
337
180
AscendNPU-IR
C++
86
141
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
986
248