Create a new class for a sequence of operations in the converter module.
This commit is contained in:
@@ -188,7 +188,15 @@ class Operation:
|
|||||||
self.args = list(args)
|
self.args = list(args)
|
||||||
|
|
||||||
def call(self, value: Any) -> Any:
|
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:
|
class Translator:
|
||||||
@@ -198,7 +206,10 @@ class Translator:
|
|||||||
self.columns: list[str] = [columns]
|
self.columns: list[str] = [columns]
|
||||||
else:
|
else:
|
||||||
self.columns: list[str] = list(columns)
|
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:
|
def translate(self, frame: Frame) -> pd.Series:
|
||||||
# Series is a Frame for the first iteration and then is actually a
|
# Series is a Frame for the first iteration and then is actually a
|
||||||
@@ -222,14 +233,12 @@ class Translator:
|
|||||||
series = series.apply(operation.call)
|
series = series.apply(operation.call)
|
||||||
return pd.Series(series)
|
return pd.Series(series)
|
||||||
|
|
||||||
def _get_operations(self, translate: dict[str, Any]) -> list[Operation]:
|
|
||||||
try:
|
class Operations:
|
||||||
operations = deepcopy(translate[YN.OPERATIONS])
|
def __init__(self, operations: list[dict[str, Any]] | dict[str, Any]):
|
||||||
except KeyError:
|
|
||||||
return []
|
|
||||||
if isinstance(operations, dict):
|
if isinstance(operations, dict):
|
||||||
operations = [operations]
|
operations = [operations]
|
||||||
operation_list: list[Operation] = []
|
operations_list: list[Operation] = []
|
||||||
for operation in operations:
|
for operation in operations:
|
||||||
name = operation.pop(YN.OPERATION)
|
name = operation.pop(YN.OPERATION)
|
||||||
if YN.ARGS in operation:
|
if YN.ARGS in operation:
|
||||||
@@ -240,8 +249,23 @@ class Translator:
|
|||||||
args = []
|
args = []
|
||||||
# All the remaining items are keyword arguments.
|
# All the remaining items are keyword arguments.
|
||||||
kwargs = operation
|
kwargs = operation
|
||||||
operation_list.append(Operation(name, args, kwargs))
|
operations_list.append(Operation(name, args, kwargs))
|
||||||
return operation_list
|
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:
|
class Converter:
|
||||||
@@ -264,7 +288,10 @@ class Converter:
|
|||||||
with open(spec_path) as spec_file:
|
with open(spec_path) as spec_file:
|
||||||
spec = spec_file.read()
|
spec = spec_file.read()
|
||||||
for key, value in substitutions.items():
|
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)
|
self.spec: dict[str, Any] = yaml.safe_load(spec)
|
||||||
jsonschema.validate(self.spec, schema)
|
jsonschema.validate(self.spec, schema)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user