class Split(_ExpandOp):
id = "str_split"
title = _("Split text into columns")
description = _("Split text at a separator into new columns.")
icon = "mdi.call-split"
method = "split"
class Params(base.Params):
column: str = Field(title=N_("Column"), json_schema_extra={"widget": "column"})
pat: str | None = Field(None, title=N_("Separator"), description=N_("Empty: whitespace."))
n: int = Field(-1, title=N_("Maximum splits"), ge=-1, description=N_("-1: all"))
regex: bool = Field(False, title=N_("Regular expression"))
prefix: str | None = Field(
None,
title=N_("Prefix of the new columns"),
min_length=1,
description=N_("Default: the column name and '_'."),
)
@classmethod
def call(cls, params: Params) -> tuple[list[Any], dict[str, Any]]:
args = [] if params.pat is None else [params.pat]
kwargs: dict[str, Any] = {}
if params.n != -1:
kwargs["n"] = params.n
if params.regex:
kwargs["regex"] = True
kwargs["expand"] = True
return args, kwargs
@classmethod
def params_from(
cls, column: str, args: list[Any], kwargs: dict[str, Any], prefix: str
) -> Params:
if kwargs.pop("expand", None) is not True or len(args) > 1:
msg = "split: expected expand=True"
raise ValueError(msg)
return cls.Params(
column=column,
pat=args[0] if args else None,
prefix=_prefix_param(prefix, column),
**kwargs,
)