Contents

  1. ../daredare/daredare.py
  2. ../daredare/__init__.py

../daredare/daredare.py 1/2

[
top][prev][next]
import sys,getopt,commands

from model.symbolic import *
from model.model import *
from solver.solver import *
from misc.calculus import *
from compilator.compilator import *

if __name__ == "__main__":
    main(sys.argv[1:])

    def main(argv):
        if len(argv)<1:
            print("not enough argument")
            sys.exit(2)
        
        filetype = ""
        
        filename = argv[-1] # last argument is the filename
        regex_mod = re.compile("(.*)\.mod")
        regex_mod_match = re.match(regex_mod,filename)
        if regex_mod_match:
            filetype = "mod"
            filename_trunc = regex_mod_match.groups()[0]
    
        regex_xml = re.compile("(.*)\.xml")
        regex_xml_match = re.match(regex_xml,filename)
        if regex_xml_match:
            filetype = "xml"
            filename_trunc = regex_xml_match.groups()[0]
        
        if filetype == "":
            print("Unknown filetype")
            sys.exit(2)
        
        # Read options
        options = {"ramsey":False,"portfolio":False}
        short_arg_dict = "hr"
        long_arg_dict = ["help","ramsey","portfolio"]
        try:
            opts, args = getopt.getopt(argv, short_arg_dict, long_arg_dict)
        except getopt.GetoptError:
            usage()
            sys.exit(2)
        for opt, arg in opts:
            if opt in ("-h","--help"):
                usage()
                sys.exit()
            if opt in ("-r","--ramsey"):
                options["ramsey"] = True
            if opt in ("--portfolio"):
                options["portfolio"] = True
                
        # Start the actual work        
        if filetype == "mod":
            process_mod_file(filename_trunc)
        elif filetype == "xml":
            model = read_xml_file(filename_trunc)
            if options["ramsey"]:
                write_ramsey_policy(filename_trunc, options, model)
            if options["portfolio"]:
                write_portfolio_version(filename_trunc, options, model)
        
    def process_mod_file(filename_trunc,options={}):
        '''read mod file ; write xml file'''
        file_path = filename_trunc
        print("Processing modfile : " + file_path + '.mod')
        output_file_path = filename_trunc + '_dd'
        command = "ruby DareDare.rb convert " + file_path + " " + output_file_path
        output = commands.getstatusoutput(command)
        if not output[0]:
            print("Conversion successfull. Written to : " + output_file_path + ".xml")
            sys.exit(2)
    
    def read_xml_file(filename_trunc,options={}):
        '''process xml file ; returns parsed model with symbolic equations'''
        print("Processing xmlfile : " + filename_trunc + ".xml")
        f = file(filename_trunc + ".xml")
        txt = f.read()
        f.close()
        model = Model(filename_trunc)
        model.import_from_xml(ET.XML(txt))
        model.sympify_model()   
        return(model)
    
    def write_ramsey_policy(filename_trunc,options,model):
        #model.export_to_modfile()
        ramsey_model = model.process_ramsey_policy_model()
        f = open(filename_trunc + "_ramsey.mod","w")
        f.write(ramsey_model.export_to_modfile())
        f.close()
        print("Ramsey model written to : " + filename_trunc + "_ramsey.mod")
    
    def write_portfolio_version(filename_trunc,options,model):
        print("Processing portfolios")
        portfolio_model = model.process_portfolio_model()
        f = open(filename_trunc + "_pf.mod","w")
        f.write(portfolio_model.export_to_modfile())
        f.close()
        print("Portfolio model written to : " + filename_trunc + "_pf.mod")
    
    def usage():
        print("Bad arguments")

../daredare/__init__.py 2/2

[
top][prev][next]
__all__ = ["compilator","misc","model","solver","extern"]

Generated by GNU enscript 1.6.4.