Pyteryx/alteryx_runner/tools/inout/text_input.py

36 lines
1.2 KiB
Python

from __future__ import annotations
from typing import Dict, Optional
import polars as pl
from tools.base import BaseTool
class TextInputTool(BaseTool):
def execute(self, inputs: Dict[str, pl.DataFrame]) -> Dict[str, pl.DataFrame]:
if self.config is None:
return {"Output": pl.DataFrame()}
fields = [
f.attrib["name"]
for f in self.config.findall("Fields/Field")
]
if not fields:
return {"Output": pl.DataFrame()}
rows: list[dict] = []
for r in self.config.findall("Data/r"):
cells = r.findall("c")
row: dict[str, Optional[str]] = {}
for i, col_name in enumerate(fields):
el = cells[i] if i < len(cells) else None
text: Optional[str] = el.text if el is not None else None
# Empty text in XML → NULL
row[col_name] = text if text else None
rows.append(row)
if not rows:
schema = {f: pl.String for f in fields}
return {"Output": pl.DataFrame(schema=schema)}
df = pl.DataFrame(rows, schema={f: pl.String for f in fields})
return {"Output": df}