wrapfile
Create a file-like wrapper based on the actual file or a path to it, or an internal memory buffer.
The purpose is to simplify coding functions with a 'filepath_or_buffer' argument meaning that what is supplied as the argument can be a file-like object, str or path object, or even None (for writing data to an in-memory buffer).
-
If the argument is file-like, it is simply wrapped to perform the actual I/O operations.
-
If the argument is str or path object, it is treated as path of the file to be opened and managed (the user does not have to bother about closing the internal file).
-
If the argument None, an empty in-memory buffer is created (io.BytesIO or io.StringIO) to which data can be written. The written data can be returned back using the getvalue() implemented in one of the classes, TextFileWriter.
The resulting wrapper redirects most of the methods to the actual file object. The close() method of the wrapper closes the actual file only when it was opened when constructing the wrapper (based on the provided file path) or if the in-memory buffer was created.
Example
from wrapfile import TextFileWriter
def say_hello(filepath_or_buffer=None):
"""Just say 'Hello, world!'
Parameters:
-----------
filepath_or_buffer : str, path or file-like object, optional
A file to save the output.
If None, the output will be returned.
Returns:
--------
str or None:
the output if the argument was omitted,
or None otherwise.
"""
with TextFileWriter(filepath_or_buffer) as f:
f.write("Hello, world!")
result = f.getvalue()
return result
# usage:
# - with no argument
print(say_hello())
# - with a str or path-like object
say_hello('myfile.txt')
# - with a file-like object
with open('myfile.txt', 'w') as f:
say_hello(f)
from wrapfile import TextFileReader
import sys
def read_from(filepath_or_buffer=sys.stdin):
"""Read from the file or standard input
Parameters:
-----------
filepath_or_buffer : str, path or file-like object, optional
A file to read the input from.
The default is to read from the standard input (sys.stdin).
Returns:
--------
str :
The file contents.
"""
with TextFileReader(filepath_or_buffer) as f:
result = f.read()
return result
# usage:
# - with the standard input
print(read_from())
# - with another file-like object
with open('myfile.txt', 'r') as f:
print(read_from(f))
# - with a str or path-like object
print(read_from('myfile.txt')
Metadata
Release files for wrapfile 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wrapfile-0.1.tar.gz | 4.5 kB | Without provenance | Details for wrapfile-0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wrapfile-0.1-py3-none-any.whl | Python 3 | none | any | Details for wrapfile-0.1-py3-none-any.whl |
Total release size: 9.4 kB
Release files / wrapfile-0.1.tar.gz
| Download URL | wrapfile-0.1.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
550842876b757363512323c5ad7fcff02c6ee4d02bd6c75768a16a8a28159b27
|
|
BLAKE2b-256 checksum How to use checksums |
2170ad28fb1ac0c55200d4d6c291e48ed42095a1f6c67c4f29af3606694c6ede
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.20
|
Release files / wrapfile-0.1-py3-none-any.whl
| Download URL | wrapfile-0.1-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
28f066385183f5dba2d3dffc4252bcf5e67ad1a45f47bc2b0cb6056336b1acd1
|
|
BLAKE2b-256 checksum How to use checksums |
646a182d0d16f6dfcf28853df29624554e11b7213fa38359a1e0caf7dd1617d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.20
|
Security for wrapfile 0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.