Skip to main content

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')

Download files

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

Source Distribution

wrapfile-0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wrapfile-0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file wrapfile-0.1.tar.gz.

File metadata

  • Download URL: wrapfile-0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.20

File hashes

Hashes for wrapfile-0.1.tar.gz
Algorithm Hash digest
SHA256 550842876b757363512323c5ad7fcff02c6ee4d02bd6c75768a16a8a28159b27
MD5 f5902d7c57b7005353e94ff39cfdfee0
BLAKE2b-256 2170ad28fb1ac0c55200d4d6c291e48ed42095a1f6c67c4f29af3606694c6ede

See more details on using hashes here.

File details

Details for the file wrapfile-0.1-py3-none-any.whl.

File metadata

  • Download URL: wrapfile-0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.20

File hashes

Hashes for wrapfile-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 28f066385183f5dba2d3dffc4252bcf5e67ad1a45f47bc2b0cb6056336b1acd1
MD5 71d6b10806719c06df3601209f1bdb4e
BLAKE2b-256 646a182d0d16f6dfcf28853df29624554e11b7213fa38359a1e0caf7dd1617d9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page