From c8faa5f516ab10f03e2711145da4c24237337e09 Mon Sep 17 00:00:00 2001 From: Berke Emrecan Arslan Date: Tue, 6 Mar 2018 17:41:06 +0300 Subject: [PATCH] Real parsers in use now --- README.md | 35 +++++++++++++++++------------------ gtester/tester.py | 21 ++++++++++++++++++--- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 36d0e19..eef0788 100644 --- a/README.md +++ b/README.md @@ -29,29 +29,28 @@ See also __Useful Resources__ which also contains link to _GTP Command Reference **Warning**: The name `gtester` conflicts with GLib unit test tool. You may want to run gtester with `python -m gtester` ### Usage - usage: gtester [-h] [-v] [--goban_args GOBAN_ARGS] [--gnugo GNUGO] [--sgf SGF] - [--clear-cache] - goban_path - - Tests your goban implementation by playing with both gnugo and your - implementation. - - positional arguments: - goban_path Path to executable of your implementation of goban + * Implement a board parser for your implementation by extending `gtester.parser.Parser` class. + * If your output is compatible with gnugo output it is not required. + * If you omit `goban_X_parser` parameter in `GobanTester` constructor, `GnuGoParser` will be used + + ```python + from gtester import GNU_GO_COMMAND + from gtester.parser import Parser + from gtester.tester import GobanTester - optional arguments: - -h, --help show this help message and exit - -v, --version show program's version number and exit - --goban_args GOBAN_ARGS - Goban arguments - --gnugo GNUGO Path to gnugo binary - --sgf SGF Test board with specific SGF file - --clear-cache Deletes downloaded games + class ExampleParser(Parser): + ... + + # GobanTester(goban_1, goban_2, goban_1_parser=None, goban_2_parser=None ... + tester = GobanTester(["example_goban", ["--mode gtp"]], GNU_GO_COMMAND, ExampleParser()) +``` Test gnugo goban implementation against itself: - python main.py gnugo --goban_args "--mode gtp" +```bash + python main.py gnugo --goban_args "--mode gtp" +``` ## Utilities diff --git a/gtester/tester.py b/gtester/tester.py index df50213..1f21f72 100644 --- a/gtester/tester.py +++ b/gtester/tester.py @@ -23,10 +23,13 @@ import gtester.sgf as sgflib from gtester.gtp import GTPMetaMachine, InvalidGTPResponse from gtester.taskmaster import start_process +from gtester.parser import GnuGoParser class GobanTester: - def __init__(self, goban_1, goban_2, sgf_file=None, + def __init__(self, goban_1, goban_2, goban_1_parser=None, + goban_2_parser=None, + sgf_file=None, sgf_str=None): self.meta = GTPMetaMachine() @@ -36,6 +39,16 @@ def __init__(self, goban_1, goban_2, sgf_file=None, self.goban_1 = self.meta.register_goban(goban_1) self.goban_2 = self.meta.register_goban(goban_2) + if goban_1_parser is not None: + self.goban_1_parser = goban_1_parser + else: + self.goban_1_parser = GnuGoParser() + + if goban_2_parser is not None: + self.goban_2_parser = goban_2_parser + else: + self.goban_2_parser = GnuGoParser() + # ==== if sgf_file is not None: self.games = [sgflib.parse_file(sgf_file)] @@ -95,14 +108,16 @@ def kill_gobans(self): for goban in [self.goban_1, self.goban_2]: self.meta.send(goban, 'quit') - @staticmethod - def _compare_boards(board_1, board_2): + def _compare_boards(self, board_1, board_2): """ Compares boards and returns different indexes :param board_1: :param board_2: :return: """ + board_1 = self.goban_1_parser.parse(board_1) + board_2 = self.goban_2_parser.parse(board_2) + board_1 = [ord(c) for c in board_1] board_2 = [ord(c) for c in board_2]