Parser¶
Takes a logical condition and parses it into a parse tree. Returns a tuple:
- Parse tree
- Variables used in the parse tree
- Region
In [1]:
from birdy import WPSClient
import os
from wps_tools.testing import get_target_url
In [2]:
# NBVAL_IGNORE_OUTPUT
url = get_target_url("sandpiper")
print(f"Using sandpiper on {url}")
Using sandpiper on https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/sandpiper/wps
In [3]:
sandpiper = WPSClient(url)
The list of available processes can be diplayed using the ? command (ex. bird?). Furthermore, help for individual processes can be diplayed using the same command (ex. bird.process?).
In [4]:
# NBVAL_IGNORE_OUTPUT
sandpiper.parser?
Signature: sandpiper.parser(conditions, loglevel='INFO', output_formats=None) Docstring: Process a condition into a parse tree Parameters ---------- conditions : string The conditions used to break down loglevel : {'CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'}string Logging level Returns ------- json : ComplexData:mimetype:`application/json` JSON file File: ~/code/birds/sandpiper/notebooks/</tmp/sandpiper-venv/lib/python3.8/site-packages/birdy/client/base.py-1> Type: method
sandpiper.parser expects the condition as a string.
In [5]:
conditions = [
"(temp_djf_iamean_s0p_hist <= -6)",
"(temp_djf_iamean_s100p_hist >= 5)"
]
output = sandpiper.parser(
conditions=conditions
)
# Access the json contents as a dictionary by setting asobj=True
parser_dict = output.get(asobj=True)[0]
Now that we've run the process, we check that the output is what we expect.
In [6]:
for key, value in parser_dict.items():
var_name = key.split()[0].replace("(","")
assert key in conditions
assert var_name in value["parse_tree"]
assert len(value["variables"][var_name]) == 5
assert value["region_variable"] is None