From 4f2ead8f34b728d026cf3911a19ef8d2454ef0ab Mon Sep 17 00:00:00 2001 From: afisher1 <4552674+afisher1@users.noreply.github.com> Date: Fri, 4 Aug 2023 15:28:06 -0700 Subject: [PATCH 1/4] Adding complex type json encode/decode extension and a test. --- gridappsd-python-lib/gridappsd/__init__.py | 1 + .../gridappsd/app_registration.py | 4 +- gridappsd-python-lib/gridappsd/goss.py | 4 +- gridappsd-python-lib/gridappsd/gridappsd.py | 4 +- .../gridappsd/json_extension.py | 55 +++++++++++++++++++ .../gridappsd/register_app.py | 9 ++- gridappsd-python-lib/gridappsd/simulation.py | 3 +- gridappsd-python-lib/gridappsd/timeseries.py | 4 +- gridappsd-python-lib/tests/test_json.py | 41 ++++++++++++++ 9 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 gridappsd-python-lib/gridappsd/json_extension.py create mode 100644 gridappsd-python-lib/tests/test_json.py diff --git a/gridappsd-python-lib/gridappsd/__init__.py b/gridappsd-python-lib/gridappsd/__init__.py index cffdd96..e57c6ab 100644 --- a/gridappsd-python-lib/gridappsd/__init__.py +++ b/gridappsd-python-lib/gridappsd/__init__.py @@ -53,5 +53,6 @@ from gridappsd.gridappsd import GridAPPSD from gridappsd.difference_builder import DifferenceBuilder from gridappsd.app_registration import ApplicationController +import gridappsd.json_extension as json diff --git a/gridappsd-python-lib/gridappsd/app_registration.py b/gridappsd-python-lib/gridappsd/app_registration.py index 03487e1..3ca2a6b 100644 --- a/gridappsd-python-lib/gridappsd/app_registration.py +++ b/gridappsd-python-lib/gridappsd/app_registration.py @@ -1,4 +1,4 @@ -import json +#import json import logging import os try: @@ -15,7 +15,7 @@ from . gridappsd import GridAPPSD from . topics import REQUEST_REGISTER_APP -from . import utils +from . import utils, json_extension as json _log = logging.getLogger(__name__) diff --git a/gridappsd-python-lib/gridappsd/goss.py b/gridappsd-python-lib/gridappsd/goss.py index 8dff23f..0151846 100644 --- a/gridappsd-python-lib/gridappsd/goss.py +++ b/gridappsd-python-lib/gridappsd/goss.py @@ -45,7 +45,7 @@ """ import base64 import inspect -import json +#import json import logging import os import random @@ -60,6 +60,8 @@ from stomp.exception import NotConnectedException from time import sleep +from gridappsd import json_extension as json + _log: Logger = logging.getLogger(inspect.getmodulename(__file__)) diff --git a/gridappsd-python-lib/gridappsd/gridappsd.py b/gridappsd-python-lib/gridappsd/gridappsd.py index d7e9297..694731b 100644 --- a/gridappsd-python-lib/gridappsd/gridappsd.py +++ b/gridappsd-python-lib/gridappsd/gridappsd.py @@ -39,14 +39,14 @@ # ------------------------------------------------------------------------------- import inspect -import json +#import json import logging from datetime import datetime from logging import DEBUG, INFO, WARNING, FATAL, WARN import time from gridappsd.goss import GOSS -from gridappsd import utils +from gridappsd import utils, json_extension as json import gridappsd.topics as t from gridappsd import ProcessStatusEnum from gridappsd.houses import Houses diff --git a/gridappsd-python-lib/gridappsd/json_extension.py b/gridappsd-python-lib/gridappsd/json_extension.py new file mode 100644 index 0000000..0c92bb3 --- /dev/null +++ b/gridappsd-python-lib/gridappsd/json_extension.py @@ -0,0 +1,55 @@ +import dataclasses +import json as _json +from typing import Any, TextIO + + +@dataclasses.dataclass +class JsonComplex: + real: float + imag: float + + +def jsonDecoderExtension(obj: Any): + rv = obj + if isinstance(obj, dict): + try: + complexInstance = JsonComplex(**obj) + rv = complex(complexInstance.real, complexInstance.imag) + except: + rv = obj + return rv + + +class JsonEncoderExtension(_json.JSONEncoder): + def default(self, obj: Any) -> Any: + rv = None + if isinstance(obj, complex): + jsonComplexInstance = JsonComplex( + real = obj.real, + imag = obj.imag + ) + rv = dataclasses.asdict(jsonComplexInstance) + elif dataclasses.is_dataclass(obj): + rv = dataclasses.asdict(obj) + else: + rv = super().default(obj) + return rv + + +def dump(data: Any, fo: TextIO): + rv = _json.dump(data, fo, cls=JsonEncoderExtension) + + +def dumps(data: Any) -> str: + rv = _json.dumps(data, cls=JsonEncoderExtension) + return rv + + +def load(fo: TextIO) -> Any: + rv = _json.load(fo, object_hook=jsonDecoderExtension) + return rv + + +def loads(data: str) -> Any: + rv = _json.loads(data, object_hook=jsonDecoderExtension) + return rv \ No newline at end of file diff --git a/gridappsd-python-lib/gridappsd/register_app.py b/gridappsd-python-lib/gridappsd/register_app.py index f9f5743..f58feb0 100644 --- a/gridappsd-python-lib/gridappsd/register_app.py +++ b/gridappsd-python-lib/gridappsd/register_app.py @@ -1,10 +1,10 @@ -import json +#import json import logging import os import sys import time import stomp -from gridappsd import ApplicationController, GridAPPSD, utils +from gridappsd import ApplicationController, GridAPPSD, utils, json_extension as json def main(): loglevel = logging.INFO @@ -25,7 +25,10 @@ def main(): _log.error("Invalid /appconfig reference...map the /appconfig to your container") sys.exit(1) - config = json.loads(open("/appconfig").read()) + config = {} + with open("/appconfig") as fo: + config = json.load(fo) + #config = json.loads(open("/appconfig").read()) if "id" not in config: _log.error("Invalid appconfig, must have a unique id set.") diff --git a/gridappsd-python-lib/gridappsd/simulation.py b/gridappsd-python-lib/gridappsd/simulation.py index 23af6f8..2edfa3c 100644 --- a/gridappsd-python-lib/gridappsd/simulation.py +++ b/gridappsd-python-lib/gridappsd/simulation.py @@ -3,11 +3,12 @@ import sys import time from copy import deepcopy -import json +#import json import logging from typing import Dict, List, Union import gridappsd.topics as t +from . import json_extension as json _log = logging.getLogger(__name__) diff --git a/gridappsd-python-lib/gridappsd/timeseries.py b/gridappsd-python-lib/gridappsd/timeseries.py index 41ba8a2..a8128b8 100644 --- a/gridappsd-python-lib/gridappsd/timeseries.py +++ b/gridappsd-python-lib/gridappsd/timeseries.py @@ -1,5 +1,5 @@ -import json -from gridappsd import topics +#import json +from gridappsd import topics, json_extension as json from typing import Optional diff --git a/gridappsd-python-lib/tests/test_json.py b/gridappsd-python-lib/tests/test_json.py new file mode 100644 index 0000000..89de077 --- /dev/null +++ b/gridappsd-python-lib/tests/test_json.py @@ -0,0 +1,41 @@ +from gridappsd import json + +def test_json_complex(): + message = { + "key1": { + "key2": complex(3.369,4.213), + "key3": { + "key4": complex(-5.147,-6.258), + "key5": { + "real": -9.654, + "imag": 8.321 + }, + }, + "key6": { + "real": 7.894, + "imag": -8.542, + "garbage": True + } + }, + "key7": complex(7.894, -8.542) + } + serializedMessage = """{"key1": {"key2": {"real": 3.369, "imag": 4.213}, "key3": {"key4": {"real": -5.147, "imag": -6.258}, "key5": {"real": -9.654, "imag": 8.321}}, "key6": {"real": 7.894, "imag": -8.542, "garbage": true}}, "key7": {"real": 7.894, "imag": -8.542}}""" + deserializedMessage = { + "key1": { + "key2": complex(3.369,4.213), + "key3": { + "key4": complex(-5.147,-6.258), + "key5": complex(-9.654, 8.321), + }, + "key6": { + "real": 7.894, + "imag": -8.542, + "garbage": True + } + }, + "key7": complex(7.894, -8.542) + } + encodedStr = json.dumps(message) + assert encodedStr == serializedMessage + decodedMessage = json.loads(encodedStr) + assert decodedMessage == deserializedMessage \ No newline at end of file From 9d1120e0065f0b81dd636785f82defa2b0556280 Mon Sep 17 00:00:00 2001 From: Craig <3979063+craig8@users.noreply.github.com> Date: Tue, 8 Aug 2023 11:48:01 -0700 Subject: [PATCH 2/4] Update gridappsd.py --- gridappsd-python-lib/gridappsd/gridappsd.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gridappsd-python-lib/gridappsd/gridappsd.py b/gridappsd-python-lib/gridappsd/gridappsd.py index 694731b..0664149 100644 --- a/gridappsd-python-lib/gridappsd/gridappsd.py +++ b/gridappsd-python-lib/gridappsd/gridappsd.py @@ -39,7 +39,6 @@ # ------------------------------------------------------------------------------- import inspect -#import json import logging from datetime import datetime from logging import DEBUG, INFO, WARNING, FATAL, WARN From 5a3a430b7dd1a93cbe102c7318c5139128d4747e Mon Sep 17 00:00:00 2001 From: Craig <3979063+craig8@users.noreply.github.com> Date: Tue, 8 Aug 2023 11:48:58 -0700 Subject: [PATCH 3/4] Update register_app.py --- gridappsd-python-lib/gridappsd/register_app.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gridappsd-python-lib/gridappsd/register_app.py b/gridappsd-python-lib/gridappsd/register_app.py index f58feb0..7ca3da0 100644 --- a/gridappsd-python-lib/gridappsd/register_app.py +++ b/gridappsd-python-lib/gridappsd/register_app.py @@ -1,4 +1,3 @@ -#import json import logging import os import sys From a404f9b4da61bbd7f9fe9f54f2293c2ca56afca4 Mon Sep 17 00:00:00 2001 From: Tonya Martin Date: Thu, 12 Oct 2023 09:37:24 -0700 Subject: [PATCH 4/4] Update release version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index adc02bf..4c6c427 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "gridappsd-python-workspace" -version = "2023.9.0" +version = "2023.9.1" description = "A GridAPPS-D Python Adapter" authors = [ "C. Allwardt <3979063+craig8@users.noreply.github.com>",