Created
September 5, 2013 12:08
-
-
Save btel/6449281 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import subprocess | |
| import sys | |
| import time | |
| import argparse | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("team1") | |
| parser.add_argument("team2") | |
| parser.add_argument("--n-runs", "-n", type=int) | |
| parser.add_argument("--n-procs", "-p", type=int) | |
| args = parser.parse_args() | |
| left_team = args.team1 | |
| right_team = args.team2 | |
| n_runs = args.n_runs | |
| n_procs = args.n_procs | |
| out_mapping = {'0': 0, '1': 0, '-': 0} | |
| running_processes = [] | |
| def start_games(): | |
| global n_runs | |
| while (len(running_processes) < n_procs) and n_runs>0: | |
| proc = subprocess.Popen(['./pelitagame', left_team, right_team, | |
| '--null', '--parseable-output'], | |
| stdout=subprocess.PIPE) | |
| running_processes.append(proc) | |
| n_runs -= 1 | |
| def update_results(p): | |
| for l in p.stdout: | |
| result = l | |
| out_mapping[result.strip()]+=1 | |
| def check_if_finished(): | |
| for p in running_processes: | |
| if p.poll() is not None: | |
| running_processes.remove(p) | |
| update_results(p) | |
| start_games() | |
| start_games() | |
| while (n_runs > 0) or len(running_processes)>0: | |
| sys.stdout.write("\r{}/{} runs left ".format( | |
| n_runs + len(running_processes), args.n_runs)) | |
| sys.stdout.flush() | |
| check_if_finished() | |
| time.sleep(1) | |
| print('\r{} wins: {}'.format(left_team, out_mapping['0'])) | |
| print('{} wins: {}'.format(right_team, out_mapping['1'])) | |
| print('Draws: {}'.format(out_mapping['-'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment