Skip to content

Commit

Permalink
Remove Parser.add_repeated_pattern() again.
Browse files Browse the repository at this point in the history
To keep the API simple, we remove the new method and recommend to use Parser.add_function()
to parse repeated values. The docs for the method contain a simple example. The anytime
parser contains an example showing how to factor out common code into a function.
  • Loading branch information
jendrikseipp committed Jul 14, 2020
1 parent 21da7a4 commit a98a937
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
2 changes: 0 additions & 2 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ v6.1 (unreleased)

Lab
^^^
* Add :meth:`Parser.add_repeated_pattern() <lab.parser.Parser.add_repeated_pattern>`
method (Thomas Keller).
* Separate tests for Singularity and FF example experiments from other tests (Jendrik Seipp).
* Skip ``cached_revision`` doctests if ``DOWNWARD_REVISION_CACHE`` variable is not set (Jendrik Seipp).
* Add ``.github/CONTRIBUTING.md`` file (Jendrik Seipp).
Expand Down
22 changes: 21 additions & 1 deletion downward/scripts/anytime-search-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,25 @@
searches and portfolios.
"""

import re

from lab.parser import Parser


def find_all_matches(attribute, regex, type=int):
"""
Look for all occurences of *regex*, cast what is found in brackets to
*type* and store the list of found items in the properties dictionary
under *attribute*. *regex* must contain exactly one bracket group.
"""

def store_all_occurences(content, props):
matches = re.findall(regex, content)
props[attribute] = [type(m) for m in matches]

return store_all_occurences


def reduce_to_min(list_name, single_name):
def reduce_to_minimum(content, props):
values = props.get(list_name, [])
Expand All @@ -39,8 +55,12 @@ def coverage(content, props):

def main():
parser = Parser()
parser.add_repeated_pattern("cost:all", r"Plan cost: (.+)\n", type=float, flags="M")
parser.add_function(find_all_matches("cost:all", r"Plan cost: (.+)\n", type=float))
parser.add_function(
find_all_matches("steps:all", r"Plan length: (.+) step\(s\).\n", type=float)
)
parser.add_function(reduce_to_min("cost:all", "cost"))
parser.add_function(reduce_to_min("steps:all", "steps"))
parser.add_function(coverage)
parser.parse()

Expand Down
27 changes: 1 addition & 26 deletions lab/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,31 +177,6 @@ def add_pattern(
_Pattern(attribute, regex, required, type, flags)
)

def add_repeated_pattern(self, name, regex, file="run.log", type=int, flags=""):
r"""
Look for **all occurences** of *regex* in *file*, cast what is
found in brackets to *type* and store the list of found items in
the properties dictionary under *attribute*.
*regex* must contain exactly one bracket group.
*flags* must be a string of Python regular expression flags (see
https://docs.python.org/3/library/re.html). E.g., ``flags="I"``
makes the matching case-insensitive.
>>> parser = Parser()
>>> parser.add_repeated_pattern("all_plan_costs", r"Plan cost: (\d+)\n")
>>> parser.add_repeated_pattern("all_f_values", r"f=(\d+)\n")
"""
flags = _get_pattern_flags(flags)

def find_all_occurences(content, props):
matches = re.findall(regex, content, flags=flags)
props[name] = [type(m) for m in matches]

self.add_function(find_all_occurences, file=file)

def add_function(self, function, file="run.log"):
r"""Call ``function(open(file).read(), properties)`` during parsing.
Expand All @@ -218,7 +193,7 @@ def add_function(self, function, file="run.log"):
>>> from lab.parser import Parser
>>> def parse_states_over_time(content, props):
... matches = re.findall(r"(.+)s: (\d+) states\n", content)
... props["states_over_time"] = [(int(t), int(s)) for t, s in matches]
... props["states_over_time"] = [(float(t), int(s)) for t, s in matches]
...
>>> parser = Parser()
>>> parser.add_function(parse_states_over_time)
Expand Down

0 comments on commit a98a937

Please sign in to comment.