Esempio n. 1
0
 def run_regression(self, input, filename):
     symtab = {}
     locals_ = {}
     globals_ = {}
     eval(compile('from smop.runtime import *\nfrom smop.core import *\n', '<imports>', 'exec'), globals_, globals_)
     t = parse.parse(input + '\n')
     # print "t=", repr(t)
     resolve.resolve(t, symtab)
     # print "symtab:",symtab
     s = backend.backend(t)
     eval(compile(s, filename, 'exec'), globals_, locals_)
     return locals_
Esempio n. 2
0
 def run_regression(self, input, filename):
     symtab = {}
     locals_ = {}
     globals_ = {}
     eval(
         compile('from smop.runtime import *\nfrom smop.core import *\n',
                 '<imports>', 'exec'), globals_, globals_)
     t = parse.parse(input + '\n')
     # print "t=", repr(t)
     resolve.resolve(t, symtab)
     # print "symtab:",symtab
     s = backend.backend(t)
     eval(compile(s, filename, 'exec'), globals_, locals_)
     return locals_
Esempio n. 3
0
def convert_mcase(mfile,pfile=None,adjust_code=True):
    """Convert MatPower case file to PyPower case file
    This method requires the Python module `smop` to be installed.
    """
    import os,sys
    from numpy import asarray,set_printoptions

    try:
        from smop import parse,resolve,backend,options,graphviz
    except ImportError:
        raise ImportError("In order to load m-file cases you have to install the `smop` python module\n")
    
    def is_number(s):
        try:
            int(s)
            return True
        except ValueError:
            return False

    if pfile is None:
        pfile = mfile[:-2]+".py"

 # prepare m-file
    print "Preparing m-file and convert to Python ... "
    newlines = []
    with open(mfile) as f:
        lines = f.readlines()    
        for line in lines:
            if len(line)>1:
                if is_number(line[-2]):
                    line = line[:-1] + ";\n"
            newlines.append(line)
    with open(mfile[:-2]+"_repaired.m","w") as f:
        f.writelines(newlines)                
        mfile = mfile[:-2]+"_repaired.m"
            
 # convert m-file            
    dot = None    
    fp = open(pfile,"w")
    print >> fp, "# Autogenerated with SMOP invoked from"
    print >> fp, "# " + " ".join(sys.argv)
    print >> fp, "from __future__ import division"
    print >> fp, "from numpy import asarray\n\n"

    buf = open(mfile).read().replace("\r\n","\n")
    func_list = parse.parse(buf if buf[-1]=='\n' else buf+'\n',mfile)
    if not func_list and strict:
        sys.exit(-1)
    
    for func_obj in func_list: 
        try:
            func_name = func_obj.head.ident.name
        except AttributeError:
            continue
        fp0 = open("parse_"+func_name+".dot","w") if dot and dot.match(func_name) else None
        if fp0:
            graphviz.graphviz(func_obj,fp0)
        if options.do_resolve:
            resolve.resolve(func_obj)
    
    for func_obj in func_list:
        s = backend.backend(func_obj)
        print >> fp, s
    fp.close()
    
 # adjust Python file    
    set_printoptions(precision=3,threshold=int(1e6),linewidth=5000)
    path = os.getcwd()    
    sys.path.insert(0,path)
    ntab = 4    # number of spaces used for indentation
    space = " "*ntab
   
    if adjust_code:        
        with open(pfile) as f:
            lines = f.readlines()
            new_lines = []
            first = True
            for line in lines:
                if line[ntab:ntab+8]=="varargin" or line[ntab:ntab+6]=="nargin":
                    line = "#" + line
                if line[ntab:ntab+3]=="mpc":
                    if first:
                        new_lines.append(space + "mpc=dict()\n")
                        first = False
                    pos1 = ntab+4
                    pos2 = line.index("=")
                    name = line[pos1:pos2]
                    if line[pos2+1]=="[":
                        print name +"...  ",
                        text = line[pos2+1:-1].replace("\n",";")
                        value = "asarray(\n" + text + ")\n"
                        if name=="bus":
                            bus = eval(value)
                            print bus.shape
                            value = "asarray(\n\t" + repr(bus)[6:] + "\n"
                        elif name=="branch":
                            branch = eval(value)
                            print branch.shape
                            value = "asarray(\n\t" + repr(branch)[6:] + "\n"
                        elif name=="busnames":                        
                            value = line[pos2+1:-1] + "\n"
                            value = value.replace("char(","").replace(")","")
                            val = eval(value)
                            print len(val)                        
                        else:
                            val = eval(value)
                            print val.shape
                            value = "asarray(\n\t"+repr(val)[6:] + "\n"
                    elif line[pos2+1:pos2+5]=="char":
                        value = line[pos2+6:line.index(")")] + "\n"
                    else:
                        value = line[pos2+1:]
                    line = line[:ntab] + 'mpc["%s"]'%name + "=" + value
                new_lines.append(line)
        with open(pfile,"w") as f:
            f.writelines(new_lines)
    print "Done.\nPython case file saved as %s" %(os.path.join(path,pfile))
