Example #1
0
parser = PrologParser()
for pl_fn in args:

    linecnt = 0
    with codecs.open(pl_fn, encoding='utf-8', errors='ignore', mode='r') as f:
        while f.readline():
            linecnt += 1
    print "%s: %d lines." % (pl_fn, linecnt)

    nlp_macros      = {}
    src             = None
    nlp_test_engine = PrologAIEngine(db)
    nlp_test_engine.set_trace(options.trace)

    with open (pl_fn, 'r') as f:
        parser.start(f, pl_fn)

        while parser.cur_sym != SYM_EOF:
            clauses = parser.clause()

            if first:
                if not parser.module:
                    raise Exception ("No module name given!")

                db.clear_module(parser.module)
                db.store_module_requirements(parser.module, parser.requirements)

                first = False

            for clause in clauses:
                print u"%7d / %7d (%3d%%) > %s" % (parser.cur_line, linecnt, parser.cur_line * 100 / linecnt, unicode(clause))
Example #2
0
#
# main
#


parser = PrologParser()

with codecs.open(outputfn, 'w', 'utf8') as outf:

    outf.write('% prolog\n\n')

    outf.write("train_prefix('{self_address:L} ').\n\n")

    with codecs.open(inputfn, 'r', 'utf8') as f:

        parser.start(f, inputfn, module_name=MODULE_NAME)

        while parser.cur_sym != SYM_EOF:
            clauses = parser.clause(db=None)

            for clause in clauses:

                if clause.head.name == 'nlp_gens':
                    convert_nlp_gens(clause.head)
                elif clause.head.name == 'nlp_macro':
                    convert_nlp_macro(clause)
                elif clause.head.name == 'nlp_gen':
                    convert_nlp_gen(clause.head)
                # elif clause.head.name == 'answerz':
                #     convert_answerz(clause)
                elif clause.head.name == 'nlp_test':
Example #3
0
#

db = None

parser = PrologParser(db, do_inline=False)

