From 1b15753424cf964ea942eff146df830ad1a423d6 Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 26 Jan 2024 10:15:26 -0500 Subject: [PATCH] [FEATS][ remove_whitespace_from_json, remove_whitespace_from_yaml] --- pyproject.toml | 2 +- swarms/utils/__init__.py | 7 +++- swarms/utils/remove_json_whitespace.py | 48 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 swarms/utils/remove_json_whitespace.py diff --git a/pyproject.toml b/pyproject.toml index b7a39e00..7a45a177 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.7.9" +version = "3.8.1" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index b57f818d..7593f0aa 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -28,7 +28,10 @@ from swarms.utils.save_logs import parse_log_file ######## from swarms.utils.yaml_output_parser import YamlOutputParser from swarms.utils.json_output_parser import JsonOutputParser - +from swarms.utils.remove_json_whitespace import ( + remove_whitespace_from_json, + remove_whitespace_from_yaml, +) __all__ = [ "SubprocessCodeInterpreter", @@ -52,4 +55,6 @@ __all__ = [ "parse_log_file", "YamlOutputParser", "JsonOutputParser", + "remove_whitespace_from_json", + "remove_whitespace_from_yaml", ] diff --git a/swarms/utils/remove_json_whitespace.py b/swarms/utils/remove_json_whitespace.py new file mode 100644 index 00000000..842e04c1 --- /dev/null +++ b/swarms/utils/remove_json_whitespace.py @@ -0,0 +1,48 @@ +import json +import yaml + +def remove_whitespace_from_json(json_string: str) -> str: + """ + Removes unnecessary whitespace from a JSON string. + + This function parses the JSON string into a Python object and then + serializes it back into a JSON string without unnecessary whitespace. + + Args: + json_string (str): The JSON string. + + Returns: + str: The JSON string with whitespace removed. + """ + parsed = json.loads(json_string) + return json.dumps(parsed, separators=(',', ':')) + +# # Example usage for JSON +# json_string = '{"field1": 123, "field2": "example text"}' +# print(remove_whitespace_from_json(json_string)) + + + +def remove_whitespace_from_yaml(yaml_string: str) -> str: + """ + Removes unnecessary whitespace from a YAML string. + + This function parses the YAML string into a Python object and then + serializes it back into a YAML string with minimized whitespace. + Note: This might change the representation style of YAML data. + + Args: + yaml_string (str): The YAML string. + + Returns: + str: The YAML string with whitespace reduced. + """ + parsed = yaml.safe_load(yaml_string) + return yaml.dump(parsed, default_flow_style=True) + +# # Example usage for YAML +# yaml_string = """ +# field1: 123 +# field2: example text +# """ +# print(remove_whitespace_from_yaml(yaml_string))