[FEATS][ remove_whitespace_from_json,

remove_whitespace_from_yaml]
pull/362/head^2
Kye 12 months ago
parent 970da21846
commit 1b15753424

@ -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 <kye@apac.ai>"]

@ -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",
]

@ -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))
Loading…
Cancel
Save