{"id":18084924,"url":"https://github.com/jwodder/inplace","last_synced_at":"2025-04-06T06:11:45.459Z","repository":{"id":44390161,"uuid":"73298075","full_name":"jwodder/inplace","owner":"jwodder","description":"In-place file processing in Python","archived":false,"fork":false,"pushed_at":"2025-01-23T14:36:50.000Z","size":199,"stargazers_count":33,"open_issues_count":5,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-30T04:09:55.343Z","etag":null,"topics":["available-on-pypi","file","fileinput","in-place","inplace","io","open","python","redirection","sed","tempfile","tmpfile"],"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/jwodder.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.md","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":"2016-11-09T15:50:18.000Z","updated_at":"2025-02-16T19:49:41.000Z","dependencies_parsed_at":"2024-01-16T16:23:11.123Z","dependency_job_id":"8a5016a9-ddcb-4edb-95fc-81b7833c73e8","html_url":"https://github.com/jwodder/inplace","commit_stats":{"total_commits":185,"total_committers":1,"mean_commits":185.0,"dds":0.0,"last_synced_commit":"1a0fc9f80333e611ce240b120ccd40e93db1f746"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Finplace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Finplace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Finplace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Finplace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwodder","download_url":"https://codeload.github.com/jwodder/inplace/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247441055,"owners_count":20939239,"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":["available-on-pypi","file","fileinput","in-place","inplace","io","open","python","redirection","sed","tempfile","tmpfile"],"created_at":"2024-10-31T15:08:39.391Z","updated_at":"2025-04-06T06:11:45.431Z","avatar_url":"https://github.com/jwodder.png","language":"Python","readme":"|repostatus| |ci-status| |coverage| |pyversions| |conda| |license|\n\n.. |repostatus| image:: https://www.repostatus.org/badges/latest/active.svg\n    :target: https://www.repostatus.org/#active\n    :alt: Project Status: Active - The project has reached a stable, usable\n          state and is being actively developed.\n\n.. |ci-status| image:: https://github.com/jwodder/inplace/actions/workflows/test.yml/badge.svg\n    :target: https://github.com/jwodder/inplace/actions/workflows/test.yml\n    :alt: CI Status\n\n.. |coverage| image:: https://codecov.io/gh/jwodder/inplace/branch/master/graph/badge.svg\n    :target: https://codecov.io/gh/jwodder/inplace\n\n.. |pyversions| image:: https://img.shields.io/pypi/pyversions/in_place.svg\n    :target: https://pypi.org/project/in_place\n\n.. |conda| image:: https://img.shields.io/conda/vn/conda-forge/in_place.svg\n    :target: https://anaconda.org/conda-forge/in_place\n    :alt: Conda Version\n\n.. |license| image:: https://img.shields.io/github/license/jwodder/inplace.svg?maxAge=2592000\n    :target: https://opensource.org/licenses/MIT\n    :alt: MIT License\n\n`GitHub \u003chttps://github.com/jwodder/inplace\u003e`_\n| `PyPI \u003chttps://pypi.org/project/in_place\u003e`_\n| `Issues \u003chttps://github.com/jwodder/inplace/issues\u003e`_\n| `Changelog \u003chttps://github.com/jwodder/inplace/blob/master/CHANGELOG.md\u003e`_\n\nThe ``in_place`` module provides an ``InPlace`` class for reading \u0026 writing a\nfile \"in-place\": data that you write ends up at the same filepath that you read\nfrom, and ``in_place`` takes care of all the necessary mucking about with\ntemporary files for you.\n\nFor example, given the file ``somefile.txt``::\n\n    'Twas brillig, and the slithy toves\n        Did gyre and gimble in the wabe;\n    All mimsy were the borogoves,\n        And the mome raths outgrabe.\n\nand the program ``disemvowel.py``:\n\n.. code:: python\n\n    import in_place\n\n    with in_place.InPlace(\"somefile.txt\") as fp:\n        for line in fp:\n            fp.write(\"\".join(c for c in line if c not in \"AEIOUaeiou\"))\n\nafter running the program, ``somefile.txt`` will have been edited in place,\nreducing it to just::\n\n    'Tws brllg, nd th slthy tvs\n        Dd gyr nd gmbl n th wb;\n    ll mmsy wr th brgvs,\n        nd th mm rths tgrb.\n\nand no sign of those pesky vowels remains!  If you want a sign of those pesky\nvowels to remain, you can instead save the file's original contents in, say,\n``somefile.txt~`` by constructing the filehandle with:\n\n.. code:: python\n\n    in_place.InPlace(\"somefile.txt\", backup_ext=\"~\")\n\nor save to ``someotherfile.txt`` with:\n\n.. code:: python\n\n    in_place.InPlace(\"somefile.txt\", backup=\"someotherfile.txt\")\n\nCompared to the in-place filtering implemented by the Python standard library's\n|fileinput|_ module, ``in_place`` offers the following benefits:\n\n- Instead of hijacking ``sys.stdout``, a new filehandle is returned for\n  writing.\n- The filehandle supports all of the standard I/O methods, not just\n  ``readline()``.\n- There are options for setting the encoding, encoding error handling, and\n  newline policy for opening the file, along with support for opening files in\n  binary mode, and these options apply to both input and output.\n- The complete filename of the backup file can be specified; you aren't\n  constrained to just adding an extension.\n- When used as a context manager, ``in_place`` will restore the original file\n  if an exception occurs.\n- The creation of temporary files won't silently clobber innocent bystander\n  files.\n\n.. |fileinput| replace:: ``fileinput``\n.. _fileinput: https://docs.python.org/3/library/fileinput.html\n\n\nInstallation\n============\n``in_place`` requires Python 3.8 or higher.  Just use `pip\n\u003chttps://pip.pypa.io\u003e`_ for Python 3 (You have pip, right?) to install it::\n\n    python3 -m pip install in_place\n\n\nBasic Usage\n===========\n``in_place`` provides a single class, ``InPlace``.  Its constructor takes the\nfollowing arguments:\n\n``name=\u003cPATH\u003e`` (required)\n   The path to the file to open \u0026 edit in-place\n\n``mode=\u003c\"b\"|\"t\"|None\u003e``\n   Whether to operate on the file in binary or text mode.  If ``mode`` is\n   ``\"b\"``, the file will be opened in binary mode, and data will be read \u0026\n   written as ``bytes`` objects.  If ``mode`` is ``\"t\"`` or ``None`` (the\n   default), the file will be opened in text mode, and data will be read \u0026\n   written as ``str`` objects.\n\n``backup=\u003cPATH\u003e``\n   If set, the original contents of the file will be saved to the given path\n   when the instance is closed.  ``backup`` cannot be set to the empty string.\n\n``backup_ext=\u003cEXTENSION\u003e``\n   If set, the path to the backup file will be created by appending\n   ``backup_ext`` to the original file path.\n\n   ``backup`` and ``backup_ext`` are mutually exclusive.  ``backup_ext`` cannot\n   be set to the empty string.\n\n``**kwargs``\n   Any additional keyword arguments (such as ``encoding``, ``errors``, and\n   ``newline``) will be forwarded to ``open()`` when opening both the input and\n   output file streams.\n\n``name``, ``backup``, and ``backup_ext`` can be ``str``, filesystem-encoded\n``bytes``, or path-like objects.\n\n``InPlace`` instances act as read-write filehandles with the usual filehandle\nattributes, specifically::\n\n    __iter__()              __next__()              closed\n    flush()                 name                    read()\n    read1() *               readinto() *            readinto1() *\n    readline()              readlines()             write()\n    writelines()\n\n    * binary mode only\n\n``InPlace`` instances also feature the following new or modified attributes:\n\n``close()``\n   Close filehandles and move files to their final destinations.  If called\n   after the filehandle has already been closed, ``close()`` does nothing.\n\n   Be sure to always close your instances when you're done with them by calling\n   ``close()`` or ``rollback()`` either explicitly or implicitly (i.e., via use\n   as a context manager).\n\n``rollback()``\n   Like ``close()``, but discard the output data (keeping the original file\n   intact) instead of replacing the original file with it\n\n``__enter__()``, ``__exit__()``\n   When an ``InPlace`` instance is used as a context manager, on exiting the\n   context, the instance will be either closed (if all went well) or rolled\n   back (if an exception occurred).  ``InPlace`` context managers are not\n   reusable_ but are reentrant_ (as long as no further operations are performed\n   after the innermost context ends).\n\n``input``\n   The actual filehandle that data is read from, in case you need to access it\n   directly\n\n``output``\n   The actual filehandle that data is written to, in case you need to access it\n   directly\n\n.. _reentrant: https://docs.python.org/3/library/contextlib.html#reentrant-cms\n.. _reusable: https://docs.python.org/3/library/contextlib.html#reusable-context-managers\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwodder%2Finplace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwodder%2Finplace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwodder%2Finplace/lists"}