pytomlpp

Python wrapper for Toml++.

Install from PyPI

View Source
"""

Python wrapper for Toml++.

Install from [PyPI](https://pypi.org/project/pytomlpp/)


"""

__all__ = ["DecodeError", "dumps", "loads", "dump", "load"]

from ._impl import DecodeError, lib_version
from ._io import dump, dumps, load, loads
#   class DecodeError(builtins.Exception):

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
args
#   def dumps(data: Dict[Any, Any]) -> str:
View Source
def dumps(data: Dict[Any, Any]) -> str:
    """Serialise data to TOML string.

    Args:
        data (Dict[Any, Any]): input data

    Returns:
        str: seralised data
    """
    return _impl.dumps(data)

Serialise data to TOML string.

Args
  • data (Dict[Any, Any]): input data
Returns

str: seralised data

#   def loads(data: str) -> Dict[Any, Any]:
View Source
def loads(data: str) -> Dict[Any, Any]:
    """Deserialise from TOML string to python dict.

    Args:
        data (str): TOML string

    Returns:
        Dict[Any, Any]: deserialised data
    """
    return _impl.loads(data)

Deserialise from TOML string to python dict.

Args
  • data (str): TOML string
Returns

Dict[Any, Any]: deserialised data

#   def dump( data: Dict[Any, Any], fl: Union[str, TextIO, BinaryIO, os.PathLike], mode: str = 'w' ) -> None:
View Source
def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w") -> None:
    """Serialise data to TOML file

    Args:
        data (Dict[Any, Any]): input data
        fl (FilePathOrObject): file like object or path
        mode (str, optional): mode to write the file, support "w", "wt" (text) or "wb" (binary). Defaults to "w".
    """
    data = _impl.dumps(data)
    if mode == "wb":
        data = data.encode("utf-8")
    if hasattr(fl, "write"):
        fl.write(data)
        return
    with open(fl, mode=mode) as fh:
        fh.write(data)

Serialise data to TOML file

Args
  • data (Dict[Any, Any]): input data
  • fl (FilePathOrObject): file like object or path
  • mode (str, optional): mode to write the file, support "w", "wt" (text) or "wb" (binary). Defaults to "w".
#   def load( fl: Union[str, TextIO, BinaryIO, os.PathLike], mode: str = 'r' ) -> Dict[Any, Any]:
View Source
def load(fl: FilePathOrObject, mode: str = "r") -> Dict[Any, Any]:
    """Deserialise from TOML file to python dict.

    Args:
        fl (FilePathOrObject): file like object or path
        mode (str, optional): mode to read the file, support "r", "rt" (text) or "rb" (binary). Defaults to "r".

    Returns:
        Dict[Any, Any]: deserialised data
    """

    if hasattr(fl, "read"):
        data = fl.read()
    else:
        with open(fl, mode=mode) as fh:
            data = fh.read()
    if isinstance(data, bytes):
        return _impl.loads(data.decode("utf-8"))
    return _impl.loads(data)

Deserialise from TOML file to python dict.

Args
  • fl (FilePathOrObject): file like object or path
  • mode (str, optional): mode to read the file, support "r", "rt" (text) or "rb" (binary). Defaults to "r".
Returns

Dict[Any, Any]: deserialised data