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 operator
|
||||
import re
|
||||
from collections.abc import Callable, Collection
|
||||
from collections.abc import Callable, Collection, Hashable
|
||||
from copy import deepcopy
|
||||
from enum import StrEnum, auto
|
||||
from enum import IntEnum, StrEnum, auto
|
||||
from importlib import resources
|
||||
from os import PathLike
|
||||
from pathlib import Path
|
||||
@@ -27,22 +27,21 @@ from numpy import nan
|
||||
|
||||
import AuDaGe
|
||||
from AuDaGe import iec
|
||||
from AuDaGe.file_utilities import find_file
|
||||
from AuDaGe.formats.controlexpert.xsy import XSYReader
|
||||
from AuDaGe.frame import AuDaGeFrame, Frame
|
||||
|
||||
|
||||
def regex_mapping(string: str, **mapping: dict[str, Any]) -> Any:
|
||||
"""Return the first value from `mapping` whose key (interpreted as a regular
|
||||
expression) matches `string`.
|
||||
"""
|
||||
for key, value in mapping.items():
|
||||
if re.search(key, string) is not None:
|
||||
return value
|
||||
class MatchMode(IntEnum):
|
||||
EXACT = auto()
|
||||
REGEX = auto()
|
||||
RREGEX = auto()
|
||||
|
||||
|
||||
class YAMLNodes(StrEnum):
|
||||
ARG = auto()
|
||||
ARGS = auto()
|
||||
COLUMN = auto()
|
||||
COLUMNS = auto()
|
||||
CONST = auto()
|
||||
DEFAULT = auto()
|
||||
@@ -51,10 +50,13 @@ class YAMLNodes(StrEnum):
|
||||
FILTER = auto()
|
||||
FORMAT = auto()
|
||||
IMPORT = auto()
|
||||
MAPPING = auto()
|
||||
NAME = auto()
|
||||
OPERATION = auto()
|
||||
OPERATIONS = auto()
|
||||
PARSE = auto()
|
||||
PATTERN = auto()
|
||||
REPL = auto()
|
||||
SCHEMA = "$schema"
|
||||
TRANSLATE = auto()
|
||||
TYPE = auto()
|
||||
@@ -63,6 +65,46 @@ class YAMLNodes(StrEnum):
|
||||
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:
|
||||
def __init__(
|
||||
self,
|
||||
@@ -75,19 +117,28 @@ class Operation:
|
||||
# Mapping of operation names to callables and default second arguments.
|
||||
operation_map: dict[str, Callable] = {
|
||||
# Mapping
|
||||
"mapping": lambda a, b=None: self.kwargs.get(a, b),
|
||||
"regexmapping": regex_mapping,
|
||||
"mapping": lambda value, **kwargs: 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
|
||||
"not": operator.not_,
|
||||
"and": lambda a, b=True: a and b,
|
||||
"or": lambda a, b=True: a or b,
|
||||
"xor": lambda a, b=True: 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,
|
||||
"rsub": lambda a, b=1: b - a,
|
||||
"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,
|
||||
"rdiv": lambda a, b=1: b / a,
|
||||
"mod": lambda a, b=1: a % b,
|
||||
@@ -136,6 +187,9 @@ class Operation:
|
||||
self.callable = operation_map[name]
|
||||
self.args = list(args)
|
||||
|
||||
def call(self, value: Any) -> Any:
|
||||
return self.callable(value, *self.args, **self.kwargs)
|
||||
|
||||
|
||||
class Translator:
|
||||
def __init__(self, translate: dict[str, Any]):
|
||||
@@ -165,13 +219,7 @@ class Translator:
|
||||
axis=1,
|
||||
)
|
||||
continue
|
||||
series = series.apply(
|
||||
lambda value, operation=operation: operation.callable(
|
||||
value,
|
||||
*operation.args,
|
||||
**operation.kwargs,
|
||||
)
|
||||
)
|
||||
series = series.apply(operation.call)
|
||||
return pd.Series(series)
|
||||
|
||||
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
|
||||
) -> Path:
|
||||
path = Path(filepath)
|
||||
folders = [] if folders is None else folders
|
||||
allfolders = AuDaGe.folders + folders + [Path(getcwd())]
|
||||
allfolders = (
|
||||
([] if folders is None else folders) + [Path(getcwd())] + AuDaGe.folders
|
||||
)
|
||||
for folder in allfolders:
|
||||
fullpath = Path(folder, path).resolve()
|
||||
if fullpath.exists():
|
||||
|
||||
Reference in New Issue
Block a user