{"id":17903636,"url":"https://github.com/pirogoeth/diecast","last_synced_at":"2025-04-03T05:44:46.708Z","repository":{"id":69011463,"uuid":"120559085","full_name":"pirogoeth/diecast","owner":"pirogoeth","description":"Dependency Injection System for Python 3","archived":false,"fork":false,"pushed_at":"2019-01-12T17:24:43.000Z","size":30,"stargazers_count":0,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-08T19:44:17.349Z","etag":null,"topics":["dependency-injection","framework","injectable-components","injector","python","python3"],"latest_commit_sha":null,"homepage":"","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/pirogoeth.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}},"created_at":"2018-02-07T03:42:56.000Z","updated_at":"2019-01-12T17:24:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"fb0a71ae-b1e6-4b50-9d6f-7fa3e72c7c84","html_url":"https://github.com/pirogoeth/diecast","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pirogoeth%2Fdiecast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pirogoeth%2Fdiecast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pirogoeth%2Fdiecast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pirogoeth%2Fdiecast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pirogoeth","download_url":"https://codeload.github.com/pirogoeth/diecast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246944385,"owners_count":20858773,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","framework","injectable-components","injector","python","python3"],"created_at":"2024-10-28T16:41:03.970Z","updated_at":"2025-04-03T05:44:46.686Z","avatar_url":"https://github.com/pirogoeth.png","language":"Python","readme":"# diecast\n\nDiecast is a dependency injection framework for Python.\n\nIt aims to be very simple to use, with an extremely simple API.\n\n## Usage\n\nTo start with diecast, install it with `pip`:\n\n    pip install -e git+https://github.com/pirogoeth/diecast.git#egg=diecast\n\n### Component Registry\n\nDiecast has a global registry that can be used. Otherwise, you can easily build a new registry:\n\n    from diecast.registry import ComponentRegistry\n    my_registry = ComponentRegistry()\n\n### Injectors\n\nInjectors must be created to inject dependencies into functions.  You can build an injector using `make_injector`:\n\n    from diecast.inject import make_injector\n\n    # If you're using the global registry\n    from diecast.registry import get_registry\n    my_registry = get_registry()\n\n    inject = make_injector(my_registry)\n\n### Injecting Components\n\nAfter creating an injector, you can use it to decorate any function you want your components to be injected to:\n\n    @inject\n    def my_function(item: MyComponent) -\u003e Any:\n        return do_something(item)\n\n    my_function()\n\nWhen calling an injected function, you can either omit the arguments you expect to be injected\nOR you can replace the injected arguments with `...` as a placeholder.\n\n    @inject\n    def my_function2(item: MyComponent, doer_cb: Callable[[Any], [Any]]) -\u003e Any:\n        return doer_cb(do_something(item))\n\n    my_function2(..., lambda item: mutate(item))\n    # is roughly equivalent to:\n    my_function2(lambda item: mutate(item))\n\n### Creating Components\n\nDiecast has a simple `Component` interface for building injectable components:\n\n    from diecast.component import Component\n\n    class MyComponent(Component):\n\n        @classmethod\n        def init(cls: Type[Component]) -\u003e 'MyComponent':\n            return MyComponent()\n\n### Registering Components\n\nAfter defining your component(s), add your component to the registry:\n\n    my_registry.add(\n        # `cls` is the type we will be injecting\n        cls=MyComponent,\n        # `init` is a callable which will create the instance of `cls`\n        # \u003cComponent\u003e.init is the default initializer and does not need to be explicitly set\n        init=MyComponent.init,\n        # `init` will *only* be called once and the instance will be stored\n        persist=True,\n    )\n\nOr, if you are using the global registry, you can use this shortcut:\n\n    from diecast.registry import register_component\n\n    register_component(\n        # `cls` is the type we will be injecting\n        cls=MyComponent,\n        # `init` is a callable which will create the instance of `cls`\n        # \u003cComponent\u003e.init is the default initializer and does not need to be explicitly set\n        init=MyComponent.init,\n        # `init` will *only* be called once and the instance will be stored\n        persist=True,\n        # `registry` defaults to the global registry\n        # set it to your registry if you so desire\n        registry=my_registry,\n    )\n\n### Getting Component Instances\n\nYou can fetch component instances easily by subscripting the registry:\n\n    instance = my_registry[MyComponent]\n\nIf the component was registered with `persist=True`, the subscript will return the\npersisted component instance.\n\nIf there is not a persisted instance, the subscript will return a new instance\nof the component.\n\n## Examples\n\nSome more practical usage examples are include in the [examples directory](/examples/).\n\n## Contributing\n\nPull requests are welcomed and encouraged.  Feel free to ask questions via the issue tracker or anywhere else (such as [Gitter](https://gitter.im/pirogoeth)).\n\nIf you're submitting a PR, please install [`pre-commit`](https://github.com/pre-commit/pre-commit) and install the local git pre-commit hook to run style checks.\n\nAny contributions will be greatly appreciated \u003c3.\n\n## License\n\nLicensed under MIT. See [LICENSE](/LICENSE) for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpirogoeth%2Fdiecast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpirogoeth%2Fdiecast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpirogoeth%2Fdiecast/lists"}