{"id":19479420,"url":"https://github.com/frostime/simple-inject-py","last_synced_at":"2026-02-13T15:06:15.557Z","repository":{"id":256884241,"uuid":"856934461","full_name":"frostime/simple-inject-py","owner":"frostime","description":"A lightweight dependency injection library for Python","archived":false,"fork":false,"pushed_at":"2025-08-12T11:28:40.000Z","size":53,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-01T01:03:50.535Z","etag":null,"topics":["dependency-injection","inversion-of-control","pip","provide-inject","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/py-simple-inject/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frostime.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-09-13T13:41:12.000Z","updated_at":"2025-08-12T11:28:40.000Z","dependencies_parsed_at":"2025-08-12T13:11:29.673Z","dependency_job_id":"1aa0e598-d247-423c-8579-dc53a450367a","html_url":"https://github.com/frostime/simple-inject-py","commit_stats":null,"previous_names":["frostime/simple-inject-py"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/frostime/simple-inject-py","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fsimple-inject-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fsimple-inject-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fsimple-inject-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fsimple-inject-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frostime","download_url":"https://codeload.github.com/frostime/simple-inject-py/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostime%2Fsimple-inject-py/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29411138,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-13T06:24:03.484Z","status":"ssl_error","status_checked_at":"2026-02-13T06:23:12.830Z","response_time":78,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","inversion-of-control","pip","provide-inject","python"],"created_at":"2024-11-10T19:54:25.392Z","updated_at":"2026-02-13T15:06:15.541Z","avatar_url":"https://github.com/frostime.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple Inject\n\n[中文 README](README_zh.md)\n\nSimple Inject is a lightweight Python dependency injection library. It provides an easy-to-use interface for managing dependencies across different namespaces and scopes.\n\n## Features\n\n- Simple and intuitive dependency injection API\n- Supports multiple namespaces to isolate dependencies\n- Implements scoped dependencies using context managers or decorators\n- Supports nested scopes for fine-grained control\n- Supports automatic dependency injection through parameters\n- Easy integration with existing projects\n- Minimal overhead and dependencies\n\n## Installation\n\nYou can install Simple Inject using pip:\n\n```\npip install py-simple-inject\n```\n\n## Quick Start\n\n### Basic Usage\n\nHere is a simple example demonstrating basic dependency injection and scope management:\n\n```python\nfrom simple_inject import provide, inject, create_scope\n\n# Provide a dependency\nprovide('config', {'debug': True})\n\n# Inject a dependency\nconfig = inject('config')\nprint(config['debug'])  # Output: True\n```\n\n### Using Namespaces\n\n```py\nfrom simple_inject import provide, inject, create_scope\n\nprovide('key', 'value1', namespace='ns1')\nprovide('key', 'value2', namespace='ns2')\n\nprint(inject('key', namespace='ns1'))  # Output: value1\nprint(inject('key', namespace='ns2'))  # Output: value2\n```\n\n### Using Scopes\n\n```py\nprovide('config', {'debug': True})\n\n# Use scopes to manage dependencies\nwith create_scope():\n    provide('config', {'debug': False})\n    config = inject('config')\n    print(config['debug'])  # Output: False\n\n# Outside the scope, the original value is preserved\nconfig = inject('config')\nprint(config['debug'])  # Output: True\n```\n\nScopes can also be used with the `scoped` decorator:\n\n```py\n@scoped()\ndef scoped_function():\n    provide('key', 'scoped_value')\n    return inject('key')\n\nprovide('key', 'outer_value')\nprint(inject('key'))  # Output: outer_value\nprint(scoped_function())  # Output: scoped_value\nprint(inject('key'))  # Output: outer_value\n```\n\n### Nested Scopes\n\nScoped scopes can be nested, and dependencies in inner scopes will override those in outer scopes.\n\n```python\nprovide('key', 'outer')\n\nwith create_scope():\n    provide('key', 'inner')\n    print(inject('key'))  # Output: inner\n\n    with create_scope() as inner_scope:\n        provide('key', 'innermost')\n        print(inject('key'))  # Output: innermost\n\n    print(inject('key'))  # Output: inner\n\nprint(inject('key'))  # Output: outer\n```\n\n### Accessing Scoped State\n\nYou can access the state of dependencies within a scope after it exits using the `scoped_state` method:\n\n```python\nprovide('config', {'debug': True})\n\nwith create_scope() as scope:\n    provide('config', {'debug': False})\n    provide('new_setting', 'scoped_value')\n\n# Get only the dependencies that were added or modified in the scope\nprovided_state = scope.scoped_state('Provided')  # Default policy\nprint(provided_state)\n# Output: {'default': {'config': {'debug': False}, 'new_setting': 'scoped_value'}}\n\n# Get the complete state within the scope\nall_state = scope.scoped_state('All')\nprint(all_state)\n# Output: {'default': {'config': {'debug': False}, 'new_setting': 'scoped_value'}}\n\n# Get state for a specific namespace\nwith create_scope() as scope:\n    provide('key1', 'value1', namespace='ns1')\n    provide('key2', 'value2', namespace='ns2')\n\nns1_state = scope.scoped_state('Provided', 'ns1')\nprint(ns1_state)  # Output: {'key1': 'value1'}\n```\n\nThe `scoped_state` method supports two policies:\n- `'Provided'` (default): Returns only dependencies that were added or modified within the scope\n- `'All'`: Returns the complete state within the scope\n\n### Automatic Injection via Function Parameters\n\nSimple Inject also supports automatic injection via function parameters. The following example demonstrates how to use this advanced feature:\n\n```python\nfrom simple_inject import provide, inject, create_scope, auto_inject, Inject\n\nclass Engine:\n    def start(self):\n        print(\"Engine started\")\n\n# Provide a dependency\nprovide('engine', Engine())\n\n# Manually inject a dependency\nengine = inject('engine')\nengine.start()  # Output: Engine started\n\n# Use automatic injection\n@auto_inject()\ndef drive(car: str, engine: Engine = Inject('engine')):\n    print(f\"Driving {car}\")\n    engine.start()\n\ndrive(\"Tesla\")  # Output: Driving Tesla and Engine started\n\n# Use scopes to manage dependencies\nwith create_scope():\n    provide('engine', Engine())  # Provide a new Engine instance\n    drive(\"BMW\")  # Output: Driving BMW and Engine started\n\n# Outside the scope, the original value is preserved\ndrive(\"Toyota\")  # Output: Driving Toyota and Engine started\n```\n\n## API Reference\n\n### `provide(key: str, value: Any, namespace: str = 'default')`\n\nProvides a dependency in the current context.\n\n### `inject(key: str, namespace: str = 'default') -\u003e Any`\n\nInjects a dependency from the current context.\n\n### `create_scope()`\n\nCreates a new dependency scope. Used with the `with` statement.\n\n### `scoped()`\n\nDecorator to create a new dependency scope for a function.\n\n### `auto_inject()`\n\nDecorator to automatically inject parameters marked with `Inject`.\n\n### `Inject(key: str, namespace: str = 'default')`\n\nClass to mark a parameter for automatic injection.\n\n### `purge(namespace: Optional[str] = None)`\n\nClears dependencies, either for a specific namespace or for all namespaces.\n\n## Contributing\n\nContributions are welcome! Feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrostime%2Fsimple-inject-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrostime%2Fsimple-inject-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrostime%2Fsimple-inject-py/lists"}