forked from lanl/PyBNF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.py
More file actions
31 lines (25 loc) · 1.01 KB
/
Copy pathtest_parser.py
File metadata and controls
31 lines (25 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import pyparsing as pp
import re
from string import punctuation
punctuation_safe = re.sub('[:,]', '', punctuation)
def parse(s):
equals = pp.Suppress('=')
colon = pp.Suppress(':')
comment = pp.Suppress(pp.Optional(pp.Literal('#') - pp.ZeroOrMore(pp.Word(pp.printables))))
nonetoken = pp.Suppress(pp.CaselessLiteral("none"))
model_file = pp.Regex(".*?\.(bngl|xml|target)")
exp_file = pp.Regex(".*?\.(exp|con|prop)")
mdmkey = pp.CaselessLiteral("model")
mdmgram = mdmkey - equals - model_file - colon - (pp.delimitedList(exp_file) ^ nonetoken) - comment
return mdmgram.parseString(s, parseAll=True).asList()
test_lines = [
"model=examples/demo/parabola.bngl : examples/demo/par1.exp",
"model=examples/egfr_ode/egfr_ode.bngl : examples/egfr_ode/timecourse.exp, examples/egfr_ode/doseresponse.exp"
]
for line in test_lines:
try:
print(f"Testing: {line}")
res = parse(line)
print(f"Result: {res}")
except Exception as e:
print(f"Failed: {e}")