Try to import all modules below a given root
Project description
✏️safer: safe file writer ✏️
Safely write or print to a file, so that the original file is only overwritten if the operation completes successfuly.
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.writer
# dangerous
with open(config_filename, 'w') as fp:
json.dump(cfg, fp) # If this fails, the config file gets broken
# safer
with safer.writer(config_filename) as fp:
json.dump(cfg, fp) # If this fails, the config file is untouched
safer.printer
with safer.printer(configs_filename) as print:
for cfg in configs:
print(json.dumps(cfg))
API call documentation
safer.writer(file, mode='w', create_parents=False, delete_failures=True, suffix='.tmp', **kwargs)
A context that yields a writable stream, like from open(), but 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 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
- kwargs:
Keywords passed to built-in open
safer.printer(file, mode='w', create_parents=False, delete_failures=True, suffix='.tmp', **kwargs)
A context that yields a function that prints to the opened file, but 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 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
- kwargs:
Keywords passed to built-in 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.8.tar.gz
.
File metadata
- Download URL: safer-0.9.8.tar.gz
- Upload date:
- Size: 2.8 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 | 820dc2f1ec1744021dd285ad0defa7bf839c5e1ffbbdc5d58c9b29b69ddb1ad2 |
|
MD5 | 110ede008065af5e938ebe0d1a11ab73 |
|
BLAKE2b-256 | c4bc7af1899122211c33327548ff2287d4eb80a91df80f50e8285bd79ef6b3d6 |
File details
Details for the file safer-0.9.8-py3-none-any.whl
.
File metadata
- Download URL: safer-0.9.8-py3-none-any.whl
- Upload date:
- Size: 5.5 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 | ca8d72ea7441bbdcb0f1f53a17d3a9a80188704339f6d7337ccdb122337d99e5 |
|
MD5 | a75935862b4fd481f98aee992e5b6a8c |
|
BLAKE2b-256 | f15b4ebe5b4be43e298fd80d686a3e4443e5d8d235bdf51b63d6997d8dd20de0 |