steinitzu / giveme Goto Github PK
View Code? Open in Web Editor NEWA simple python dependency injector
License: MIT License
A simple python dependency injector
License: MIT License
If you try to as in example
import asyncio
from giveme import Injector
injector = Injector()
class SomeClass:
async def start(self):
...
async def ping(self):
...
@injector.register(name="some_cls")
async def some_class_fabric() -> SomeClass:
some_class = SomeClass()
await some_class.start()
return some_class
@injector.inject
async def async_action(some_cls):
await some_cls.ping()
asyncio.run(async_action())
you will get
Traceback (most recent call last):
File "/Users/isaeev-vs/PycharmProjects/giveme/main.py", line 28, in <module>
asyncio.run(async_action())
File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/Users/isaeev-vs/PycharmProjects/giveme/giveme/injector.py", line 217, in awrapper
return await function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/isaeev-vs/PycharmProjects/giveme/main.py", line 25, in async_action
await some_cls.ping()
^^^^^^^^^^^^^
AttributeError: 'coroutine' object has no attribute 'ping'
sys:1: RuntimeWarning: coroutine 'some_class_fabric' was never awaited
Because here it is corutine object.
It was good if:
Currently a single Manager
instance is created on import.
The manager
acts a global registry for dependencies.
This means there's no easy way to create more than one dependency registry per python process, making giveme unsuitable for use in frameworks and projects which need clear separation between dependency graphs.
To solve this a new class should be introduced, (calling it Injector
for now) and the decorators @register
and @inject
become instance methods of it.
Dependencies will then be bound to a particular instance of Injector
rather than all dependencies being registered globally
example of new usage:
from giveme import Injector
my_injector = Injector()
@my_injector.register
def dependency():
...
@my_injector.inject
def do_stuff(dependency):
...
This is a breaking change so should be introduced with a middle-step with clear deprecation warnings and migration path from the old decorators.
@steinitzu we need PYPI_TOKEN
in secrets part of project. So we can release new versions from github actions.
The project cannot be integrated in modern projects with static type checkers like mypy
- will you support typing?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.