From 017877d5f428630fb01cda94d374a76c3bda123b Mon Sep 17 00:00:00 2001 From: Backest <99742401+Backist@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:28:52 +0100 Subject: [PATCH 01/11] Added optional encoding to dump and load functions --- src/pytomlpp/_io.py | 143 ++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 71 deletions(-) diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 0df3ba2..491741f 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -1,71 +1,72 @@ -"""Python wrapper for Toml++ IO methods.""" - -import os -from typing import Any, BinaryIO, Dict, TextIO, Union - -from . import _impl - -FilePathOrObject = Union[str, TextIO, BinaryIO, os.PathLike] - - -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) - - -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) - - -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) - - -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) +"""Python wrapper for Toml++ IO methods.""" + +import os +from typing import Any, BinaryIO, Dict, TextIO, Union, Optional + +from . import _impl + +FilePathOrObject = Union[str, TextIO, BinaryIO, os.PathLike] + + +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) + + +def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: Optional[str] = "utf-8") -> 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". + encoding (str): defaults to utf-8, if None, local encoding is selected. + """ + 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, encoding=encoding or None) as fh: + fh.write(data) + + +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) + + +def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = "utf-8") -> 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, encoding=encoding or None) as fh: + data = fh.read() + if isinstance(data, bytes): + return _impl.loads(data.decode("utf-8")) + return _impl.loads(data) From 583864a4518eeb12440def47975a252a9b4d9339 Mon Sep 17 00:00:00 2001 From: Backest <99742401+Backist@users.noreply.github.com> Date: Tue, 2 Jan 2024 15:42:52 +0100 Subject: [PATCH 02/11] Update _io.py. Binary modes does not take encoding argument. --- src/pytomlpp/_io.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 491741f..1c75ed1 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -27,15 +27,17 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: 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". - encoding (str): defaults to utf-8, if None, local encoding is selected. + encoding (str): defaults to utf-8, if None, local encoding is selected. + NOTE: ``Binary mode does not take enconding argument.`` """ data = _impl.dumps(data) if mode == "wb": + enconding = None data = data.encode("utf-8") if hasattr(fl, "write"): fl.write(data) return - with open(fl, mode=mode, encoding=encoding or None) as fh: + with open(fl, mode=mode, encoding=encoding) as fh: fh.write(data) @@ -61,11 +63,12 @@ def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = "utf-8 Returns: Dict[Any, Any]: deserialised data """ - + if mode == "rb": + encoding = None if hasattr(fl, "read"): data = fl.read() else: - with open(fl, mode=mode, encoding=encoding or None) as fh: + with open(fl, mode=mode, encoding=encoding) as fh: data = fh.read() if isinstance(data, bytes): return _impl.loads(data.decode("utf-8")) From d1bab50bc3697b6083d28122d096d0dbeba23f3d Mon Sep 17 00:00:00 2001 From: Backest <99742401+Backist@users.noreply.github.com> Date: Tue, 2 Jan 2024 15:48:55 +0100 Subject: [PATCH 03/11] Update _io.py --- src/pytomlpp/_io.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 1c75ed1..90092c5 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -28,7 +28,7 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: 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". encoding (str): defaults to utf-8, if None, local encoding is selected. - NOTE: ``Binary mode does not take enconding argument.`` + NOTE: ``If mode is binary mode, encoding optional argument will be negligible.`` """ data = _impl.dumps(data) if mode == "wb": @@ -59,6 +59,7 @@ def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = "utf-8 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". + NOTE: ``If mode is binary mode, encoding optional argument will be negligible.`` Returns: Dict[Any, Any]: deserialised data From 438a7ae28ef1d97699c6fb7de02af9a6f326bce8 Mon Sep 17 00:00:00 2001 From: Backest <99742401+Backist@users.noreply.github.com> Date: Tue, 2 Jan 2024 16:04:13 +0100 Subject: [PATCH 04/11] Update _io.py. Fix syntax error. --- src/pytomlpp/_io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 90092c5..63fc3a5 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -32,7 +32,7 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: """ data = _impl.dumps(data) if mode == "wb": - enconding = None + encoding = None data = data.encode("utf-8") if hasattr(fl, "write"): fl.write(data) From 1caf467cf28981a98e647677956df22734cda341 Mon Sep 17 00:00:00 2001 From: Backest <99742401+Backist@users.noreply.github.com> Date: Tue, 2 Jan 2024 16:10:55 +0100 Subject: [PATCH 05/11] Add documentation and fix encoding parameter trouble with binary modes. --- src/pytomlpp/_io.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 63fc3a5..dc14c57 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -59,6 +59,7 @@ def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = "utf-8 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". + encoding (str): defaults to utf-8, if None, local encoding is selected. NOTE: ``If mode is binary mode, encoding optional argument will be negligible.`` Returns: From 948fba74bf15554b32711db9438f862d677fe34e Mon Sep 17 00:00:00 2001 From: Backist Date: Tue, 2 Jan 2024 16:14:19 +0100 Subject: [PATCH 06/11] Hotfix, syntax error. --- src/pytomlpp/_io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index dc14c57..cc9878f 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -33,7 +33,7 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: data = _impl.dumps(data) if mode == "wb": encoding = None - data = data.encode("utf-8") + data = data.encode("utf-8") if hasattr(fl, "write"): fl.write(data) return From 70b454840d97bb2f99daf43f62a9a4ed29c02e23 Mon Sep 17 00:00:00 2001 From: Backist Date: Tue, 2 Jan 2024 17:22:59 +0100 Subject: [PATCH 07/11] Updated load and dump functions. --- benchmark/run.py | 3 +-- src/pytomlpp/_io.py | 3 +-- tests/python-tests/test_api.py | 1 + 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/benchmark/run.py b/benchmark/run.py index 5120913..7a8cd55 100644 --- a/benchmark/run.py +++ b/benchmark/run.py @@ -1,10 +1,9 @@ import timeit from pathlib import Path -_parsers = [] +_parsers = [('pytomlpp', pytomlpp.loads)] import pytomlpp -_parsers.append(('pytomlpp', pytomlpp.loads)) try: import rtoml diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 7944635..5ad7c29 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -33,7 +33,7 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: data = _impl.dumps(data) if mode == "wb": encoding = None - data = data.encode("utf-8") + data = data.encode("utf-8") if hasattr(fl, "write"): fl.write(data) return @@ -42,7 +42,6 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: - def loads(data: str) -> Dict[Any, Any]: """Deserialise from TOML string to python dict. diff --git a/tests/python-tests/test_api.py b/tests/python-tests/test_api.py index addbf4a..e8e0336 100644 --- a/tests/python-tests/test_api.py +++ b/tests/python-tests/test_api.py @@ -11,6 +11,7 @@ from dateutil import parser as dateutil_parser +# import pytomlpp import pytomlpp TOML_TESTS_SKIP = [ From d6bd675dabcbfea46466f30803d4000309715f63 Mon Sep 17 00:00:00 2001 From: Backist Date: Tue, 2 Jan 2024 17:35:50 +0100 Subject: [PATCH 08/11] Updating enconding optional argument as None. Giving the freedom to user to pass whatever encoding to use. --- src/pytomlpp/_io.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 5ad7c29..1213c45 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -20,7 +20,7 @@ def dumps(data: Dict[Any, Any]) -> str: return _impl.dumps(data) -def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: Optional[str] = "utf-8") -> None: +def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: Optional[str] = None) -> None: """Serialise data to TOML file Args: @@ -32,7 +32,6 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: """ data = _impl.dumps(data) if mode == "wb": - encoding = None data = data.encode("utf-8") if hasattr(fl, "write"): fl.write(data) @@ -54,7 +53,7 @@ def loads(data: str) -> Dict[Any, Any]: return _impl.loads(data) -def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = "utf-8") -> Dict[Any, Any]: +def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = None) -> Dict[Any, Any]: """Deserialise from TOML file to python dict. Args: @@ -66,8 +65,6 @@ def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = "utf-8 Returns: Dict[Any, Any]: deserialised data """ - if mode == "rb": - encoding = None if hasattr(fl, "read"): data = fl.read() else: From 11a815540c0cef7448e32cf668d92197075d8eea Mon Sep 17 00:00:00 2001 From: Backist Date: Tue, 2 Jan 2024 17:38:41 +0100 Subject: [PATCH 09/11] Updating docs. --- src/pytomlpp/_io.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 1213c45..48ada4e 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -27,7 +27,7 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: 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". - encoding (str): defaults to utf-8, if None, local encoding is selected. + encoding (str): defaults to None. Local enconding will be selected with``locale.getpreferredencoding(False)`` NOTE: ``If mode is binary mode, encoding optional argument will be negligible.`` """ data = _impl.dumps(data) @@ -59,7 +59,7 @@ def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = None) 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". - encoding (str): defaults to utf-8, if None, local encoding is selected. + encoding (str): defaults to None. Local enconding will be selected with ``locale.getpreferredencoding(False)`` . NOTE: ``If mode is binary mode, encoding optional argument will be negligible.`` Returns: From 2d46a99931fefc9a9797cdf84d15e76b207a9470 Mon Sep 17 00:00:00 2001 From: Backist Date: Tue, 2 Jan 2024 22:18:27 +0100 Subject: [PATCH 10/11] . --- benchmark/run.py | 2 +- src/pytomlpp/_io.py | 5 ++--- tests/python-tests/test_api.py | 1 - 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/benchmark/run.py b/benchmark/run.py index 7a8cd55..783aa07 100644 --- a/benchmark/run.py +++ b/benchmark/run.py @@ -1,9 +1,9 @@ import timeit from pathlib import Path -_parsers = [('pytomlpp', pytomlpp.loads)] import pytomlpp +_parsers = [('pytomlpp', pytomlpp.loads)] try: import rtoml diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 48ada4e..edb56aa 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -27,7 +27,7 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: 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". - encoding (str): defaults to None. Local enconding will be selected with``locale.getpreferredencoding(False)`` + encoding (str): defaults to None. If None, local enconding will be selected. NOTE: ``If mode is binary mode, encoding optional argument will be negligible.`` """ data = _impl.dumps(data) @@ -40,7 +40,6 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: fh.write(data) - def loads(data: str) -> Dict[Any, Any]: """Deserialise from TOML string to python dict. @@ -59,7 +58,7 @@ def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = None) 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". - encoding (str): defaults to None. Local enconding will be selected with ``locale.getpreferredencoding(False)`` . + encoding (str): defaults to None. If None, local enconding will be selected. NOTE: ``If mode is binary mode, encoding optional argument will be negligible.`` Returns: diff --git a/tests/python-tests/test_api.py b/tests/python-tests/test_api.py index e8e0336..addbf4a 100644 --- a/tests/python-tests/test_api.py +++ b/tests/python-tests/test_api.py @@ -11,7 +11,6 @@ from dateutil import parser as dateutil_parser -# import pytomlpp import pytomlpp TOML_TESTS_SKIP = [ From d04e0b2173546a1724d20d2f4c120f334cfb99d4 Mon Sep 17 00:00:00 2001 From: Backist Date: Tue, 2 Jan 2024 22:19:21 +0100 Subject: [PATCH 11/11] . --- src/pytomlpp/_io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index edb56aa..bfbf9f6 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -37,7 +37,7 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: fl.write(data) return with open(fl, mode=mode, encoding=encoding) as fh: - fh.write(data) + fh.write(data) def loads(data: str) -> Dict[Any, Any]: