from jsonpath_ng.ext import parse import json from itertools import chain # 파일 경로 설정 project_json_path = './sample/제2502회 코딩활용능력 2급 B형 정답/project.json' scoring_json_path = './scoring.json' # JSON 파일 읽기 def read_json(file_path): with open(file_path, 'r', encoding='utf-8') as file: return json.load(file) # 요소 탐색 함수 def find_element(project_data, jsonpath_expr): jsonpath_expr = parse(jsonpath_expr) return [match.value for match in jsonpath_expr.find(project_data)] # 요소 탐색 함수 def find_script_element(project_data, jsonpath_expr): jsonpath_expr = parse(jsonpath_expr) match = jsonpath_expr.find(project_data) if not match: return None return match[0].value # jsonpath_expr_list 로 넘어온 jsonpath들을 하나씩 parse 해주고 결과를 result 리스트로 반환 def find_list_element(data, jsonpath_expr_list): result = [] for jsonpath_expr in jsonpath_expr_list: jsonpath_expr = parse(jsonpath_expr) result.append([match.value for match in jsonpath_expr.find(data)]) return result # main 함수 def main(): project_data = read_json(project_json_path) scoring_data = read_json(scoring_json_path) total_points = 0 for key, value in scoring_data.items(): ele = value.get('ele') type = value.get('type') blocks = value.get('blocks') # 정답 answer = value.get('answer') print(f"example: {key}") if type == "scene": exists = find_element(project_data, ele) if exists: print(f"elements found for {ele}") if exists == answer: total_points += value.get('points') elif exists and answer == None: total_points += value.get('points') print(f"{ele} found "); else : print(f"{exists} not found"); else: print(f"Element '{ele}' exists in project.json: {exists}") if type == "script": exists = find_script_element(project_data, ele) if exists == None: temp = None else: temp = json.loads(exists) innerKey= 1; for block in blocks: innerType = block.get('type') if temp == None: print(f"{key}-{innerKey}: Script Not exist") innerKey = innerKey + 1 continue if innerType == "list": block_exists = find_list_element(temp, block.get('ele')) else: block_exists = find_element(temp, block.get('ele')) # 정답 answer = block.get('answer', None) if isinstance(answer, list): flat_matches = list(chain.from_iterable(block_exists)) else: flat_matches = block_exists[0] # 블록에 따라 params 값이나 statements 값이 있는 경우 처리 추가 필요 if block_exists: if answer is not None and answer != flat_matches: print(f"{key}-{innerKey}: {answer} != {flat_matches}") elif answer is not None and answer == flat_matches: print(f"{key}-{innerKey}: {answer} == {flat_matches}") total_points += block.get('points') elif answer is None and block_exists: total_points += block.get('points') print(f"{key}-{innerKey}: exist ele: {block_exists}") else: print(f"No elements found for {block.get('ele')}") innerKey = innerKey + 1 print(f"Total Points: {total_points}") if __name__ == "__main__": main()