Extend mapping operations to support reverse regex matching and external files.
This commit is contained in:
@@ -12,9 +12,9 @@ using them.
|
|||||||
import math
|
import math
|
||||||
import operator
|
import operator
|
||||||
import re
|
import re
|
||||||
from collections.abc import Callable, Collection
|
from collections.abc import Callable, Collection, Hashable
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from enum import StrEnum, auto
|
from enum import IntEnum, StrEnum, auto
|
||||||
from importlib import resources
|
from importlib import resources
|
||||||
from os import PathLike
|
from os import PathLike
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -27,22 +27,21 @@ from numpy import nan
|
|||||||
|
|
||||||
import AuDaGe
|
import AuDaGe
|
||||||
from AuDaGe import iec
|
from AuDaGe import iec
|
||||||
|
from AuDaGe.file_utilities import find_file
|
||||||
from AuDaGe.formats.controlexpert.xsy import XSYReader
|
from AuDaGe.formats.controlexpert.xsy import XSYReader
|
||||||
from AuDaGe.frame import AuDaGeFrame, Frame
|
from AuDaGe.frame import AuDaGeFrame, Frame
|
||||||
|
|
||||||
|
|
||||||
def regex_mapping(string: str, **mapping: dict[str, Any]) -> Any:
|
class MatchMode(IntEnum):
|
||||||
"""Return the first value from `mapping` whose key (interpreted as a regular
|
EXACT = auto()
|
||||||
expression) matches `string`.
|
REGEX = auto()
|
||||||
"""
|
RREGEX = auto()
|
||||||
for key, value in mapping.items():
|
|
||||||
if re.search(key, string) is not None:
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
class YAMLNodes(StrEnum):
|
class YAMLNodes(StrEnum):
|
||||||
ARG = auto()
|
ARG = auto()
|
||||||
ARGS = auto()
|
ARGS = auto()
|
||||||
|
COLUMN = auto()
|
||||||
COLUMNS = auto()
|
COLUMNS = auto()
|
||||||
CONST = auto()
|
CONST = auto()
|
||||||
DEFAULT = auto()
|
DEFAULT = auto()
|
||||||
@@ -51,10 +50,13 @@ class YAMLNodes(StrEnum):
|
|||||||
FILTER = auto()
|
FILTER = auto()
|
||||||
FORMAT = auto()
|
FORMAT = auto()
|
||||||
IMPORT = auto()
|
IMPORT = auto()
|
||||||
|
MAPPING = auto()
|
||||||
NAME = auto()
|
NAME = auto()
|
||||||
OPERATION = auto()
|
OPERATION = auto()
|
||||||
OPERATIONS = auto()
|
OPERATIONS = auto()
|
||||||
PARSE = auto()
|
PARSE = auto()
|
||||||
|
PATTERN = auto()
|
||||||
|
REPL = auto()
|
||||||
SCHEMA = "$schema"
|
SCHEMA = "$schema"
|
||||||
TRANSLATE = auto()
|
TRANSLATE = auto()
|
||||||
TYPE = auto()
|
TYPE = auto()
|
||||||
@@ -63,6 +65,46 @@ class YAMLNodes(StrEnum):
|
|||||||
YN = YAMLNodes
|
YN = YAMLNodes
|
||||||
|
|
||||||
|
|
||||||
|
def mapping(
|
||||||
|
key_string: str,
|
||||||
|
mapping: dict[str, Any] | str,
|
||||||
|
column: Hashable = None,
|
||||||
|
default: Any = None,
|
||||||
|
match_mode: MatchMode = MatchMode.EXACT,
|
||||||
|
**kwargs,
|
||||||
|
) -> Any:
|
||||||
|
"""Return the first value from `mapping` whose key (interpreted as a regular
|
||||||
|
expression) matches `key_string`.
|
||||||
|
"""
|
||||||
|
if isinstance(mapping, str):
|
||||||
|
mapping_path = find_file(mapping)
|
||||||
|
with open(mapping_path) as mapping_file:
|
||||||
|
mapping_dict = yaml.safe_load(mapping_file)
|
||||||
|
else:
|
||||||
|
mapping_dict = mapping
|
||||||
|
match match_mode:
|
||||||
|
case MatchMode.EXACT:
|
||||||
|
item = mapping_dict[key_string]
|
||||||
|
case MatchMode.REGEX:
|
||||||
|
for key, value in mapping_dict.items():
|
||||||
|
if re.search(key_string, key) is not None:
|
||||||
|
item = value
|
||||||
|
break
|
||||||
|
case MatchMode.RREGEX:
|
||||||
|
for key, value in mapping_dict.items():
|
||||||
|
if re.search(key, key_string) is not None:
|
||||||
|
item = value
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
return item[column]
|
||||||
|
except IndexError, KeyError:
|
||||||
|
# `item` is not a collection or `column` is not provided or invalid.
|
||||||
|
return item
|
||||||
|
except NameError:
|
||||||
|
# `item` was not found in `mapping`.
|
||||||
|
return default
|
||||||
|
|
||||||
|
|
||||||
class Operation:
|
class Operation:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -75,19 +117,28 @@ class Operation:
|
|||||||
# Mapping of operation names to callables and default second arguments.
|
# Mapping of operation names to callables and default second arguments.
|
||||||
operation_map: dict[str, Callable] = {
|
operation_map: dict[str, Callable] = {
|
||||||
# Mapping
|
# Mapping
|
||||||
"mapping": lambda a, b=None: self.kwargs.get(a, b),
|
"mapping": lambda value, **kwargs: mapping(
|
||||||
"regexmapping": regex_mapping,
|
value, match_mode=MatchMode.EXACT, **kwargs
|
||||||
|
),
|
||||||
|
"regexmapping": lambda value, **kwargs: mapping(
|
||||||
|
value, match_mode=MatchMode.REGEX, **kwargs
|
||||||
|
),
|
||||||
|
"rregexmapping": lambda value, **kwargs: mapping(
|
||||||
|
value, match_mode=MatchMode.RREGEX, **kwargs
|
||||||
|
),
|
||||||
# Math & Logic
|
# Math & Logic
|
||||||
"not": operator.not_,
|
"not": operator.not_,
|
||||||
"and": lambda a, b=True: a and b,
|
"and": lambda a, b=True: a and b,
|
||||||
"or": lambda a, b=True: a or b,
|
"or": lambda a, b=True: a or b,
|
||||||
"xor": lambda a, b=True: a ^ b,
|
"xor": lambda a, b=True: a ^ b,
|
||||||
"add": lambda a, b=1: a + b,
|
"add": lambda a, b=1: a + b,
|
||||||
"radd": lambda a, b=1: b + a, # for concatenating strings
|
"radd": lambda a, b=1: (
|
||||||
|
b + a
|
||||||
|
), # for concatenating strings/collections
|
||||||
"sub": lambda a, b=1: a - b,
|
"sub": lambda a, b=1: a - b,
|
||||||
"rsub": lambda a, b=1: b - a,
|
"rsub": lambda a, b=1: b - a,
|
||||||
"mul": lambda a, b=1: a * b,
|
"mul": lambda a, b=1: a * b,
|
||||||
"rmul": lambda a, b=1: b * a, # for making collections
|
"rmul": lambda a, b=1: b * a, # for making strings/collections
|
||||||
"div": lambda a, b=1: a / b,
|
"div": lambda a, b=1: a / b,
|
||||||
"rdiv": lambda a, b=1: b / a,
|
"rdiv": lambda a, b=1: b / a,
|
||||||
"mod": lambda a, b=1: a % b,
|
"mod": lambda a, b=1: a % b,
|
||||||
@@ -136,6 +187,9 @@ class Operation:
|
|||||||
self.callable = operation_map[name]
|
self.callable = operation_map[name]
|
||||||
self.args = list(args)
|
self.args = list(args)
|
||||||
|
|
||||||
|
def call(self, value: Any) -> Any:
|
||||||
|
return self.callable(value, *self.args, **self.kwargs)
|
||||||
|
|
||||||
|
|
||||||
class Translator:
|
class Translator:
|
||||||
def __init__(self, translate: dict[str, Any]):
|
def __init__(self, translate: dict[str, Any]):
|
||||||
@@ -165,13 +219,7 @@ class Translator:
|
|||||||
axis=1,
|
axis=1,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
series = series.apply(
|
series = series.apply(operation.call)
|
||||||
lambda value, operation=operation: operation.callable(
|
|
||||||
value,
|
|
||||||
*operation.args,
|
|
||||||
**operation.kwargs,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return pd.Series(series)
|
return pd.Series(series)
|
||||||
|
|
||||||
def _get_operations(self, translate: dict[str, Any]) -> list[Operation]:
|
def _get_operations(self, translate: dict[str, Any]) -> list[Operation]:
|
||||||
|
|||||||
@@ -12,8 +12,9 @@ def find_file(
|
|||||||
filepath: PathLike | Path | str, folders: list[Path] | None = None
|
filepath: PathLike | Path | str, folders: list[Path] | None = None
|
||||||
) -> Path:
|
) -> Path:
|
||||||
path = Path(filepath)
|
path = Path(filepath)
|
||||||
folders = [] if folders is None else folders
|
allfolders = (
|
||||||
allfolders = AuDaGe.folders + folders + [Path(getcwd())]
|
([] if folders is None else folders) + [Path(getcwd())] + AuDaGe.folders
|
||||||
|
)
|
||||||
for folder in allfolders:
|
for folder in allfolders:
|
||||||
fullpath = Path(folder, path).resolve()
|
fullpath = Path(folder, path).resolve()
|
||||||
if fullpath.exists():
|
if fullpath.exists():
|
||||||
|
|||||||
Reference in New Issue
Block a user