Files
NexQuant/rdagent/components/coder/factor_coder/utils.py
T

52 lines
1.7 KiB
Python
Raw Normal View History

2024-05-21 22:48:41 +08:00
from pathlib import Path
import pandas as pd
# render it with jinja
from jinja2 import Environment, StrictUndefined
2024-06-14 12:59:44 +08:00
2024-07-05 17:42:00 +08:00
from rdagent.components.coder.factor_coder.config import FACTOR_IMPLEMENT_SETTINGS
2024-05-21 22:48:41 +08:00
TPL = """
{{file_name}}
```{{type_desc}}
{{content}}
````
"""
# Create a Jinja template from the string
JJ_TPL = Environment(undefined=StrictUndefined).from_string(TPL)
2024-05-21 22:48:41 +08:00
2024-05-21 22:48:41 +08:00
def get_data_folder_intro():
"""Directly get the info of the data folder.
2024-05-21 22:48:41 +08:00
It is for preparing prompting message.
"""
content_l = []
for p in Path(FACTOR_IMPLEMENT_SETTINGS.data_folder_debug).iterdir():
2024-05-21 22:48:41 +08:00
if p.name.endswith(".h5"):
df = pd.read_hdf(p)
# get df.head() as string with full width
pd.set_option("display.max_columns", None) # or 1000
pd.set_option("display.max_rows", None) # or 1000
pd.set_option("display.max_colwidth", None) # or 199
rendered = JJ_TPL.render(
file_name=p.name,
type_desc="generated by `pd.read_hdf(filename).head()`",
content=df.head().to_string(),
)
content_l.append(rendered)
elif p.name.endswith(".md"):
with open(p) as f:
content = f.read()
rendered = JJ_TPL.render(
file_name=p.name,
type_desc="markdown",
content=content,
)
content_l.append(rendered)
else:
raise NotImplementedError(
f"file type {p.name} is not supported. Please implement its description function.",
)
return "\n ----------------- file splitter -------------\n".join(content_l)