MCPcopy Create free account
hub / github.com/InternScience/SciReason / format_load

Function format_load

opencompass/datasets/teval/utils/format_load.py:3–44  ·  view source on GitHub ↗

Format the raw data into the format that can be evaluated. Args: raw_data (str): The raw data. start_character (str, optional): The start character. Defaults to '', if using it, the string will be sliced from the first start_character. end_character (str, optional): The

(raw_data: str, start_character: str = '', end_character: str = '')

Source from the content-addressed store, hash-verified

1import ast
2import json
3def format_load(raw_data: str, start_character: str = '', end_character: str = ''):
4 """Format the raw data into the format that can be evaluated.
5
6 Args:
7 raw_data (str): The raw data.
8 start_character (str, optional): The start character. Defaults to '', if using it, the string will be sliced from the first start_character.
9 end_character (str, optional): The end character. Defaults to '', if using it, the string will be sliced to the last end_character.
10
11 Returns:
12 str: The formatted data.
13 """
14 if type(raw_data) != str:
15 # the data has been evaluated
16 return raw_data
17 if "```json" in raw_data:
18 raw_data = raw_data[raw_data.find("```json") + len("```json"):]
19 raw_data = raw_data.strip("`")
20 if start_character != '':
21 raw_data = raw_data[raw_data.find(start_character):]
22 if end_character != '':
23 raw_data = raw_data[:raw_data.rfind(end_character) + len(end_character)]
24 successful_parse = False
25 try:
26 data = ast.literal_eval(raw_data)
27 successful_parse = True
28 except Exception as e:
29 pass
30 try:
31 if not successful_parse:
32 data = json.loads(raw_data)
33 successful_parse = True
34 except Exception as e:
35 pass
36 try:
37 if not successful_parse:
38 data = json.loads(raw_data.replace("\'", "\""))
39 successful_parse = True
40 except Exception as e:
41 pass
42 if not successful_parse:
43 raise Exception("Cannot parse raw data")
44 return data

Callers 4

format_loadMethod · 0.90
format_loadMethod · 0.90
json_format_parseMethod · 0.90
json_format_parseMethod · 0.90

Calls 1

replaceMethod · 0.80

Tested by

no test coverage detected