[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gnubg] Python bindings question

From: jorma kala
Subject: Re: [Bug-gnubg] Python bindings question
Date: Wed, 10 Oct 2007 11:18:52 +0200

Thanks a lot!
that just what I need

On 10/10/07, Jim Segrave <address@hidden> wrote:
On Tue 09 Oct 2007 (20:25 +0200), jorma kala wrote:
> Hi,
> I've seen that it is possible to load and run a python script in gnubg (I'm
> using the non gui version).
> Is it also possible to run gnubg from a python program?
> For instance, the python script called batch.py that comes with the gnubg
> distribution; I've tried running it successfully from gnubg.
> But is it possible to run it as a standalone python program (I get errors
> when I try to do it)?
> Do the python bindings allow to do a batch analysis of matches, for
> instance, from a python program, without first starting gnubg?

Yes, you can run copies of gnubg from a Python program - I use it for
exactly what you describe, to analyse multiple matches. However, the
os.popen2 command is a Unix/Linux/Mac Python command, I don't know how
you do the same under Windows

The relevant block of code (which does an analysis, adds the results
to the relational databse and saves the .sgf file looks like this:

    # start a cli gnubg up, with its stdin and stdout connected to
    # this script
    (child_stdin, child_stdout) = os.popen2("/usr/local/bin/gnubg -t")
    # tell it to import a .mat file and analyse it (dg_id is the name
    # of the .mat file, for example 1193225.mat)
    child_stdin.write("import mat /tmp/%d.mat\n" % dg_id)
    child_stdin.write("analyse match\n");

    # some stuff I want in my database
    child_stdin.write("set matchinfo place %09.9d\n" % dg_id)
    child_stdin.write("set matchinfo round %d\n" % round)
    child_stdin.write("set matchinfo event %s\n" % name)

    # ask gnubg for the match stats so we can display them
    child_stdin.write("show stat mat\n")

    # add to database
    child_stdin.write("relational add mat\n")

    # occasionally gnubg asks a yes/no question here, can't remember
    # what it is

    # save the match
    child_stdin.write("save mat /home/jes/bg/%s.sgf\n" % file)

    # gnubg will have printed the match results before it sees the
    # quit command, read the lines and display them

    for l in child_stdout.readlines():
        print l,
        sys.stdout.flush ()

    for l in child_stdout.readlines():
        print l,


Jim Segrave           address@hidden

reply via email to

[Prev in Thread] Current Thread [Next in Thread]