Spaces:
Runtime error
Runtime error
Roland Ding commited on
Commit ·
d8c5bc7
1
Parent(s): 4053a3e
2.2.8.25 updated the py_list_to_db_list and py_dict_to_db_map to include bool and byte.
Browse files- utility.py +21 -0
utility.py
CHANGED
|
@@ -11,6 +11,10 @@ from pdfminer.pdfdocument import PDFDocument
|
|
| 11 |
following functions are for file manipulation
|
| 12 |
'''
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# read pdf file and return text
|
| 15 |
def read_pdf(file_path):
|
| 16 |
'''
|
|
@@ -109,6 +113,10 @@ def db_map_to_py_dict(db_map):
|
|
| 109 |
py_dict[k] = int(v) if float(v)%1 ==0 else float(v)
|
| 110 |
elif l == "L":
|
| 111 |
py_dict[k] = db_list_to_py_list(v)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
else:
|
| 113 |
py_dict[k] = v
|
| 114 |
|
|
@@ -129,6 +137,10 @@ def py_dict_to_db_map(py_dict):
|
|
| 129 |
db_map[key] = {"L":py_list_to_db_list(value)}
|
| 130 |
elif type(value) is bytes:
|
| 131 |
db_map[key] = {"BS":value}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
return db_map
|
| 134 |
|
|
@@ -161,6 +173,14 @@ def py_list_to_db_list(py_list):
|
|
| 161 |
# item = py_dict_to_db_map(value)
|
| 162 |
elif type(value) is list:
|
| 163 |
item = {"L":py_list_to_db_list(value)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
db_list.append(item)
|
| 166 |
|
|
@@ -173,3 +193,4 @@ following functions are used for business logic. (to be moved to business logic
|
|
| 173 |
# function to calculate the estimated cost of the translation
|
| 174 |
def est_cost(n_tokens,rate):
|
| 175 |
return round(rate*n_tokens/1000,4)
|
|
|
|
|
|
| 11 |
following functions are for file manipulation
|
| 12 |
'''
|
| 13 |
|
| 14 |
+
keyword_search = lambda kw, text: kw.lower() in text.lower()
|
| 15 |
+
list_or = lambda l: sum(l)>0
|
| 16 |
+
list_and = lambda l: sum(l)==len(l)
|
| 17 |
+
|
| 18 |
# read pdf file and return text
|
| 19 |
def read_pdf(file_path):
|
| 20 |
'''
|
|
|
|
| 113 |
py_dict[k] = int(v) if float(v)%1 ==0 else float(v)
|
| 114 |
elif l == "L":
|
| 115 |
py_dict[k] = db_list_to_py_list(v)
|
| 116 |
+
elif l == "BS":
|
| 117 |
+
py_dict[k] = v
|
| 118 |
+
elif l == "BOOL":
|
| 119 |
+
py_dict[k] = v
|
| 120 |
else:
|
| 121 |
py_dict[k] = v
|
| 122 |
|
|
|
|
| 137 |
db_map[key] = {"L":py_list_to_db_list(value)}
|
| 138 |
elif type(value) is bytes:
|
| 139 |
db_map[key] = {"BS":value}
|
| 140 |
+
elif type(value) is bool:
|
| 141 |
+
db_map[key] = {"BOOL":value}
|
| 142 |
+
elif value is None:
|
| 143 |
+
db_map[key] = {"NULL":True}
|
| 144 |
|
| 145 |
return db_map
|
| 146 |
|
|
|
|
| 173 |
# item = py_dict_to_db_map(value)
|
| 174 |
elif type(value) is list:
|
| 175 |
item = {"L":py_list_to_db_list(value)}
|
| 176 |
+
elif type(value) is tuple:
|
| 177 |
+
item = {"L":py_list_to_db_list(value)}
|
| 178 |
+
elif type(value) is bytes:
|
| 179 |
+
item = {"BS":value}
|
| 180 |
+
elif type(value) is bool:
|
| 181 |
+
item = {"BOOL":value}
|
| 182 |
+
elif value is None:
|
| 183 |
+
item = {"NULL":True}
|
| 184 |
|
| 185 |
db_list.append(item)
|
| 186 |
|
|
|
|
| 193 |
# function to calculate the estimated cost of the translation
|
| 194 |
def est_cost(n_tokens,rate):
|
| 195 |
return round(rate*n_tokens/1000,4)
|
| 196 |
+
|