with codecs.open(outputfn, 'w', 'utf8') as outf:

    outf.write('#!/usr/bin/env python\n')
    outf.write('# -*- coding: utf-8 -*-\n\n')

    outf.write('def get_data(k):\n')

    with codecs.open(inputfn, 'r', 'utf8') as f:

        parser.start(f, inputfn, module_name='foo')

        while parser.cur_sym != SYM_EOF:
            clauses = parser.clause()

            for clause in clauses:

                if clause.head.name == 'train_prefix':
                    convert_train_prefix(clause.head)
                elif clause.head.name == 'train':
                    convert_train(clause)
                elif clause.head.name == 'test':
                    convert_test(clause)

                else:
                    outf.write(u"    def %s:\n        %s\n" %
Example #4
0
def run(args):
    """
    %prog run command ::: file1 file2

    Parallelize a set of commands on grid. The syntax is modeled after GNU
    parallel <http://www.gnu.org/s/parallel/man.html#options>

    {}   - input line
    {.}  - input line without extension
    {/}  - basename of input line
    {/.} - basename of input line without extension
    {#}  - sequence number of job to run
    :::  - Use arguments from the command line as input source instead of stdin
    (standard input).

    A few examples:
    ls -1 *.fastq | %prog run process {} {.}.pdf  # use stdin
    %prog run process {} {.}.pdf ::: *fastq  # use :::
    %prog run "zcat {} >{.}" ::: *.gz  # quote redirection
    """
    queue_choices = ("default", "fast", "medium", "himem")
    p = OptionParser(run.__doc__)
    p.add_option("-l", dest="queue", default="default", choices=queue_choices,
                 help="Name of the queue, one of {0} [default: %default]".\
                      format("|".join(queue_choices)))
    p.add_option("-t",
                 dest="threaded",
                 type="int",
                 help="Append '-pe threaded N' [default: %default]")
    opts, args = p.parse_args(args)

    if len(args) == 0:
        sys.exit(not p.print_help())

    sep = ":::"
    if sep in args:
        sepidx = args.index(sep)
        filenames = args[sepidx + 1:]
        args = args[:sepidx]
        if not filenames:
            filenames = [""]
    else:
        filenames = sys.stdin

    assert args, "Command empty"
    cmd = " ".join(args)

    for i, filename in enumerate(filenames):
        filename = filename.strip()
        noextname = filename.rsplit(".", 1)[0]
        basename = op.basename(filename)
        basenoextname = basename.rsplit(".", 1)[0]
        ncmd = cmd

        if "{}" in ncmd:
            ncmd = ncmd.replace("{}", filename)
        else:
            ncmd += " " + filename

        ncmd = ncmd.replace("{.}", noextname)
        ncmd = ncmd.replace("{/}", basename)
        ncmd = ncmd.replace("{/.}", basenoextname)
        ncmd = ncmd.replace("{#}", str(i))

        outfile = None
        if ">" in ncmd:
            ncmd, outfile = ncmd.split(">", 1)
            ncmd, outfile = ncmd.strip(), outfile.strip()

        p = GridProcess(ncmd,
                        outfile=outfile,
                        queue=opts.queue,
                        threaded=opts.threaded)
        p.start(path=None)  # current folder
Example #5
0
File: grid.py Project: bennyyu/jcvi
def run(args):
    """
    %prog run command ::: file1 file2

    Parallelize a set of commands on grid. The syntax is modeled after GNU
    parallel <http://www.gnu.org/s/parallel/man.html#options>

    {}   - input line
    {.}  - input line without extension
    {/}  - basename of input line
    {/.} - basename of input line without extension
    {#}  - sequence number of job to run
    :::  - Use arguments from the command line as input source instead of stdin
    (standard input).

    A few examples:
    ls -1 *.fastq | %prog run process {} {.}.pdf  # use stdin
    %prog run process {} {.}.pdf ::: *fastq  # use :::
    %prog run "zcat {} >{.}" ::: *.gz  # quote redirection
    """
    queue_choices = ("default", "fast", "medium", "himem")
    p = OptionParser(run.__doc__)
    p.add_option("-l", dest="queue", default="default", choices=queue_choices,
                 help="Name of the queue, one of {0} [default: %default]".\
                      format("|".join(queue_choices)))
    p.add_option("-t", dest="threaded", type="int",
                 help="Append '-pe threaded N' [default: %default]")
    opts, args = p.parse_args(args)

    if len(args) == 0:
        sys.exit(not p.print_help())

    sep = ":::"
    if sep in args:
        sepidx = args.index(sep)
        filenames = args[sepidx + 1:]
        args = args[:sepidx]
        if not filenames:
            filenames = [""]
    else:
        filenames = sys.stdin

    assert args, "Command empty"
    cmd = " ".join(args)

    for i, filename in enumerate(filenames):
        filename = filename.strip()
        noextname = filename.rsplit(".", 1)[0]
        basename = op.basename(filename)
        basenoextname = basename.rsplit(".", 1)[0]
        ncmd = cmd

        if "{}" in ncmd:
            ncmd = ncmd.replace("{}", filename)
        else:
            ncmd += " " + filename

        ncmd = ncmd.replace("{.}", noextname)
        ncmd = ncmd.replace("{/}", basename)
        ncmd = ncmd.replace("{/.}", basenoextname)
        ncmd = ncmd.replace("{#}", str(i))

        outfile = None
        if ">" in ncmd:
            ncmd, outfile = ncmd.split(">", 1)
            ncmd, outfile = ncmd.strip(), outfile.strip()

        p = GridProcess(ncmd, outfile=outfile,
                        queue=opts.queue, threaded=opts.threaded)
        p.start(path=None)  # current folder
Example #6
0

db = None

parser = PrologParser(db, do_inline = False)

with codecs.open(outputfn, 'w', 'utf8') as outf:

    outf.write('#!/usr/bin/env python\n')
    outf.write('# -*- coding: utf-8 -*-\n\n')

    outf.write('def get_data(k):\n')

    with codecs.open(inputfn, 'r', 'utf8') as f:

        parser.start(f, inputfn, module_name='foo')

        while parser.cur_sym != SYM_EOF:
            clauses = parser.clause()

            for clause in clauses:

                if clause.head.name == 'train_prefix':
                    convert_train_prefix(clause.head)
                elif clause.head.name == 'train':
                    convert_train(clause)
                elif clause.head.name == 'test':
                    convert_test(clause)
                    
                else:
                    outf.write(u"    def %s:\n        %s\n" % (unicode(clause.head), unicode(clause.body)))
Example #7
0
#
# main
#


parser = PrologParser()

with codecs.open(outputfn, 'w', 'utf8') as outf:

    outf.write('% prolog\n\n')

    outf.write("train_prefix('{self_address:L} ').\n\n")

    with codecs.open(inputfn, 'r', 'utf8') as f:

        parser.start(f, inputfn, module_name=MODULE_NAME)

        while parser.cur_sym != SYM_EOF:
            clauses = parser.clause(db=None)

            for clause in clauses:

                if clause.head.name == 'nlp_gens':
                    convert_nlp_gens(clause.head)
                elif clause.head.name == 'nlp_macro':
                    convert_nlp_macro(clause)
                elif clause.head.name == 'nlp_gen':
                    convert_nlp_gen(clause.head)
                # elif clause.head.name == 'answerz':
                #     convert_answerz(clause)
                elif clause.head.name == 'nlp_test':