bug-gnubg
[Top][All Lists]
Advanced

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

Re: [Bug-gnubg] Python bindings question


From: Jim Segrave
Subject: Re: [Bug-gnubg] Python bindings question
Date: Wed, 10 Oct 2007 07:26:05 +0200
User-agent: Mutt/1.5.13 (2006-08-11)

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
    child_stdin.write("y\n")
    
    # save the match
    child_stdin.write("save mat /home/jes/bg/%s.sgf\n" % file)
    child_stdin.write("quit\n")
    child_stdin.close()

    # 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,
        sys.stdout.flush()
        
    child_stdout.close()


-- 
Jim Segrave           address@hidden






reply via email to

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