Skip to main content

Try to import all modules below a given root

Project description

✏️safer ✏️

Safely write or print to a file, leaving it unchanged if there’s an exception.

Works on Python versions 2.7 through 3.8 and likely beyond.

Writes are performed on a temporary file, which is only copied over the original file after the context completes successfully. Note that this temporarily uses as much disk space as the old file and the new file put together.

EXAMPLES

safer.open

import safer

with safer.open(filename, 'w') as fp:
    for line in source():
       fp.write('this and that\n')
       fp.write('two-lines!')

    if CHANGED_MY_MIND:
        raise ValueError
        # Contents of `filename` will be unchanged

safer.printer

with safer.printer(filename) as print:
    print('this', 'and', 'that')
    print('two', 'lines', sep='-', end='!')

    if CHANGED_MY_MIND:
        raise ValueError
        # Contents of ``filename`` will be unchanged

API call documentation

safer.open(file, mode='r', create_parents=False, delete_failures=True, suffix='.tmp')

A context that yields a stream like built-in open() would, and undoes any changes to the file if there’s an exception.

Arguments:
file:

Path to the file to be opened

mode:

Mode string passed to built-in open

create_parents:

If True, all parent directories are automatically created

delete_failures:

Are partial files deleted if the context terminates with an exception?

suffix:

File suffix to use for temporary files

safer.printer(file, mode='w', create_parents=False, delete_failures=True, suffix='.tmp')

A context that yields a print function that prints to the file, but which undoes any changes to the file if there’s an exception.

Arguments:
file:

Path to the file to be opened

mode:

Mode string passed to built-in open

create_parents:

If True, all parent directories are automatically created

delete_failures:

Are partial files deleted if the context terminates with an exception?

suffix:

File suffix to use for temporary files

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safer-0.9.7.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

safer-0.9.7-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file safer-0.9.7.tar.gz.

File metadata

  • Download URL: safer-0.9.7.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for safer-0.9.7.tar.gz
Algorithm Hash digest
SHA256 96ceadf9a05dfa9c6c5ab0b8ff895c8cc8fb8d8ba86898c0b13b0a94637c174d
MD5 9f0d44d33a69b3a3bb08c13312e329e1
BLAKE2b-256 9da93c773b742b1eabba75bc9d76b895422f70f2da0fe204cfe8c2ba8faf4b8c

See more details on using hashes here.

File details

Details for the file safer-0.9.7-py3-none-any.whl.

File metadata

  • Download URL: safer-0.9.7-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for safer-0.9.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a1c025a8f5ce04caf7df0300361556bb7847156c6099e0f1292249db7b13a6a3
MD5 0d860ec742a23beb2a5a98a839ecdb55
BLAKE2b-256 cff9284e3a258584428d8c9f54147a59dbf54165861ac9f054ff4d574967492c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page