afscgap.query_util

(c) 2023 Regents of University of California / The Eric and Wendy Schmidt Center for Data Science and the Environment at UC Berkeley.

 1"""
 2(c) 2023 Regents of University of California / The Eric and Wendy Schmidt Center
 3for Data Science and the Environment at UC Berkeley.
 4"""
 5import numbers
 6
 7
 8def interpret_query_to_ords(target: dict) -> dict:
 9    """Convert a description of a query to ORDS syntax.
10
11    Args:
12        target: The "native Python structures" version of the query.
13
14    Returns:
15        Dicitionary encoding the ORDS-expected query format.
16    """
17    target_items = target.items()
18
19    def interpret_value(value):
20        if value is None:
21            return None
22
23        if not (isinstance(value, tuple) or isinstance(value, list)):
24            return value
25
26        if len(value) != 2:
27            raise RuntimeError('Range param given without 2 elements.')
28
29        lower_limit_given = value[0] is not None
30        upper_limit_given = value[1] is not None
31        lower_limit_infinity = not lower_limit_given
32        upper_limit_infinity = not upper_limit_given
33
34        if lower_limit_infinity and upper_limit_infinity:
35            return None
36        elif lower_limit_infinity:
37            return {"$lte": value[1]}
38        elif upper_limit_infinity:
39            return {"$gte": value[0]}
40        else:
41            return {"$between": value}
42
43    target_transform_items = map(
44        lambda x: (x[0], interpret_value(x[1])),
45        target_items
46    )
47    return dict(target_transform_items)
48
49
50def interpret_query_to_py(target: dict) -> dict:
51    """Emulate a query in Python.
52
53    Args:
54        target: The "native Python structures" format of the query.
55
56    Returns:
57        Dictionary mapping from key to function which returns if a candidate
58        value for that field satisfies the criteria described for that field.
59    """
60    target_items = target.items()
61
62    def interpret_value(value):
63        if value is None:
64            return None
65
66        if isinstance(value, dict):
67            raise RuntimeError('No ORDS params for presence_only=False.')
68
69        if isinstance(value, numbers.Number):
70            return lambda x: abs(x - value) < 0.00001
71
72        if not isinstance(value, tuple):
73            return lambda x: x == value
74
75        if len(value) != 2:
76            raise RuntimeError('Range param given without 2 elements.')
77
78        lower_limit_given = value[0] is not None
79        upper_limit_given = value[1] is not None
80        lower_limit_infinity = not lower_limit_given
81        upper_limit_infinity = not upper_limit_given
82
83        if lower_limit_infinity and upper_limit_infinity:
84            return lambda x: True
85        elif lower_limit_infinity:
86            return lambda x: x <= value[1]
87        elif upper_limit_infinity:
88            return lambda x: x >= value[0]
89        else:
90            return lambda x: x >= value[0] and x <= value[1]
91
92    target_transform_items = map(
93        lambda x: (x[0], interpret_value(x[1])),
94        target_items
95    )
96    return dict(target_transform_items)
def interpret_query_to_ords(target: dict) -> dict:
 9def interpret_query_to_ords(target: dict) -> dict:
10    """Convert a description of a query to ORDS syntax.
11
12    Args:
13        target: The "native Python structures" version of the query.
14
15    Returns:
16        Dicitionary encoding the ORDS-expected query format.
17    """
18    target_items = target.items()
19
20    def interpret_value(value):
21        if value is None:
22            return None
23
24        if not (isinstance(value, tuple) or isinstance(value, list)):
25            return value
26
27        if len(value) != 2:
28            raise RuntimeError('Range param given without 2 elements.')
29
30        lower_limit_given = value[0] is not None
31        upper_limit_given = value[1] is not None
32        lower_limit_infinity = not lower_limit_given
33        upper_limit_infinity = not upper_limit_given
34
35        if lower_limit_infinity and upper_limit_infinity:
36            return None
37        elif lower_limit_infinity:
38            return {"$lte": value[1]}
39        elif upper_limit_infinity:
40            return {"$gte": value[0]}
41        else:
42            return {"$between": value}
43
44    target_transform_items = map(
45        lambda x: (x[0], interpret_value(x[1])),
46        target_items
47    )
48    return dict(target_transform_items)

Convert a description of a query to ORDS syntax.

Arguments:
  • target: The "native Python structures" version of the query.
Returns:

Dicitionary encoding the ORDS-expected query format.

def interpret_query_to_py(target: dict) -> dict:
51def interpret_query_to_py(target: dict) -> dict:
52    """Emulate a query in Python.
53
54    Args:
55        target: The "native Python structures" format of the query.
56
57    Returns:
58        Dictionary mapping from key to function which returns if a candidate
59        value for that field satisfies the criteria described for that field.
60    """
61    target_items = target.items()
62
63    def interpret_value(value):
64        if value is None:
65            return None
66
67        if isinstance(value, dict):
68            raise RuntimeError('No ORDS params for presence_only=False.')
69
70        if isinstance(value, numbers.Number):
71            return lambda x: abs(x - value) < 0.00001
72
73        if not isinstance(value, tuple):
74            return lambda x: x == value
75
76        if len(value) != 2:
77            raise RuntimeError('Range param given without 2 elements.')
78
79        lower_limit_given = value[0] is not None
80        upper_limit_given = value[1] is not None
81        lower_limit_infinity = not lower_limit_given
82        upper_limit_infinity = not upper_limit_given
83
84        if lower_limit_infinity and upper_limit_infinity:
85            return lambda x: True
86        elif lower_limit_infinity:
87            return lambda x: x <= value[1]
88        elif upper_limit_infinity:
89            return lambda x: x >= value[0]
90        else:
91            return lambda x: x >= value[0] and x <= value[1]
92
93    target_transform_items = map(
94        lambda x: (x[0], interpret_value(x[1])),
95        target_items
96    )
97    return dict(target_transform_items)

Emulate a query in Python.

Arguments:
  • target: The "native Python structures" format of the query.
Returns:

Dictionary mapping from key to function which returns if a candidate value for that field satisfies the criteria described for that field.