From d088c34dfa3607dbe02b6a40a051942943b06e31 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 1 Mar 2023 22:15:12 -0600 Subject: [PATCH 1/2] feat: Added reachable stringify & encode functionality. --- openjson/__init__.py | 8 ++-- openjson/api.py | 14 +++++++ openjson/encoding/__init__.py | 6 ++- openjson/encoding/encode.py | 69 +++++++++++++++++++++++++++++++++++ setup.py | 2 +- 5 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 openjson/api.py create mode 100644 openjson/encoding/encode.py diff --git a/openjson/__init__.py b/openjson/__init__.py index cfb22d6..678b33f 100644 --- a/openjson/__init__.py +++ b/openjson/__init__.py @@ -1,9 +1,7 @@ # Dunder attributes -__version__ = "v0.0.0" # update setup.py +__version__ = "v0.0.1" # update setup.py __author__ = "Jordan Welsman" -from .encoding import * -from .loading import * -from .saving import * +from .api import * -__all__ = encoding.__all__, loading.__all__, saving.__all__ \ No newline at end of file +__all__ = api.__all__ \ No newline at end of file diff --git a/openjson/api.py b/openjson/api.py new file mode 100644 index 0000000..8e68960 --- /dev/null +++ b/openjson/api.py @@ -0,0 +1,14 @@ +from openjson.encoding import _encode + +__all__ = ['stringify'] + +def stringify(object: dict) -> str: + string: str = "{" + for index, item in enumerate(object): + if index != len(object) - 1: + string += f"{_encode(key=item, value=object[item])}, " + else: + string += f"{_encode(key=item, value=object[item])}" + + string += "}" + return string \ No newline at end of file diff --git a/openjson/encoding/__init__.py b/openjson/encoding/__init__.py index b680692..ec295fc 100644 --- a/openjson/encoding/__init__.py +++ b/openjson/encoding/__init__.py @@ -1 +1,5 @@ -__all__ = [] \ No newline at end of file +# Import submodule files so +# functions are usable at +# 'from openjson import _' level. +from .encode import * +__all__ = encode.__all__ \ No newline at end of file diff --git a/openjson/encoding/encode.py b/openjson/encoding/encode.py new file mode 100644 index 0000000..f9cd96b --- /dev/null +++ b/openjson/encoding/encode.py @@ -0,0 +1,69 @@ +# External class visibility +__all__ = ['_encode_object', '_encode_array', '_encode_number_int', '_encode_number_real', '_encode_string', '_encode_boolean', '_encode_null', '_encode'] + +def _encode_object(key: str, value: dict): + string = f"\"{key}\": " + "{" + for index, item in enumerate(value): + if index != len(value) - 1: + string += f"{_encode(item, value[item])}, " + else: + string += f"{_encode(item, value[item])}" + string += "}" + return string + + +def _encode_array(key: str, value: list): + string = f"\"{key}\": [" + for index, item in enumerate(value): + if index != len(value) - 1: + string += f"\"{item}\", " + else: + string += f"\"{item}\"" + string += "]" + return string + + +def _encode_number_int(key: str, value: int): + return f"\"{key}\": {value}" + + +def _encode_number_real(key: str, value: int, precision: float = None): + if precision is not None: + return f"\"{key}\": {round(value, precision)}" + else: + return f"\"{key}\": {value}" + + +def _encode_string(key: str, value: str): + return f"\"{key}\": \"{value}\"" + + +def _encode_boolean(key: str, value: bool): + if value is True: + return f"\"{key}\": true" + if value is False: + return f"\"{key}\": false" + + +def _encode_null(key: str): + return f"\"{key}\": null" + + +def _encode(key: str, value: object, int_precision: int = None) -> str: + match type(value).__name__: + case "dict": + return _encode_object(key=key, value=value) + case "list": + return _encode_array(key=key, value=value) + case "int": + return _encode_number_int(key=key, value=value) + case "float": + return _encode_number_real(key=key, value=value, precision=int_precision) + case "str": + return _encode_string(key=key, value=value) + case "bool": + return _encode_boolean(key=key, value=value) + case "NoneType": + return _encode_null(key=key) + case other: + raise NotImplementedError(f"Type \"{other}\" is not supported.") diff --git a/setup.py b/setup.py index 811d53a..92665a8 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup # Arguments -version = "0.0.0" # update __init__.py +version = "0.0.1" # update __init__.py python_version = ">=3.10" # Long description from README.md From 17664b0c42034d5f6023d3acd9815bd5b991ba5b Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 1 Mar 2023 22:18:40 -0600 Subject: [PATCH 2/2] build: Incremented version number. --- openjson/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openjson/__init__.py b/openjson/__init__.py index 678b33f..b29979a 100644 --- a/openjson/__init__.py +++ b/openjson/__init__.py @@ -1,5 +1,5 @@ # Dunder attributes -__version__ = "v0.0.1" # update setup.py +__version__ = "v0.1.0" # update setup.py __author__ = "Jordan Welsman" from .api import * diff --git a/setup.py b/setup.py index 92665a8..f11ab41 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup # Arguments -version = "0.0.1" # update __init__.py +version = "0.1.0" # update __init__.py python_version = ">=3.10" # Long description from README.md