Esempio n. 4
0
def main():
    """
    !a="def f(): \\n\\treturn 123"
    !exec a
    !print f
    !print f()
    !reload(backend)
    =>> function t=foo(a) \\
    ... t=123
    !exec foo(3)
    """
    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:],
                                       "d:ho:vVsr:S:X:",
                                       [
                                        "dot=",
                                        "exclude=",
                                        "help",
                                        "syntax-errors=",
                                        "output=",
                                        "runtime=",
                                        "strict",
                                        "verbose",
                                        "version",
                                       ])
    except getopt.GetoptError as err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    exclude_list = []
    output = None
    strict = 0
    dot = None
    runtime = []

    for o, a in opts:
        if o in ("-r", "--runtime"):
            runtime += [a]
        elif o in ("-s", "--strict"):
            strict = 1
        elif o in ("-S", "--syntax-errors"):
            options.syntax_errors += a.split(",")
        elif o in ("-d", "--dot"):
            dot = re.compile(a)
        elif o in ("-X", "--exclude"):
            exclude_list += [ "%s.m" % b for b in a.split(",")]
        elif o in ("-v", "--verbose"):
            options.verbose += 1
        elif o in ("-V", "--version"):
            print "SMOP compiler version " + __version__
            sys.exit()
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-o", "--output"):
            output = a
        else:
            assert False, "unhandled option"

    """
    if not args:
        usage()
        sys.exit()
    """
    if not args:
        symtab = {}
        print "? for help"
        while 1:
            try:
                buf = input("octave: ")
                if not buf:
                    continue
                while buf[-1] == "\\":
                    buf = buf[:-1] + "\n" + input("... ")
                if buf[0] == '?':
                    print main.__doc__
                    continue
                if buf[0] == "!":
                    try:
                        exec(buf[1:])
                    except Exception as ex:
                        print ex
                    continue
                t = parse.parse(buf if buf[-1]=='\n' else buf+'\n')
                if not t:
                    continue
                print "t=", repr(t)
                print 60*"-"
                resolve.resolve(t,symtab)
                print "t=", repr(t)
                print 60*"-"
                print "symtab:",symtab
                s = backend.backend(t)
                print "python:",s.strip()
                try:
                    print eval(s)
                except SyntaxError:
                    exec(s)
            except EOFError:
                return
            except Exception as ex:
                print ex

    if not output:
        output = "a.py"
    fp = open(output,"w") if output != "-" else sys.stdout
    print >> fp, "# Autogenerated with SMOP version " + __version__
    print >> fp, "# " + " ".join(sys.argv)
    print >> fp, "from __future__ import division"
    for a in runtime:
        print >> fp, "from %s import *" % a

    for pattern in args:
        for filename in glob.glob(os.path.expanduser(pattern)):
            if not filename.endswith(".m"):
                print "\tIngored file: '%s'" % filename
                continue
            if os.path.basename(filename) in exclude_list:
                print "\tExcluded file: '%s'" % filename
                continue
            if options.verbose:
                print filename
            buf = open(filename).read().replace("\r\n","\n")
            func_list = parse.parse(buf if buf[-1]=='\n' else buf+'\n',filename)
            if not func_list and strict:
                sys.exit(-1)

            for func_obj in func_list:
                try:
                    func_name = func_obj.head.ident.name
                    if options.verbose:
                        print "\t",func_name
                except AttributeError:
                    if options.verbose:
                        print "\tJunk ignored"
                    if strict:
                        sys.exit(-1)
                    continue
                fp0 = open("parse_"+func_name+".dot","w") if dot and dot.match(func_name) else None
                if fp0:
                    graphviz.graphviz(func_obj,fp0)
                if options.do_resolve:
                    G = resolve.resolve(func_obj)

            for func_obj in func_list:
                s = backend.backend(func_obj)
                print >> fp, s
