def main(): parser = argparse.ArgumentParser() parser.add_argument('--input', '-i', help="Input file, default: stdin.", default=None, type=str, dest='fname') args = parser.parse_args() fname = args.fname tw = TreeWalker(action, Ctx) tree = loadfile(fname) tw.walktree(tree)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--input', '-i', help="Input file, default: stdin.", default=None, type=str, dest='fname') args = parser.parse_args() if args.fname: args.title = 'Tree analysis for ' + args.fname else: args.title = 'Tree analysis process ' + str(os.getpid()) legend = '(\\$: problem size <= possible scores, \\+: optimal split, \\*: root code not a solution.)' title = args.title fname = args.fname global PFXGEN PFXGEN = PrefixGen() PFXGEN.skip_non_reducing = True tw = TreeWalker(action, Ctx) tree = loadfile(fname) tw.walktree(tree) summary = Summary(tree) global PRINTED_PFX_LEN PRINTED_PFX_LEN = (len(str(CODETABLE.CODES[0]))+2) * MAX_PFX_LEN + 2 columns = ( ('prefix', PRINTED_PFX_LEN, 'r'), ('flags', max(len('flags'), 6), 'l'), ('#children', max(len('#children'), 6), 'l'), ('problem size', max(len('problem size'), 6), 'l'), ('max subproblem', max(len('max subproblem'), 6), 'l'), ('#preserving', max(len('#preserving'), 6), 'l'), ('#distinct/in', max(len('#distinct/in'), 4), 'l'), ('#distinct/all', max(len('#distinct/all'), 4), 'l')) separator = ' '.join(map(lambda c: '=' * c[1], columns)) header = ' '.join(map(lambda c: pad(c[0], c), columns)) formats = ("%s", "%s", "%5d", "%5d", "%5d", "%6d", "%4d", "%4d") locale.setlocale(locale.LC_ALL, 'en_US') print title print '=' * len(title) print "\nAverage game: {:.4f} moves, Longest game: {} moves\n\n".format( summary.mean_game(), locale.format("%d", summary.longest_game(), grouping=True)) print """.. table:: Prefix properties of a game tree %s :widths: %s :column-wrapping: %s :column-alignment: %s :column-dividers: %s """ % (legend, ('1 ' * len(columns)), ('false' + ' true' * len(columns[1:])), ('left' + ' right' * len(columns[1:])), 'single' + ' single' * len(columns)) print ' ' + separator print ' ' + header print ' ' + separator for r in sorted(RECORDS, lambda a, b: cmp(len(a[0]), len(b[0])) or cmp(a[0], b[0]) or cmp(b[4], a[4])): (pfx, insoln, optimal, nchildren, psize, maxprob, ninv, nd_in, nd_all) = r fields = (format_prefix(pfx), ''.join((('\\$' if psize <= CODETABLE.NSCORES else ''), ('' if insoln else '\\*'), ('\\+' if optimal else ''))), nchildren, psize, maxprob, ninv, nd_in, nd_all) printable = map(lambda i: locale.format(i[0], i[1], grouping=True), zip(formats, fields)) printable = map(lambda i: pad(i[0], i[1]), zip(printable, columns)) print ' ' + (' '.join(printable)) print ' ' + separator