{"id":16942502,"url":"https://github.com/defnull/contexter","last_synced_at":"2026-03-12T01:03:53.819Z","repository":{"id":13170798,"uuid":"15853856","full_name":"defnull/contexter","owner":"defnull","description":"Contexter: A better contextlib","archived":false,"fork":false,"pushed_at":"2022-08-30T13:12:21.000Z","size":13,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-28T06:52:36.259Z","etag":null,"topics":["python"],"latest_commit_sha":null,"homepage":null,"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/defnull.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-13T00:15:17.000Z","updated_at":"2024-07-23T02:17:02.000Z","dependencies_parsed_at":"2022-09-17T05:01:20.309Z","dependency_job_id":null,"html_url":"https://github.com/defnull/contexter","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/defnull/contexter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defnull%2Fcontexter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defnull%2Fcontexter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defnull%2Fcontexter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defnull%2Fcontexter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/defnull","download_url":"https://codeload.github.com/defnull/contexter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defnull%2Fcontexter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30410359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-12T00:40:14.898Z","status":"ssl_error","status_checked_at":"2026-03-12T00:40:08.439Z","response_time":84,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6: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":["python"],"created_at":"2024-10-13T21:12:07.433Z","updated_at":"2026-03-12T01:03:53.804Z","avatar_url":"https://github.com/defnull.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Contexter: A better contextlib\n===============================================================================\n\nContexter is a full replacement of the contextlib_ standard library module. It comes with more features, a nicer API and full support for Python 2.5 up to 3.x from a single source file.\n\nTo keep it short: Contexter allows you to nest and stack context managers in an easy and intuitive way.\n\nEnough talk, let's see an example:\n\n.. code:: python\n\n    ''' Copy the content of one file to another\n         and protect everything with a lock. '''\n\n    with Contexter(lock) as ctx:\n        in_file  = ctx \u003c\u003c open('a.txt')\n        out_file = ctx \u003c\u003c open('b.txt', 'w')\n        out_file.write(in_file.read())\n\nLook at that. It's beautiful, isn't it? Let me explain: You call ``Contexter()`` with any number of context managers as arguments and later attach additional managers with the neat ``value = ctx \u003c\u003c thing`` syntax. That's it. Only one level of indentation, no matter how many managers you need.\n\nJust for comparison:\n\n.. code:: python\n\n    # Python 2.5 and 2.6\n    with lock:\n        with open('a.txt') as in_file:\n            with open('b.txt', 'w') as out_file:\n                out_file.write(in_file.read())\n\n    # Starting with Python 2.7 and 3.2\n    with lock, open('a.txt') as in_file, open('b.txt', 'w') as out_file:\n        out_file.write(in_file.read())\n\n    # Deprecated since Python 2.7 and 3.2\n    with contextlib.nested(lock, open('a.txt'), open('b.txt', 'w')) as values:\n        in_file, out_file = values\n        out_file.write(in_file.read())\n\n    # Since Python 3.3 (not backported to 2.7)\n    with contextlib.ExitStack() as stack:\n        stack.enter_context(lock)\n        in_file  = stack.enter_context(open('a.txt'))\n        out_file = stack.enter_context(open('b.txt', 'w'))\n        out_file.write(in_file.read())\n\n\nReplacing `contextlib.nested`_\n-------------------------------------------------------------------------------\n\nYou can use ``Contexter(*managers)`` as a drop-in replacement for ``contextlib.nested(*managers)``, just without the `confusing error prone quirks mentioned in the official documentation \u003chttp://docs.python.org/3/library/contextlib.html\u003e`_.\n\nReplacing `contextlib.closing`_\n-------------------------------------------------------------------------------\n\nJust forget about it. Contexter turns close-able objects into context managers automatically.\n\nReplacing `contextlib.ExitStack`_\n-------------------------------------------------------------------------------\n\nContexter offeres everything ``contextlib.ExitStack`` does (and more). If you want a drop-in replacement that also works for Python 2.x and 3.2, you can use our backported ``ExitStack``, a subclass of ``Contexter`` that is API compatible to the contextlib variant.\n\nReplacing everything else from contextlib\n-------------------------------------------------------------------------------\n\nIf you really want to stick with the standard API, you can. Contexter implements all public APIs from contextlib and backports new features as soon as they are introduced.\n\n\nMore examples:\n-------------------------------------------------------------------------------\n\nContexter keeps track of the results of all invoked context managers. You can access the results later and don't have to unpack them all at the beginning.\n\n.. code:: python\n\n    with Contexter(open('a.txt'), open('b.txt', 'w')) as ctx:\n        in_file, out_file = ctx.values()\n        assert ctx.value(0) is in_file\n        assert ctx[0] is in_file\n        assert ctx[0:2] == [in_file, out_file]\n        assert len(ctx) == 2\n\nIf you don't like the ``\u003c\u003c`` syntax, there is a method that does the same.\n\n.. code:: python\n\n    with Contexter() as ctx:\n        in_file = ctx \u003c\u003c open('a.txt')\n        out_file = ctx.append(open('b.txt', 'w'))\n\nContexter contexts are nestable. Each level of nesting maintains its own stack of context managers and result values. This allows you to control the lifetime of contexts very precisely.\n\n.. code:: python\n\n    with Contexter() as ctx:\n        out_file = ctx \u003c\u003c open('b.txt', 'w')\n\n        with ctx:\n            in_file = ctx \u003c\u003c open('a.txt')\n            copy_data(in_file, out_file)\n\n        assert in_file.closed == True\n        assert out_file.closed == False\n\nLinks\n-------------------------------------------------------------------------------\n\n.. target-notes::\n\n.. _contextlib: http://docs.python.org/3/library/contextlib.html\n.. _contextlib.nested: http://docs.python.org/2/library/contextlib.html#contextlib.nested\n.. _contextlib.closing: http://docs.python.org/3/library/contextlib.html#contextlib.closing\n.. _contextlib.ExitStack: http://docs.python.org/3/library/contextlib.html#contextlib.ExitStack\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefnull%2Fcontexter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdefnull%2Fcontexter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefnull%2Fcontexter/lists"}