nanoutils.file_container¶
An abstract container for reading and writing files.
Index¶
|
Take a path- or file-like object and return an appropiate context manager. |
|
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 tocontextlib.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:
- class nanoutils.AbstractFileContainer(*args, **kwargs)[source]¶
An abstract container for reading and writing files.
Two public methods are defined within this class:
AbstractFileContainer.read()
: Construct a new instance from this object’s class by reading the content to a file or file object. How the content of the to-be read file is parsed has to be defined in theAbstractFileContainer._read()
abstract method. Additional post processing, after the new instance has been created, can be performed withAbstractFileContainer._read_postprocess()
AbstractFileContainer.write()
: Write the content of this instance to an opened file or file object. How the content of the to-be exported class instance is parsed has to be defined in theAbstractFileContainer._write()
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
orIO
) –bytes_decoding (
str
, optional) – The type of encoding to use when reading from file when it will be/is be opened inbytes
mode. This value should be left empty otherwise.**kwargs (
Any
) – Further keyword arguments foropen()
. Only relevant if file is a path-like object.
- Returns:
A new instance constructed from file.
- Return type:
- 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:
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:
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
orIO
) –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 inbytes
mode. This value should be left empty otherwise.**kwargs (
Any
) – Further keyword arguments foropen()
. Only relevant if file is a path-like object.
- Return type:
- 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 eitherstr
orbytes
, the exact type matching that of file_obj.
- Return type:
See also
AbstractFileContainer.write()
Write the content of this instance to file.