python的服务定位器。
luogo的Python项目详细描述
loogo是python3的基本服务定位器。我在java中进行了大量的依赖注入和服务定位工作,并认为用我当前选择的语言从头开始构建一些东西会很有意思。
为什么叫luogo?我现在并没有充满有趣/聪明的想法;但是,我一直在看这部意大利犯罪剧,它激发了我学习意大利语的灵感……
你现在可能已经猜到“loogo”是意大利语的location或place。
安装
pip install luogo
用法
可以从在服务文件中定义服务开始(名称中必须包含“service”)。 服务用service装饰符表示。
# world_servce.pyfromluogoimportService@ServiceclassHelloWorldService:defgreeting(self):return"Hello World!"
然后,您的服务客户机或实现可以位于任何文件或模块中。
# client.pyfromluogoimportLocator,Implementation@ImplementationclassHelloWorldServiceClient:defgreeting(self):service=Locator.get_service("HelloWorldService")returnservice.greeting()
print(HelloWorldServiceClient().greeting())# Hello World!
您还可以将功能标记为服务:
# world_service.pyfromluogoimportService@Servicedefhello_world_service:return"Hello World!"
# client.pyfromluogoimportLocator,Implementation@Implementationdefhello_world_service_client:returnLocator.get_service("hello_world_service")
print(hello_world_service_client())# Hello World!
注释
- I finally made it a thing to start using python3 so this probably isn’t python2 compatible (might have used a python3 lib somewhere). I might work on it though.
- Services need to be a file that features the word service somewhere in the file name; however, implementations can be anywhere.
- Locator depends on a case insensitive string representation of the service name; however, it’s easy to get that if you don’t always feel like typing it out.
为我的目的工作,希望它能对其他人有用!