forked from Vasukaman/AiParty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
55 lines (39 loc) · 1.36 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from enum import Enum
import sys
from typing import *
import re
import csv
import os
def save_txt_file(filepath:str, text:str):
with open(filepath, "w") as f:
f.write(text)
def get_folder_path(filepath:str) -> str:
# get the path of the script
script_path = os.path.abspath(filepath)
# get the folder path of the script
folder_path = os.path.dirname(script_path)
# print(f"Folder path {folder_path}")
return folder_path
def check_enum_and_return_name (variable:str, enum_class:Enum):
if variable in enum_class.__members__:
return getattr(enum_class, variable)
else:
print(f"Invalid {enum_class.__name__}: {variable}")
sys.exit()
def replace_placeholder(text:str, placeholder:str, replacement:str) -> str:
#text = text.replace("{"+placeholder+"}", replacement)
text = text.replace(placeholder, replacement)
return text
def get_placeholders(string:str) -> List[str]:
placeholders = re.findall("\{.*?\}",string)
return placeholders
def csv_to_dict_list(file_path):
dict_list = []
with open(file_path, 'r', encoding='utf-8') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
print(row)
dict_list.append(row)
for row in dict_list:
row["Content"].replace('\\n', '\n')
return dict_list