A service locator for python.
luogo is a basic service locator for Python3. I’ve been working a lot with dependency injection and service location in Java and thought it would be interesting to build something from scratch in my current language of choice.
Why the name luogo? I’m not brimming with funny/clever ideas at the moment; however, I’ve been watching this Italian crime drama which has inspired me to work on my Italian…
You might have guessed by now that “luogo” is Italian for location or place.
pip install luogo
You can start by defining your services in a service file (must feature “service” in the name). Services are denoted with the Service decorator.
# world_servce.py from luogo import Service @Service class HelloWorldService: def greeting(self): return "Hello World!"
Your service client or implementation can then be in any file or module.
# client.py from luogo import Locator, Implementation @Implementation class HelloWorldServiceClient: def greeting(self): service = Locator.get_service("HelloWorldService") return service.greeting()
print(HelloWorldServiceClient().greeting()) # Hello World!
You can also mark functions as services:
# world_service.py from luogo import Service @Service def hello_world_service: return "Hello World!"
# client.py from luogo import Locator, Implementation @Implementation def hello_world_service_client: return Locator.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.
Works for my purposes, hopefully it can be of use to someone else!