adjust code style to follow PEP8
This commit is contained in:
parent
3edd920b0d
commit
40eaca47ae
3 changed files with 68 additions and 32 deletions
|
@ -1,13 +1,16 @@
|
|||
from arpeggio.cleanpeg import ParserPEG
|
||||
from typing import List, Tuple, Sequence, Optional, Union
|
||||
from typing import List, Union
|
||||
import arpeggio
|
||||
|
||||
from . import grammar, ParseError, JobShopProblem, JobShopVisitor
|
||||
from . import grammar, JobShopProblem, JobShopVisitor
|
||||
|
||||
|
||||
class JobShop1Visitor(JobShopVisitor):
|
||||
"""instanciated for semantic analysis of parse_tree"""
|
||||
|
||||
def visit_problem_instance(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> JobShopProblem:
|
||||
def visit_problem_instance(
|
||||
self, node: arpeggio.ParseTreeNode,
|
||||
children: arpeggio.SemanticActionResults) -> JobShopProblem:
|
||||
if self.debug:
|
||||
print("problem_instance\nchildren:", children)
|
||||
problem: JobShopProblem = children[3]
|
||||
|
@ -15,30 +18,40 @@ class JobShop1Visitor(JobShopVisitor):
|
|||
problem.description = children[2]
|
||||
return problem
|
||||
|
||||
def visit_instance_list(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> List[JobShopProblem]:
|
||||
def visit_instance_list(
|
||||
self, node: arpeggio.ParseTreeNode,
|
||||
children: arpeggio.SemanticActionResults) -> List[JobShopProblem]:
|
||||
if self.debug:
|
||||
print("instance_list\nchildren:", children)
|
||||
return list(filter(lambda x: isinstance(x, JobShopProblem), children))
|
||||
|
||||
def visit_skip_preface(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> List[JobShopProblem]:
|
||||
def visit_skip_preface(
|
||||
self, node: arpeggio.ParseTreeNode,
|
||||
children: arpeggio.SemanticActionResults) -> List[JobShopProblem]:
|
||||
if self.debug:
|
||||
print("skip_preface\nchildren:", children)
|
||||
return list(filter(lambda x: type(x) is list, children))[0]
|
||||
|
||||
# ignore eol nodes
|
||||
def visit_eol(self, node:arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> None:
|
||||
def visit_eol(
|
||||
self, node: arpeggio.ParseTreeNode,
|
||||
children: arpeggio.SemanticActionResults) -> None:
|
||||
return None
|
||||
|
||||
|
||||
# ignore trim_ws nodes
|
||||
def visit_trim_ws(self, node:arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> None:
|
||||
def visit_trim_ws(
|
||||
self, node: arpeggio.ParseTreeNode,
|
||||
children: arpeggio.SemanticActionResults) -> None:
|
||||
return None
|
||||
|
||||
|
||||
def parse_string(inputdata: str) -> JobShopProblem:
|
||||
"""parse string with jobshop1-formatted data (multiple problem instances)
|
||||
and return list of JobShopProblem s"""
|
||||
parse_tree = parser.parse(inputdata)
|
||||
return arpeggio.visit_parse_tree(parse_tree, JobShop1Visitor())
|
||||
|
||||
|
||||
def parse_file(filename: Union[str, bytes]) -> List[JobShopProblem]:
|
||||
"""Open file with jobshop1-formatted data (multiple problem instances),
|
||||
parse it and return list of JobShopProblem s"""
|
||||
|
@ -46,11 +59,12 @@ def parse_file(filename: Union[str, bytes]) -> List[JobShopProblem]:
|
|||
with open(filename) as datafile:
|
||||
inputdata: str = datafile.read()
|
||||
return parse_string(inputdata)
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
print(parse_file("../inputdata/jobshop1.txt"))
|
||||
|
||||
|
||||
parser = ParserPEG(grammar, "job_shop1", skipws=False)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue