Skip to content

Commit eb2a9a8

Browse files
committed
Add truth, is, and not functions
1 parent 345ad5a commit eb2a9a8

5 files changed

Lines changed: 57 additions & 6 deletions

File tree

CHANGES

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
Changes
22
=======
33

4-
1.0b1 (2023-04-18)
4+
1.0rc1 (2023-04-18)
5+
------------------
6+
7+
- The truth, is, and not functions from operators have been added to
8+
the mapping of functions available in expressions.
9+
10+
1.0b1 (2023-04-13)
511
------------------
612

713
- Require pyparsing >= 3.0 (#29).

src/fio_planet/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

1717
"""fio_planet: Fiona CLI plugins from Planet Labs."""
1818

19-
__version__ = "1.0b1"
19+
__version__ = "1.0rc1"

src/fio_planet/features.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def __getitem__(self, key):
4747
elif key in dir(shapely):
4848
return lambda g, *args, **kwargs: getattr(shapely, key)(g, *args, **kwargs)
4949
elif key in dir(shapely.ops):
50-
return lambda g, *args, **kwargs: getattr(shapely.ops, key)(g, *args, **kwargs)
50+
return lambda g, *args, **kwargs: getattr(shapely.ops, key)(
51+
g, *args, **kwargs
52+
)
5153
else:
5254
return (
5355
lambda g, *args, **kwargs: getattr(g, key)(*args, **kwargs)

src/fio_planet/snuggs.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
ZeroOrMore,
6161
alphanums,
6262
pyparsing_common,
63+
replace_with,
6364
)
6465

6566
__all__ = ["eval"]
@@ -125,6 +126,9 @@ class ExpressionError(SyntaxError):
125126
"!=": operator.ne,
126127
">=": operator.ge,
127128
">": operator.gt,
129+
"truth": operator.truth,
130+
"is": operator.is_,
131+
"not": operator.not_,
128132
}
129133

130134

@@ -149,9 +153,9 @@ def compose(f, g):
149153
"itemgetter": operator.itemgetter,
150154
}
151155

152-
nil = Keyword("null").set_parse_action(lambda source, loc, toks: None)
153-
true = Keyword("true").set_parse_action(lambda source, loc, toks: True)
154-
false = Keyword("false").set_parse_action(lambda source, loc, toks: False)
156+
nil = Keyword("null").set_parse_action(replace_with(None))
157+
true = Keyword("true").set_parse_action(replace_with(True))
158+
false = Keyword("false").set_parse_action(replace_with(False))
155159

156160

157161
def resolve_var(source, loc, toks):

tests/test_snuggs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Python module tests
2+
#
3+
# Copyright 2023 Planet Labs PBC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Tests of the snuggs module."""
18+
19+
import pytest
20+
21+
from fio_planet import snuggs
22+
23+
24+
@pytest.mark.parametrize("arg", ["''", "null", "false", 0])
25+
def test_truth_false(arg):
26+
"""Expression is not true."""
27+
assert not snuggs.eval(f"(truth {arg})")
28+
29+
30+
@pytest.mark.parametrize("arg", ["'hi'", "true", 1])
31+
def test_truth(arg):
32+
"""Expression is true."""
33+
assert snuggs.eval(f"(truth {arg})")
34+
35+
36+
@pytest.mark.parametrize("arg", ["''", "null", "false", 0])
37+
def test_not(arg):
38+
"""Expression is true."""
39+
assert snuggs.eval(f"(not {arg})")

0 commit comments

Comments
 (0)