Update processor.py
Browse files- processor.py +24 -6
processor.py
CHANGED
|
@@ -221,6 +221,15 @@ class DatasetCommandCenter:
|
|
| 221 |
|
| 222 |
def _apply_projection(self, row, recipe):
|
| 223 |
new_row = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
for col_def in recipe['columns']:
|
| 225 |
t_type = col_def.get('type', 'simple')
|
| 226 |
|
|
@@ -242,23 +251,32 @@ class DatasetCommandCenter:
|
|
| 242 |
elif t_type == 'python':
|
| 243 |
# Advanced Python Eval
|
| 244 |
try:
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
context['json'] = json
|
| 248 |
-
val = eval(col_def['expression'], {}, context)
|
| 249 |
new_row[col_def['name']] = val
|
| 250 |
-
except:
|
|
|
|
| 251 |
new_row[col_def['name']] = None
|
| 252 |
|
| 253 |
return new_row
|
| 254 |
|
| 255 |
def _passes_filter(self, row, filter_str):
|
| 256 |
-
if not filter_str
|
|
|
|
| 257 |
try:
|
|
|
|
| 258 |
context = row.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
return eval(filter_str, {}, context)
|
| 260 |
except:
|
| 261 |
return False
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
|
| 264 |
def _generate_card(self, source_id, target_id, recipe, license_name):
|
|
|
|
| 221 |
|
| 222 |
def _apply_projection(self, row, recipe):
|
| 223 |
new_row = {}
|
| 224 |
+
|
| 225 |
+
# 1. Prepare Execution Context for Python Mode
|
| 226 |
+
# This allows users to write: row['text'] OR just text
|
| 227 |
+
eval_context = row.copy()
|
| 228 |
+
eval_context['row'] = row # <--- CRITICAL FIX: explicit 'row' variable
|
| 229 |
+
eval_context['json'] = json
|
| 230 |
+
import re
|
| 231 |
+
eval_context['re'] = re # Allow regex usage
|
| 232 |
+
|
| 233 |
for col_def in recipe['columns']:
|
| 234 |
t_type = col_def.get('type', 'simple')
|
| 235 |
|
|
|
|
| 251 |
elif t_type == 'python':
|
| 252 |
# Advanced Python Eval
|
| 253 |
try:
|
| 254 |
+
# Logic: eval("row['text'].upper()", globals, locals)
|
| 255 |
+
val = eval(col_def['expression'], {}, eval_context)
|
|
|
|
|
|
|
| 256 |
new_row[col_def['name']] = val
|
| 257 |
+
except Exception as e:
|
| 258 |
+
# If it fails (e.g. key error on specific row), return None
|
| 259 |
new_row[col_def['name']] = None
|
| 260 |
|
| 261 |
return new_row
|
| 262 |
|
| 263 |
def _passes_filter(self, row, filter_str):
|
| 264 |
+
if not filter_str or not filter_str.strip():
|
| 265 |
+
return True
|
| 266 |
try:
|
| 267 |
+
# Fix context here as well so filters like "len(row['text']) > 5" work
|
| 268 |
context = row.copy()
|
| 269 |
+
context['row'] = row
|
| 270 |
+
context['json'] = json
|
| 271 |
+
import re
|
| 272 |
+
context['re'] = re
|
| 273 |
+
|
| 274 |
return eval(filter_str, {}, context)
|
| 275 |
except:
|
| 276 |
return False
|
| 277 |
+
return new_row
|
| 278 |
+
|
| 279 |
+
|
| 280 |
|
| 281 |
|
| 282 |
def _generate_card(self, source_id, target_id, recipe, license_name):
|