Esempio n. 5
0
def main():
    """
    !a="def f(): \\n\\treturn 123"
    !exec a
    !print f
    !print f()
    !reload(backend)
    =>> function t=foo(a) \\
    ... t=123
    !exec foo(3)
    """
    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:],
                                       "d:ho:vVsr:S:X:",
                                       [
                                        "dot=",
                                        "exclude=",
                                        "help",
                                        "syntax-errors=",
                                        "output=",
                                        "runtime=",
                                        "strict",
                                        "verbose",
                                        "version",
                                       ])
    except getopt.GetoptError as err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    exclude_list = []
    output = None
    strict = 0
    dot = None
    runtime = []

    for o, a in opts:
        if o in ("-r", "--runtime"):
            runtime += [a]
        elif o in ("-s", "--strict"):
            strict = 1
        elif o in ("-S", "--syntax-errors"):
            options.syntax_errors += a.split(",")
        elif o in ("-d", "--dot"):
            dot = re.compile(a)
        elif o in ("-X", "--exclude"):
            exclude_list += [ "%s.m" % b for b in a.split(",")]
        elif o in ("-v", "--verbose"):
            options.verbose += 1
        elif o in ("-V", "--version"):
            print "SMOP compiler version " + __version__
            sys.exit()
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-o", "--output"):
            output = a
        else:
            assert False, "unhandled option"

    """
    if not args:
        usage()
        sys.exit()
    """
    if not args:
        symtab = {}
        print "? for help"
        while 1:
            try:
                buf = input("octave: ")
                if not buf:
                    continue
                while buf[-1] == "\\":
                    buf = buf[:-1] + "\n" + input("... ")
                if buf[0] == '?':
                    print main.__doc__
                    continue
                if buf[0] == "!":
                    try:
                        exec(buf[1:])
                    except Exception as ex:
                        print ex
                    continue
                t = parse.parse(buf if buf[-1]=='\n' else buf+'\n')
                if not t:
                    continue
                print "t=", repr(t)
                print 60*"-"
                resolve.resolve(t,symtab)
                print "t=", repr(t)
                print 60*"-"
                print "symtab:",symtab
                s = backend.backend(t)
                print "python:",s.strip()
                try:
                    print eval(s)
                except SyntaxError:
                    exec(s)
            except EOFError:
                return
            except Exception as ex:
                print ex

    if not output:
        output = "a.py"
    fp = open(output,"w") if output != "-" else sys.stdout
    print >> fp, "# Autogenerated with SMOP version " + __version__
    print >> fp, "# " + " ".join(sys.argv)
    print >> fp, "from __future__ import division"
    for a in runtime:
        print >> fp, "from %s import *" % a

    for pattern in args:
        for filename in glob.glob(os.path.expanduser(pattern)):
            if not filename.endswith(".m"):
                print "\tIngored file: '%s'" % filename
                continue
            if os.path.basename(filename) in exclude_list:
                print "\tExcluded file: '%s'" % filename
                continue
            if options.verbose:
                print filename
            buf = open(filename).read().replace("\r\n","\n")
            func_list = parse.parse(buf if buf[-1]=='\n' else buf+'\n',filename)
            if not func_list and strict:
                sys.exit(-1)

            for func_obj in func_list:
                try:
                    func_name = func_obj.head.ident.name
                    if options.verbose:
                        print "\t",func_name
                except AttributeError:
                    if options.verbose:
                        print "\tJunk ignored"
                    if strict:
                        sys.exit(-1)
                    continue
                fp0 = open("parse_"+func_name+".dot","w") if dot and dot.match(func_name) else None
                if fp0:
                    graphviz.graphviz(func_obj,fp0)
                if options.do_resolve:
                    G = resolve.resolve(func_obj)

            for func_obj in func_list:
                s = backend.backend(func_obj)
                print >> fp, s