Skip to content

Commit d8a5084

Browse files
selector can accept python None, and can accept boolean values
1 parent 7f8a93a commit d8a5084

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

bidsbuilder/interpreter/selectors.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class SelectorParser():
114114
('ADD_OP', r'[+\-]'), # Additive operators
115115
('MULT_OP', r'[*/]'), # Multiplicative operators
116116
('LOGIC_OP', r'&&|\|\|'), # logic Operators
117+
('BOOL', r'false|true'), # Bool values
117118
('ID', r'[A-Za-z_]\w*'), # Identifiers
118119
('LPAREN', r'\('), # Left paren
119120
('RPAREN', r'\)'), # Right paren
@@ -404,12 +405,12 @@ def primary(self):
404405

405406
if cur.kind == "NUMBER":
406407
val = self.match("NUMBER")
407-
return int(val)
408+
return selectorFunc(val=int(val))
408409

409410
elif cur.kind == "STRING":
410411
val = self.match("STRING")
411412
#need to trim outer " " or ' '
412-
return val[1:-1]
413+
return selectorFunc(val=val[1:-1])
413414

414415
elif cur.kind == "LPAREN":
415416
self.match("LPAREN")
@@ -432,6 +433,13 @@ def primary(self):
432433
requires_input=False,
433434
is_callable=True)
434435

436+
elif cur.kind == "BOOL":
437+
self.match("BOOL")
438+
if cur.val == "true":
439+
return selectorFunc(val=True)
440+
elif cur.val == "false":
441+
return selectorFunc(val=False)
442+
435443
elif cur.kind == "ID":
436444
self.match("ID")
437445
if cur.val in self.FIELDS_MAP.keys():
@@ -457,9 +465,10 @@ def primary(self):
457465
requires_input=False,
458466
is_callable=True)
459467

460-
elif cur.val == "null":
461-
return None
468+
elif cur.val == "null":
469+
return selectorFunc(val=None)
462470
else:
471+
raise ValueError(f"unrecognised identifier for {cur.val}")
463472
return cur.val
464473

465474
else:

testing/testSchema.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
from bidsbuilder.util.schema import parse_load_schema
2-
from bidsbuilder.interpreter.selectors import selectorHook
2+
from bidsbuilder.interpreter.selectors import selectorHook, SelectorParser
3+
34
schema = parse_load_schema(debug=True)
45

56
tests = schema.meta.expression_tests
6-
7+
passed = 0
8+
failed = 0
9+
exception = 0
710
#print(tests)
811
for i, pair in enumerate(tests):
912
expression = pair["expression"]
1013
result = pair["result"]
11-
print(type(result))
12-
print(f"pair: {pair}")
13-
#print(f"expresson: {expression}")
14-
#print(f"result: {result}")
14+
print(f"pair {i}: {pair}")
1515

16-
test = selectorHook.from_raw(expression)
17-
#tRes = selectorHook.from_raw(result)
18-
print(f"\n{test}")
16+
func = SelectorParser.from_raw(expression).parse()
17+
func.evaluate_static_nodes()
18+
t_result = SelectorParser.from_raw(result).parse()
19+
t_result.evaluate_static_nodes()
1920
try:
20-
res = test()
21-
print(res)
21+
if func() == t_result():
22+
print("test pass")
23+
passed += 1
24+
else:
25+
print("test failed")
26+
print(func)
27+
failed += 1
2228
except Exception as e:
23-
print(e)
24-
breakpoint()
29+
print("test exception")
30+
exception += 1
2531

32+
print(f"passed: {passed}")
33+
print(f"failed: {failed}")
34+
print(f"errors: {exception}")

0 commit comments

Comments
 (0)