{"id":19201337,"url":"https://github.com/caioaao/compynent","last_synced_at":"2025-07-29T01:35:49.002Z","repository":{"id":62564248,"uuid":"301883479","full_name":"caioaao/compynent","owner":"caioaao","description":"A dependency Injection and context management microlibrary","archived":false,"fork":false,"pushed_at":"2020-11-16T01:26:09.000Z","size":38,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-13T14:29:49.975Z","etag":null,"topics":["dependency-injection","lifecycle","microframework","python","state-management"],"latest_commit_sha":null,"homepage":"https://compynent.readthedocs.io/","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/caioaao.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":"CONTRIBUTING.rst","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-07T00:00:34.000Z","updated_at":"2021-08-16T19:29:18.000Z","dependencies_parsed_at":"2022-11-03T16:45:38.060Z","dependency_job_id":null,"html_url":"https://github.com/caioaao/compynent","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/caioaao/compynent","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caioaao%2Fcompynent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caioaao%2Fcompynent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caioaao%2Fcompynent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caioaao%2Fcompynent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/caioaao","download_url":"https://codeload.github.com/caioaao/compynent/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caioaao%2Fcompynent/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267616717,"owners_count":24116162,"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","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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","lifecycle","microframework","python","state-management"],"created_at":"2024-11-09T12:38:02.012Z","updated_at":"2025-07-29T01:35:48.953Z","avatar_url":"https://github.com/caioaao.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"=========\ncompynent\n=========\n\n\n.. image:: https://img.shields.io/pypi/v/compynent.svg\n        :target: https://pypi.python.org/pypi/compynent\n\n.. image:: https://img.shields.io/travis/caioaao/compynent.svg\n        :target: https://travis-ci.com/caioaao/compynent\n\n.. image:: https://readthedocs.org/projects/compynent/badge/?version=latest\n        :target: https://compynent.readthedocs.io/en/latest/?badge=latest\n        :alt: Documentation Status\n\n\nAn easy way to define a dependency graph between parts of your application and manage their life-cycle using the built-in context managers.\n\nCompynent is a micro library for dependency injection and managing state in an application.\n\n\n* Free software: MIT license\n* Documentation: https://compynent.readthedocs.io.\n\n\nInstall\n-------\n\nRecommended install is using pip:\n\n.. code-block:: console\n\n    $ pip install compynent\n\n\nQuick start\n-----------\n\n\"Components\" in your application are just context managers. All of them receive their dependencies in their init, and they will be initialized by the time the context is \"entered\".\n\nFor example, let's define two components `A`, and `B`, and `A` depends on `B`.\n\n.. code-block:: python\n\n   from contextlib import contextmanager\n   @contextmanager\n   def component_a(b):\n       print('entered A')\n       yield b\n       print('exiting A')\n   @contextmanager\n   def component_b():\n       print('entered B')\n       yield 35\n       print('exiting B')\n\n\nNow, let's define our system config and use it inside a context:\n\n.. code-block:: python\n\n   from compynent import System\n   system = System({'a': (component_a, ['b']),\n                    'b': (component_b, [])})\n   with system.start() as ctx:\n       print('A: %d' % ctx['a'])\n       print('B: %d' % ctx['b'])\n\nWhen we run the code block above, we get::\n\n   entered B\n   entered A\n   A: 35\n   B: 35\n   exiting A\n   exiting B\n\n\nBut why?\n--------\n\nDependency Injection is a good idea for many reasons. I suggest reading about it and learning for yourself (trust me, it's worth it).\n\nNow, why not use one of the many dependency injection libraries out there? My second suggestion is to start a Python REPL and run ``import this`` (or read about the `zen of python`_). This library is focused on the second and third lines:\n\n- Explicit is better than implicit\n- Simple is better than complex\n\nAll the libraries fall short in one or two of these rules. Some will depend on incantations to automatically find where to inject code, while others provide a complex API for defining all sorts of dependencies, bindings, providers, containers, etc. This library doesn't do that.  It provides a way to define that component A depends on component B and **that's it**. Everything else is up to the user. No singletons, factories, etc., for the sole reason that it's not needed in Python.\n\nAnother reason is state management. One important feature of dependency injection is being able to share state between different parts of your code while maintaining the parts decoupled. This means the dependency graph is important to define the order in which the application parts must be initialized. But, if you want an application that is actually easily testable, you also want the cleanup to be done properly in the reverse order of initialization. Most libraries out there assume your program will run forever or that you will handle releasing resources manually after it's done. Well, Python solves that pretty well already with `context managers`_, so why not take advantage of that?\n\nDependency Injection Libraries\n------------------------------\n\nPython community came up with other great libraries. If this one is not for you, make sure to check the others out:\n\n- http://python-dependency-injector.ets-labs.org/\n- https://github.com/google/pinject\n- https://github.com/ivankorobkov/python-inject\n\nCredits\n-------\n\nThis package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.\n\nIt was inspired by the Component_ library for Clojure, written by `Stuart Sierra`_. I hope this project carries some of that simplicity with it.\n\n.. _Cookiecutter: https://github.com/audreyr/cookiecutter\n.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage\n.. _Component: https://github.com/stuartsierra/component\n.. _`Stuart Sierra`: https://stuartsierra.com/\n.. _`context managers`: https://docs.python.org/3/library/stdtypes.html#typecontextmanager\n.. _`zen of python`: https://www.python.org/dev/peps/pep-0020/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaioaao%2Fcompynent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcaioaao%2Fcompynent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaioaao%2Fcompynent/lists"}