|
1 | 1 | import os |
2 | | -from collections import ChainMap |
3 | 2 | from pathlib import Path |
4 | | -from typing import Any |
5 | 3 |
|
6 | 4 | import numpy as np |
7 | 5 | import xarray as xr |
@@ -151,48 +149,22 @@ def test_transform_full_component(): |
151 | 149 | z: "z"i array |
152 | 150 | """ |
153 | 151 | parser = make_typed_parser(grammar) |
154 | | - |
155 | | - class BlockTransformer(TypedTransformer): |
156 | | - def start(self, items: list[Any]) -> dict: |
157 | | - return ChainMap(*items) |
158 | | - |
159 | | - def block(self, items: list[Any]) -> dict: |
160 | | - return items[0] |
161 | | - |
162 | | - def options_block(self, items: list[Any]) -> dict: |
163 | | - return {"options": items[0]} |
164 | | - |
165 | | - def arrays_block(self, items: list[Any]) -> dict: |
166 | | - return {"arrays": items[0]} |
167 | | - |
168 | | - def options_vars(self, items: list[Any]) -> dict: |
169 | | - return {item[0].lower(): item[1] for item in items} |
170 | | - |
171 | | - def arrays_vars(self, items: list[Any]) -> dict: |
172 | | - return {item[0].lower(): item[1] for item in items} |
173 | | - |
174 | | - def r2d2(self, _: list[Any]) -> bool: |
175 | | - return "r2d2", True |
176 | | - |
177 | | - def b(self, items: list[Any]) -> tuple[str, str]: |
178 | | - return "b", items[0] |
179 | | - |
180 | | - def c(self, items: list[Any]) -> tuple[str, int]: |
181 | | - return "c", items[0] |
182 | | - |
183 | | - def p(self, items: list[Any]) -> tuple[str, float]: |
184 | | - return "p", items[0] |
185 | | - |
186 | | - def x(self, items: list[Any]) -> tuple[str, dict]: |
187 | | - return "x", TypedTransformer.try_create_dataarray(items[0]) |
188 | | - |
189 | | - def y(self, items: list[Any]) -> tuple[str, dict]: |
190 | | - return "y", TypedTransformer.try_create_dataarray(items[0]) |
191 | | - |
192 | | - def z(self, items: list[Any]) -> tuple[str, dict]: |
193 | | - return "z", TypedTransformer.try_create_dataarray(items[0]) |
194 | | - |
195 | | - transformer = BlockTransformer(visit_tokens=False) |
| 152 | + transformer = TypedTransformer( |
| 153 | + visit_tokens=False, |
| 154 | + dfn={ |
| 155 | + "options": { |
| 156 | + "r2d2": {"name": "r2d2", "type": "keyword"}, |
| 157 | + "b": {"name": "b", "type": "string"}, |
| 158 | + "c": {"name": "c", "type": "integer"}, |
| 159 | + "p": {"name": "p", "type": "double"}, |
| 160 | + }, |
| 161 | + "arrays": { |
| 162 | + "x": {"name": "x", "type": "double", "shape": None}, |
| 163 | + "y": {"name": "y", "type": "array", "shape": None}, |
| 164 | + "z": {"name": "z", "type": "array", "shape": None}, |
| 165 | + }, |
| 166 | + }, |
| 167 | + ) |
196 | 168 | result = transformer.transform( |
197 | 169 | parser.parse(""" |
198 | 170 | BEGIN OPTIONS |
|
0 commit comments