Coverage for python/lsst/ctrl/mpexec/dataid_match.py: 32%
60 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-01 02:08 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-01 02:08 -0700
1# This file is part of ctrl_mpexec.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22__all__ = ["DataIdMatch"]
24import operator
25from typing import Any, Callable, List, Optional, Tuple
27import astropy.time
28from lsst.daf.butler import DataId
29from lsst.daf.butler.registry.queries.expressions.parser import Node, ParserYacc, TreeVisitor # type: ignore
32class _DataIdMatchTreeVisitor(TreeVisitor):
33 """Expression tree visitor which evaluates expression using values from
34 DataId.
35 """
37 def __init__(self, dataId: DataId):
38 self.dataId = dataId
40 def visitNumericLiteral(self, value: str, node: Node) -> Any:
41 # docstring is inherited from base class
42 try:
43 return int(value)
44 except ValueError:
45 return float(value)
47 def visitStringLiteral(self, value: str, node: Node) -> Any:
48 # docstring is inherited from base class
49 return value
51 def visitTimeLiteral(self, value: astropy.time.Time, node: Node) -> Any:
52 # docstring is inherited from base class
53 return value
55 def visitRangeLiteral(self, start: int, stop: int, stride: Optional[int], node: Node) -> Any:
56 # docstring is inherited from base class
57 if stride is None:
58 return range(start, stop + 1)
59 else:
60 return range(start, stop + 1, stride)
62 def visitIdentifier(self, name: str, node: Node) -> Any:
63 # docstring is inherited from base class
64 return self.dataId[name]
66 def visitUnaryOp(self, operator_name: str, operand: Any, node: Node) -> Any:
67 # docstring is inherited from base class
68 operators: dict[str, Callable[[Any], Any]] = {
69 "NOT": operator.not_,
70 "+": operator.pos,
71 "-": operator.neg,
72 }
73 return operators[operator_name](operand)
75 def visitBinaryOp(self, operator_name: str, lhs: Any, rhs: Any, node: Node) -> Any:
76 # docstring is inherited from base class
77 operators = {
78 "OR": operator.or_,
79 "AND": operator.and_,
80 "+": operator.add,
81 "-": operator.sub,
82 "*": operator.mul,
83 "/": operator.truediv,
84 "%": operator.mod,
85 "=": operator.eq,
86 "!=": operator.ne,
87 "<": operator.lt,
88 ">": operator.gt,
89 "<=": operator.le,
90 ">=": operator.ge,
91 }
92 return operators[operator_name](lhs, rhs)
94 def visitIsIn(self, lhs: Any, values: List[Any], not_in: bool, node: Node) -> Any:
95 # docstring is inherited from base class
96 is_in = True
97 for value in values:
98 if not isinstance(value, range):
99 value = [value]
100 if lhs in value:
101 break
102 else:
103 is_in = False
104 if not_in:
105 is_in = not is_in
106 return is_in
108 def visitParens(self, expression: Any, node: Node) -> Any:
109 # docstring is inherited from base class
110 return expression
112 def visitTupleNode(self, items: Tuple[Any, ...], node: Node) -> Any:
113 # docstring is inherited from base class
114 raise NotImplementedError()
116 def visitFunctionCall(self, name: str, args: List[Any], node: Node) -> Any:
117 # docstring is inherited from base class
118 raise NotImplementedError()
120 def visitPointNode(self, ra: Any, dec: Any, node: Node) -> Any:
121 # docstring is inherited from base class
122 raise NotImplementedError()
125class DataIdMatch:
126 """Class that can match DataId against the user-defined string expression.
128 Parameters
129 ----------
130 expression : `str`
131 User-defined expression, supports syntax defined by daf_butler
132 expression parser. Maps identifiers in the expression to the values of
133 DataId.
134 """
136 def __init__(self, expression: str):
137 parser = ParserYacc()
138 self.expression = expression
139 self.tree = parser.parse(expression)
141 def match(self, dataId: DataId) -> bool:
142 """Matches DataId contents against the expression.
144 Parameters
145 ----------
146 dataId : `DataId`
147 DataId that is matched against an expression.
149 Returns
150 -------
151 match : `bool`
152 Result of expression evaluation.
154 Raises
155 ------
156 KeyError
157 Raised when identifier in expression is not defined for given
158 `DataId`.
159 TypeError
160 Raised when expression evaluates to a non-boolean type or when
161 operation in expression cannot be performed on operand types.
162 NotImplementedError
163 Raised when expression includes valid but unsupported syntax, e.g.
164 function call.
165 """
166 visitor = _DataIdMatchTreeVisitor(dataId)
167 result = self.tree.visit(visitor)
168 if not isinstance(result, bool):
169 raise TypeError(f"Expression '{self.expression}' returned non-boolean object {type(result)}")
170 return result