file read write utils

This commit is contained in:
yzlin 2024-02-01 20:25:53 +08:00
parent e7ae79b2cb
commit 4cd09e703c
3 changed files with 9 additions and 13 deletions

View file

@ -485,13 +485,13 @@ def read_json_file(json_file: str, encoding="utf-8") -> list[Any]:
return data
def write_json_file(json_file: str, data: list, encoding=None):
def write_json_file(json_file: str, data: list, encoding: str = None, indent: int = 4):
folder_path = Path(json_file).parent
if not folder_path.exists():
folder_path.mkdir(parents=True, exist_ok=True)
with open(json_file, "w", encoding=encoding) as fout:
json.dump(data, fout, ensure_ascii=False, indent=4, default=to_jsonable_python)
json.dump(data, fout, ensure_ascii=False, indent=indent, default=to_jsonable_python)
def import_class(class_name: str, module_name: str) -> type:

View file

@ -2,12 +2,12 @@
# @Date : 12/12/2023 4:14 PM
# @Author : stellahong (stellahong@fuzhi.ai)
# @Desc :
import json
import os
import nbformat
from metagpt.const import DATA_PATH
from metagpt.utils.common import write_json_file
def save_code_file(name: str, code_context: str, file_format: str = "py") -> None:
@ -33,8 +33,7 @@ def save_code_file(name: str, code_context: str, file_format: str = "py") -> Non
elif file_format == "json":
# Parse the code content as JSON and save
data = {"code": code_context}
with open(file_path, "w", encoding="utf-8") as fp:
json.dump(data, fp, indent=2)
write_json_file(file_path, data, encoding="utf-8", indent=2)
elif file_format == "ipynb":
nbformat.write(code_context, file_path)
else: