nanoutils.file_container

An abstract container for reading and writing files.

Index

file_to_context(file, **kwargs)

Take a path- or file-like object and return an appropiate context manager.

AbstractFileContainer(*args, **kwargs)

An abstract container for reading and writing files.

API

nanoutils.file_to_context(file, **kwargs)[source]

Take a path- or file-like object and return an appropiate context manager.

Passing a path-like object will supply it to open(), while passing a file-like object will pass it to contextlib.nullcontext().

Examples

>>> from io import StringIO
>>> from nanoutils import file_to_context

>>> path_like = 'file_name.txt'
>>> file_like = StringIO('this is a file-like object')

>>> context1 = file_to_context(file_like)
>>> with context1 as f1:
...     ...  

>>> context2 = file_to_context(path_like)  
>>> with context2 as f2:  
...     ... # insert operations here  
Parameters:
Returns:

An initialized context manager. Entering the context manager will return a file-like object.

Return type:

ContextManager[IO]

class nanoutils.AbstractFileContainer(*args, **kwargs)[source]

An abstract container for reading and writing files.

Two public methods are defined within this class:

Examples

>>> from io import StringIO
>>> from nanoutils import AbstractFileContainer

>>> class SubClass(AbstractFileContainer):
...     def __init__(self, value: str):
...         self.value = value
...
...     @classmethod
...     def _read(cls, file_obj, decoder):
...         value = decoder(file_obj.read())
...         return {'value': value}
...
...     def _write(self, file_obj, encoder):
...         value = encoder(self.value)
...         file_obj.write(value)

>>> file1 = StringIO('This is a file-like object')
>>> file2 = StringIO()

>>> obj = SubClass.read(file1)
>>> obj.write(file2)

>>> print(file2.getvalue())
This is a file-like object
classmethod read(file, bytes_decoding=None, **kwargs)[source]

Construct a new instance from this object’s class by reading the content of file.

Parameters:
  • file (str, bytes, os.PathLike or IO) –

    A path- or file-like object.

  • bytes_decoding (str, optional) – The type of encoding to use when reading from file when it will be/is be opened in bytes mode. This value should be left empty otherwise.

  • **kwargs (Any) – Further keyword arguments for open(). Only relevant if file is a path-like object.

Returns:

A new instance constructed from file.

Return type:

nanoutils.AbstractFileContainer

abstract classmethod _read(file_obj, decoder)[source]

A helper function for read().

Parameters:
  • file_obj (IO[AnyStr]) – A file-like object opened in read mode.

  • decoder (Callable[[AnyStr], str]) – A function for converting the items of file_obj into strings.

Returns:

A dictionary with keyword arguments for a new instance of this objects’ class.

Return type:

Dict[str, Any]

See also

AbstractFileContainer.read()

Construct a new instance from this object’s class by reading the content of file.

_read_postprocess()[source]

Post process new instances created by read().

Return type:

None

See also

AbstractFileContainer.read()

Construct a new instance from this object’s class by reading the content of file.

write(file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, bytes_encoding=None, **kwargs)[source]

Write the content of this instance to file.

Parameters:
  • file (str, bytes, os.PathLike or IO) –

    A path- or file-like object. Defaults to sys.stdout if not specified.

  • bytes_encoding (str, optional) – The type of encoding to use when writing to file when it will be/is be opened in bytes mode. This value should be left empty otherwise.

  • **kwargs (Any) – Further keyword arguments for open(). Only relevant if file is a path-like object.

Return type:

None

abstract _write(file_obj, encoder)[source]

A helper function for write().

Parameters:
  • file_obj (IO[AnyStr]) – A file-like object opened in write mode.

  • encoder (Callable[[str], AnyStr]) – A function for converting strings into either str or bytes, the exact type matching that of file_obj.

Return type:

None

See also

AbstractFileContainer.write()

Write the content of this instance to file.