mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-17 15:35:21 +02:00
rm useless code in test code
This commit is contained in:
parent
5fc01b092a
commit
6ea4abe596
4 changed files with 4 additions and 87 deletions
|
|
@ -1,23 +1,7 @@
|
|||
import unittest
|
||||
import json5
|
||||
|
||||
def flatten_json_object(obj, parent_key='', sep=', '):
|
||||
if isinstance(obj, str):
|
||||
return dict([("value", obj)])
|
||||
|
||||
if isinstance(obj, list):
|
||||
return dict([("value", sep.join(str(v) for v in obj))])
|
||||
|
||||
items = []
|
||||
for key, value in obj.items():
|
||||
new_key = f"{parent_key}{sep}{key}" if parent_key else key
|
||||
if isinstance(value, dict):
|
||||
items.extend(flatten_json_object(value, new_key, sep=sep).items())
|
||||
elif isinstance(value, list):
|
||||
items.append((new_key, sep.join(str(v) for v in value)))
|
||||
else:
|
||||
items.append((new_key, value))
|
||||
return dict(items)
|
||||
from metagpt.utils.resp_parse import flatten_json_object
|
||||
|
||||
class TestFlattenJsonObject(unittest.TestCase):
|
||||
def test_flatten_json_object(self):
|
||||
|
|
|
|||
|
|
@ -1,45 +1,6 @@
|
|||
import unittest
|
||||
import json5
|
||||
import re
|
||||
|
||||
|
||||
def flatten_json_object(obj, parent_key='', sep=', '):
|
||||
if isinstance(obj, str):
|
||||
return dict([("value", obj)])
|
||||
|
||||
if isinstance(obj, list):
|
||||
return dict([("value", sep.join(str(v) for v in obj))])
|
||||
|
||||
items = []
|
||||
for key, value in obj.items():
|
||||
new_key = f"{parent_key}{sep}{key}" if parent_key else key
|
||||
if isinstance(value, dict):
|
||||
items.extend(flatten_json_object(value, new_key, sep=sep).items())
|
||||
elif isinstance(value, list):
|
||||
items.append((new_key, sep.join(str(v) for v in value)))
|
||||
else:
|
||||
items.append((new_key, value))
|
||||
return dict(items)
|
||||
|
||||
def flatten_json_structure(json_array):
|
||||
if (isinstance(json_array, list) and len(json_array) == 1 and not isinstance(json_array[0], str)):
|
||||
return flatten_json_structure(json_array[0])
|
||||
|
||||
if (isinstance(json_array, dict) and len(json_array.values()) == 1 and not isinstance(list(json_array.values())[0],
|
||||
str)):
|
||||
return flatten_json_structure(list(json_array.values())[0])
|
||||
|
||||
flattened_json_array = []
|
||||
|
||||
if (isinstance(json_array, dict)):
|
||||
json_array = json_array.values()
|
||||
|
||||
for json_object in json_array:
|
||||
flattened_dict = flatten_json_object(json_object)
|
||||
flattened_values = ", ".join(str(v) for v in flattened_dict.values())
|
||||
flattened_json_array.append(flattened_values)
|
||||
|
||||
return flattened_json_array
|
||||
from metagpt.utils.resp_parse import flatten_json_structure
|
||||
|
||||
class TestFlattenJson(unittest.TestCase):
|
||||
def test_flatten_json_structure(self):
|
||||
|
|
|
|||
|
|
@ -1,35 +1,6 @@
|
|||
import unittest
|
||||
import json5
|
||||
import re
|
||||
|
||||
def try_parse_json(input_text):
|
||||
input_text.index
|
||||
start_index_brackets = input_text.find('[')
|
||||
end_index_brackets = input_text.rfind(']')
|
||||
start_index_curly = input_text.find('{')
|
||||
end_index_curly = input_text.rfind('}')
|
||||
|
||||
start_index = start_index_brackets
|
||||
end_index = end_index_brackets
|
||||
|
||||
if (start_index_curly != -1 and (start_index_curly < start_index_brackets or start_index_brackets < 0)):
|
||||
start_index = start_index_curly
|
||||
end_index = end_index_curly
|
||||
|
||||
if start_index >= 0 and end_index > 0:
|
||||
json_string = input_text[start_index:end_index + 1]
|
||||
json_string = re.sub(r'\}[\s]*\{', '}, {', json_string)
|
||||
json_string = re.sub(r'\][\s]*\[', '], [', json_string)
|
||||
json_string = re.sub(r'"[\s]*"', '", "', json_string)
|
||||
|
||||
try:
|
||||
json_object = json5.loads(json_string)
|
||||
except ValueError:
|
||||
json_object = json5.loads(f"[{json_string}]")
|
||||
|
||||
return json_object
|
||||
|
||||
raise Exception("No JSON object found in input text.")
|
||||
from metagpt.utils.resp_parse import try_parse_json
|
||||
|
||||
|
||||
class TestTryParseJson(unittest.TestCase):
|
||||
|
|
@ -56,3 +27,4 @@ class TestTryParseJson(unittest.TestCase):
|
|||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
try_parse_json('{"a": [ jjj}')
|
||||
Loading…
Add table
Add a link
Reference in a new issue