Create a new class for a sequence of operations in the converter module.

This commit is contained in:
2026-11-14 23:29:23 -06:00
parent c2bdb8bdf9
commit 358b5a538c

View File

@@ -188,7 +188,15 @@ class Operation:
self.args = list(args)
def call(self, value: Any) -> Any:
return self.callable(value, *self.args, **self.kwargs)
try:
return self.callable(value, *self.args, **self.kwargs)
except re.PatternError as pe:
raise re.PatternError(
f"Error='{pe.msg}', "
f"value='{value}', "
f"pattern='{self.kwargs['pattern']}', "
f"repl='{self.kwargs['repl']}'"
) from pe
class Translator:
@@ -198,7 +206,10 @@ class Translator:
self.columns: list[str] = [columns]
else:
self.columns: list[str] = list(columns)
self.operations = self._get_operations(translate)
try:
self.operations = Operations(translate[YN.OPERATIONS])
except KeyError:
self.operations = []
def translate(self, frame: Frame) -> pd.Series:
# Series is a Frame for the first iteration and then is actually a
@@ -222,14 +233,12 @@ class Translator:
series = series.apply(operation.call)
return pd.Series(series)
def _get_operations(self, translate: dict[str, Any]) -> list[Operation]:
try:
operations = deepcopy(translate[YN.OPERATIONS])
except KeyError:
return []
class Operations:
def __init__(self, operations: list[dict[str, Any]] | dict[str, Any]):
if isinstance(operations, dict):
operations = [operations]
operation_list: list[Operation] = []
operations_list: list[Operation] = []
for operation in operations:
name = operation.pop(YN.OPERATION)
if YN.ARGS in operation:
@@ -240,8 +249,23 @@ class Translator:
args = []
# All the remaining items are keyword arguments.
kwargs = operation
operation_list.append(Operation(name, args, kwargs))
return operation_list
operations_list.append(Operation(name, args, kwargs))
self.operations_list = operations_list
def __getitem__(self, index):
return self.operations_list[index]
def __iter__(self):
return iter(self.operations_list)
def __len__(self):
return len(self.operations_list)
def run(self, series: pd.Series) -> pd.Series:
for operation in self.operations_list:
series = pd.Series(series.apply(operation.call))
return series
class Converter:
@@ -264,7 +288,10 @@ class Converter:
with open(spec_path) as spec_file:
spec = spec_file.read()
for key, value in substitutions.items():
spec = spec.replace(key, value)
# If the key does not already start and end with dollar signs,
# add them.
wrapped_key = f"${key.strip('$')}$"
spec = spec.replace(wrapped_key, value)
self.spec: dict[str, Any] = yaml.safe_load(spec)
jsonschema.validate(self.spec, schema)