Try to import all modules below a given root
Project description
✏️safer: a safer file writer ✏️
No more partial writes or corruption! safer writes a whole file or nothing.
safer.writer() and safer.printer() are context managers that open a file for writing or printing: if an Exception is raised, then the original file is left unaltered.
Install safer from the command line using pip:
pip install safer
Tested on Python 2.7, and 3.4 through 3.8.
EXAMPLES
safer.writer
# dangerous
with open(file, 'w') as fp:
json.dump(data, fp) # If this fails, the file is corrupted
# safer
with safer.writer(file) as fp:
json.dump(data, fp) # If this fails, the file is unaltered
safer.printer
# dangerous
with open(file, 'w') as fp:
for item in items:
print(item, file=fp)
# Prints a partial file if ``items`` raises an exception while iterating
# or any ``item.__str__()`` raises an exception
# safer
with safer.printer(file) as print:
for item in items:
print(item)
# Either the whole file is written, or nothing
API call documentation
safer.writer(file, mode='w', create_parent=False, delete_failures=True, **kwargs)
A context manager that yields a writable stream returned from open(), but leaves the file unchanged if an exception is raised.
It uses an extra temporary file which is renamed over the file only after the context manager exits successfully: this requires as much disk space as the old and new files put together.
If mode contains either 'a' (append), or '+' (update), then the original file will be copied to the temporary file before writing starts.
- Arguments:
- file:
Path to the file to be opened
- mode:
Mode string passed to open()
- create_parent:
If true, create the parent directory of the file if it doesn’t exist
- delete_failures:
If true, the temporary file is deleted if there is an exception
- kwargs:
Keywords passed to open()
safer.printer(file, mode='w', create_parent=False, delete_failures=True, **kwargs)
A context manager that yields a function that prints to the opened file, but leaves the file unchanged if an exception is raised.
It uses an extra temporary file which is renamed over the file only after the context manager exits successfully: this requires as much disk space as the old and new files put together.
If mode contains either 'a' (append), or '+' (update), then the original file will be copied to the temporary file before writing starts.
- Arguments:
- file:
Path to the file to be opened
- mode:
Mode string passed to open()
- create_parent:
If true, create the parent directory of the file if it doesn’t exist
- delete_failures:
If true, the temporary file is deleted if there is an exception
- kwargs:
Keywords passed to open()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file safer-0.9.11.tar.gz
.
File metadata
- Download URL: safer-0.9.11.tar.gz
- Upload date:
- Size: 3.1 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | db6917cdc3b30c6002634a5f68e28a9ac6f9253bd3e2a35d1b823d1965af4c73 |
|
MD5 | 029d3a2f2f1c344605c1fe650c0425a9 |
|
BLAKE2b-256 | 6d23247e630a5cb23bfdd7b9d56b48d2798bfeaa938dc9ea1f44d069741ce5f2 |
File details
Details for the file safer-0.9.11-py3-none-any.whl
.
File metadata
- Download URL: safer-0.9.11-py3-none-any.whl
- Upload date:
- Size: 6.1 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ce7796378171ac04a41f62bf72170dc3bf94aa46f84d1a0ccf0c539d450694a |
|
MD5 | 2b9f858273a40e7176c4c2d5a2040bd2 |
|
BLAKE2b-256 | eb89fc34184f2016ec7bdfcf30426f98c09885214efa6ab1b07b1339a8db62d1 |