Changeset 7b1dcf9 in sasmodels


Ignore:
Timestamp:
Dec 18, 2017 4:36:32 PM (6 years ago)
Author:
Paul Kienzle <pkienzle@…>
Children:
2694cb8
Parents:
67cc0ff
Message:

linting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/py2c.py

    r8224d24 r7b1dcf9  
    8181BOOLOP_SYMBOLS = {} 
    8282BOOLOP_SYMBOLS[ast.And] = '&&' 
    83 BOOLOP_SYMBOLS[ast.Or]  = '||' 
     83BOOLOP_SYMBOLS[ast.Or] = '||' 
    8484 
    8585CMPOP_SYMBOLS = {} 
    86 CMPOP_SYMBOLS[ast.Eq]    = '==' 
     86CMPOP_SYMBOLS[ast.Eq] = '==' 
    8787CMPOP_SYMBOLS[ast.NotEq] = '!=' 
    8888CMPOP_SYMBOLS[ast.Lt] = '<' 
     
    145145        self.new_lines = 0 
    146146        self.c_proc = [] 
    147 # for C 
     147        # for C 
    148148        self.signature_line = 0 
    149149        self.arguments = [] 
     
    181181    def add_c_line(self, x): 
    182182        string = '' 
    183         for i in range(self.indentation): 
     183        for _ in range(self.indentation): 
    184184            string += ("    ") 
    185185        string += str(x) 
     
    188188 
    189189    def add_current_line(self): 
    190         if(len(self.current_statement) > 0): 
     190        if self.current_statement: 
    191191            self.add_c_line(self.current_statement) 
    192192            self.current_statement = '' 
    193193 
    194194    def AddUniqueVar(self, new_var): 
    195         if((new_var not in self.C_Vars)): 
     195        if new_var not in self.C_Vars: 
    196196            self.C_Vars.append(str(new_var)) 
    197197 
     
    210210            self.write_c('# line: %s' % node.lineno) 
    211211            self.new_lines = 1 
    212         if(len(self.current_statement)): 
     212        if self.current_statement: 
    213213            self.Statements.append(self.current_statement) 
    214214            self.current_statement = '' 
    215215 
    216216    def body(self, statements): 
    217         if(len(self.current_statement)): 
     217        if self.current_statement: 
    218218            self.add_current_line() 
    219219        self.new_line = True 
    220220        self.indentation += 1 
    221221        for stmt in statements: 
    222             target_name = '' 
    223             if(hasattr(stmt, 'targets')): 
    224                 if(hasattr(stmt.targets[0], 'id')): 
    225                     target_name = stmt.targets[0].id # target name needed for debug only 
     222            #if hasattr(stmt, 'targets') and hasattr(stmt.targets[0], 'id'): 
     223            #    target_name = stmt.targets[0].id # target name needed for debug only 
    226224            self.visit(stmt) 
    227225        self.add_current_line() # just for breaking point. to be deleted. 
     
    242240            else: 
    243241                want_comma.append(True) 
    244 # for C 
     242 
     243        # for C 
    245244        for arg in node.args: 
    246245            # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 
     
    254253        for arg, default in zip(node.args, padding + node.defaults): 
    255254            if default is not None: 
    256                 # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 
    257255                # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 
    258256                try: 
     
    281279 
    282280    def define_C_Vars(self, target): 
    283         if(hasattr(target, 'id')): 
    284 # a variable is considered an array if it apears in the agrument list 
    285 # and being assigned to. For example, the variable p in the following 
    286 # sniplet is a pointer, while q is not 
    287 # def somefunc(p, q): 
    288 #  p = q + 1 
    289 #  return 
    290 # 
    291             if(target.id not in self.C_Vars): 
    292                 if(target.id in self.arguments): 
     281        if hasattr(target, 'id'): 
     282        # a variable is considered an array if it apears in the agrument list 
     283        # and being assigned to. For example, the variable p in the following 
     284        # sniplet is a pointer, while q is not 
     285        # def somefunc(p, q): 
     286        #  p = q + 1 
     287        #  return 
     288        # 
     289            if target.id not in self.C_Vars: 
     290                if target.id in self.arguments: 
    293291                    idx = self.arguments.index(target.id) 
    294292                    new_target = self.arguments[idx] + "[0]" 
    295                     if(new_target not in self.C_Pointers): 
     293                    if new_target not in self.C_Pointers: 
    296294                        target.id = new_target 
    297295                        self.C_Pointers.append(self.arguments[idx]) 
     
    301299    def add_semi_colon(self): 
    302300        semi_pos = self.current_statement.find(';') 
    303         if(semi_pos > 0.0): 
    304             self.current_statement = self.current_statement.replace(';','') 
     301        if semi_pos > 0.0: 
     302            self.current_statement = self.current_statement.replace(';', '') 
    305303        self.write_c(';') 
    306304 
     
    312310            self.define_C_Vars(target) 
    313311            self.visit(target) 
    314         if(len(self.Tuples) > 0): 
     312        if self.Tuples: 
    315313            tplTargets = list(self.Tuples) 
    316314            del self.Tuples[:] 
     
    327325            self.add_semi_colon() 
    328326            self.add_current_line() 
    329         if((self.is_sequence) and (not self.visited_args)): 
     327        if self.is_sequence and not self.visited_args: 
    330328            for target in node.targets: 
    331                 if(hasattr(target, 'id')): 
    332                     if((target.id in self.C_Vars) and(target.id not in self.C_DclPointers)): 
    333                         if(target.id not in self.C_DclPointers): 
     329                if hasattr(target, 'id'): 
     330                    if target.id in self.C_Vars and target.id not in self.C_DclPointers: 
     331                        if target.id not in self.C_DclPointers: 
    334332                            self.C_DclPointers.append(target.id) 
    335                             if(target.id in self.C_Vars): 
     333                            if target.id in self.C_Vars: 
    336334                                self.C_Vars.remove(target.id) 
    337335        self.current_statement = '' 
    338336 
    339337    def visit_AugAssign(self, node): 
    340         if(node.target.id not in self.C_Vars): 
    341             if(node.target.id not in self.arguments): 
     338        if node.target.id not in self.C_Vars: 
     339            if node.target.id not in self.arguments: 
    342340                self.C_Vars.append(node.target.id) 
    343341        self.visit(node.target) 
     
    365363        self.generic_visit(node) 
    366364 
    367     def listToDeclare(self, Vars): 
    368         s = '' 
    369         if(len(Vars) > 0): 
    370             s = ",".join(Vars) 
    371         return(s) 
     365    def listToDeclare(self, vars): 
     366        return ", ".join(vars) 
    372367 
    373368    def write_C_Pointers(self, start_var): 
    374         if(len(self.C_DclPointers) > 0): 
    375             vars = "" 
     369        if self.C_DclPointers: 
     370            var_list = [] 
    376371            for c_ptr in self.C_DclPointers: 
    377372                if(len(vars) > 0): 
    378373                    vars += ", " 
    379                 if(c_ptr not in self.arguments): 
    380                     vars += "*" + c_ptr 
    381                 if(c_ptr in self.C_Vars): 
    382                     if(c_ptr in self.C_Vars): 
    383                         self.C_Vars.remove(c_ptr) 
    384             if(len(vars) > 0): 
    385                 c_dcl = "    double " + vars + ";" 
    386                 self.c_proc.insert(start_var, c_dcl + "\n") 
     374                if c_ptr not in self.arguments: 
     375                    var_list.append("*" + c_ptr) 
     376                if c_ptr in self.C_Vars: 
     377                    self.C_Vars.remove(c_ptr) 
     378            if var_list: 
     379                c_dcl = "    double " + ", ".join(var_list) + ";\n" 
     380                self.c_proc.insert(start_var, c_dcl) 
    387381                start_var += 1 
    388382        return start_var 
     
    391385        fLine = False 
    392386        start_var = self.write_C_Pointers(start_var) 
    393         if(len(self.C_IntVars) > 0): 
     387        if self.C_IntVars: 
    394388            for var in self.C_IntVars: 
    395                 if(var in self.C_Vars): 
     389                if var in self.C_Vars: 
    396390                    self.C_Vars.remove(var) 
    397391            s = self.listToDeclare(self.C_IntVars) 
     
    400394            start_var += 1 
    401395 
    402         if(len(self.C_Vars) > 0): 
     396        if self.C_Vars: 
    403397            s = self.listToDeclare(self.C_Vars) 
    404398            self.c_proc.insert(start_var, "    double " + s + ";\n") 
    405399            fLine = True 
    406400            start_var += 1 
    407         if(len(self.C_Vectors) > 0): 
     401 
     402        if self.C_Vectors: 
    408403            s = self.listToDeclare(self.C_Vectors) 
    409404            for n in range(len(self.C_Vectors)): 
     
    412407                self.c_proc.insert(start_var, c_dcl + "\n") 
    413408                start_var += 1 
     409 
    414410        del self.C_Vars[:] 
    415411        del self.C_IntVars[:] 
     
    417413        del self.C_Pointers[:] 
    418414        self.C_DclPointers 
    419         if(fLine == True): 
     415        if fLine: 
    420416            self.c_proc.insert(start_var, "\n") 
    421         return 
    422         s = '' 
    423         for n in range(len(self.C_Vars)): 
    424             s += str(self.C_Vars[n]) 
    425             if n < len(self.C_Vars) - 1: 
    426                 s += ", " 
    427         if(len(s) > 0): 
    428             self.c_proc.insert(start_var, "    double " + s + ";\n") 
    429             self.c_proc.insert(start_var + 1, "\n") 
    430  
    431     def ListToString(self, strings): 
    432         s = '' 
    433         for n in range(len(strings)): 
    434             s += strings[n] 
    435             if(n < (len(strings) - 1)): 
    436                 s += ", " 
    437         return(s) 
    438  
    439     def getMethodSignature(self): 
    440         args_str = '' 
    441         for n in range(len(self.arguments)): 
    442             args_str += "double " + self.arguments[n] 
    443             if(n < (len(self.arguments) - 1)): 
    444                 args_str += ", " 
    445         return(args_str) 
    446417 
    447418    def InsertSignature(self): 
    448         args_str = '' 
    449         for n in range(len(self.arguments)): 
    450             args_str += "double " + self.arguments[n] 
    451             if(self.arguments[n] in self.C_Pointers): 
    452                 args_str += "[]" 
    453             if(n < (len(self.arguments) - 1)): 
    454                 args_str += ", " 
     419        arg_decls = [] 
     420        for arg in self.arguments: 
     421            decl = "double " + arg 
     422            if arg in self.C_Pointers: 
     423                decl += "[]" 
     424            arg_decls.append(decl) 
     425        args_str = ", ".join(arg_decls) 
    455426        self.strMethodSignature = 'double ' + self.name + '(' + args_str + ")" 
    456         if(self.signature_line >= 0): 
     427        if self.signature_line >= 0: 
    457428            self.c_proc.insert(self.signature_line, self.strMethodSignature) 
    458429 
     
    463434        self.arguments = [] 
    464435        self.name = node.name 
    465         print("Parsing '" + self.name + "'") 
    466         args_str = "" 
     436        #if self.name not in self.required_functions[0]: 
     437        #   return 
     438        #print("Parsing '" + self.name + "'") 
    467439 
    468440        self.visit(node.args) 
    469         self.getMethodSignature() 
     441        # for C 
    470442        self.signature_line = len(self.c_proc) 
     443        #self.add_c_line(self.strMethodSignature) 
    471444        self.add_c_line("\n{") 
    472445        start_vars = len(self.c_proc) + 1 
     
    493466            paren_or_comma() 
    494467            self.visit(base) 
    495         # XXX: the if here is used to keep this module compatible 
    496         #      with python 2.6. 
     468        # CRUFT: python 2.6 does not have "keywords" attribute 
    497469        if hasattr(node, 'keywords'): 
    498470            for keyword in node.keywords: 
     
    521493            if len(else_) == 0: 
    522494                break 
    523 #            elif hasattr(else_, 'orelse'): 
     495            #elif hasattr(else_, 'orelse'): 
    524496            elif len(else_) == 1 and isinstance(else_[0], ast.If): 
    525497                node = else_[0] 
    526 #                self.newline() 
     498                #self.newline() 
    527499                self.write_c('else if ') 
    528500                self.visit(node.test) 
     
    531503                self.add_current_line() 
    532504                self.add_c_line('}') 
    533 #                break 
     505                #break 
    534506            else: 
    535507                self.newline() 
     
    541513    def getNodeLineNo(self, node): 
    542514        line_number = -1 
    543         if(hasattr(node,'value')): 
     515        if hasattr(node, 'value'): 
    544516            line_number = node.value.lineno 
    545517        elif hasattr(node, 'iter'): 
    546518            if hasattr(node.iter, 'lineno'): 
    547519                line_number = node.iter.lineno 
    548         return(line_number) 
     520        return line_number 
    549521 
    550522    def GetNodeAsString(self, node): 
    551523        res = '' 
    552         if(hasattr(node, 'n')): 
     524        if hasattr(node, 'n'): 
    553525            res = str(node.n) 
    554         elif(hasattr(node, 'id')): 
     526        elif hasattr(node, 'id'): 
    555527            res = node.id 
    556         return(res) 
     528        return res 
    557529 
    558530    def GetForRange(self, node): 
     
    568540            self.current_statement = '' 
    569541        self.current_statement = temp_statement 
    570         if(len(for_args) == 1): 
     542        if len(for_args) == 1: 
    571543            stop = for_args[0] 
    572         elif(len(for_args) == 2): 
     544        elif len(for_args) == 2: 
    573545            start = for_args[0] 
    574546            stop = for_args[1] 
    575         elif(len(for_args) == 3): 
     547        elif len(for_args) == 3: 
    576548            start = for_args[0] 
    577549            stop = for_args[1] 
     
    579551        else: 
    580552            raise("Ilegal for loop parameters") 
    581         return(start, stop, step) 
     553        return start, stop, step 
    582554 
    583555    def visit_For(self, node): 
    584 # node: for iterator is stored in node.target. 
    585 # Iterator name is in node.target.id. 
     556        # node: for iterator is stored in node.target. 
     557        # Iterator name is in node.target.id. 
    586558        self.add_current_line() 
    587559        fForDone = False 
    588560        self.current_statement = '' 
    589         if(hasattr(node.iter, 'func')): 
    590             if(hasattr(node.iter.func, 'id')): 
    591                 if(node.iter.func.id == 'range'): 
     561        if hasattr(node.iter, 'func'): 
     562            if hasattr(node.iter.func, 'id'): 
     563                if node.iter.func.id == 'range': 
    592564                    self.visit(node.target) 
    593565                    iterator = self.current_statement 
    594566                    self.current_statement = '' 
    595                     if(iterator not in self.C_IntVars): 
     567                    if iterator not in self.C_IntVars: 
    596568                        self.C_IntVars.append(iterator) 
    597569                    start, stop, step = self.GetForRange(node) 
    598                     self.write_c("for(" + iterator + "=" + str(start) + \ 
    599                                   " ; " + iterator + " < " + str(stop) + \ 
    600                                   " ; " + iterator + " += " + str(step) + ") {") 
     570                    self.write_c("for(" + iterator + "=" + str(start) + 
     571                                 " ; " + iterator + " < " + str(stop) + 
     572                                 " ; " + iterator + " += " + str(step) + ") {") 
    601573                    self.body_or_else(node) 
    602574                    self.write_c("}") 
    603575                    fForDone = True 
    604         if(fForDone == False): 
     576        if not fForDone: 
    605577            line_number = self.getNodeLineNo(node) 
    606578            self.current_statement = '' 
     
    636608 
    637609    def visit_Print(self, node): 
    638 # XXX: python 2.6 only 
     610        # CRUFT: python 2.6 only 
    639611        self.newline(node) 
    640612        self.write_c('print ') 
     
    704676 
    705677    def visit_Raise(self, node): 
    706         # XXX: Python 2.6 / 3.0 compatibility 
     678        # CRUFT: Python 2.6 / 3.0 compatibility 
    707679        self.newline(node) 
    708680        self.write_python('raise') 
     
    738710            else: 
    739711                want_comma.append(True) 
    740         if(hasattr(node.func, 'id')): 
    741             if(node.func.id not in self.C_Functions): 
     712        if hasattr(node.func, 'id'): 
     713            if node.func.id not in self.C_Functions: 
    742714                self.C_Functions.append(node.func.id) 
    743             if(node.func.id == 'abs'): 
     715            if node.func.id == 'abs': 
    744716                self.write_c("fabs ") 
    745             elif(node.func.id == 'int'): 
     717            elif node.func.id == 'int': 
    746718                self.write_c('(int) ') 
    747             elif(node.func.id == "SINCOS"): 
     719            elif node.func.id == "SINCOS": 
    748720                self.WriteSincos(node) 
    749721                return 
     
    776748    def visit_Name(self, node): 
    777749        self.write_c(node.id) 
    778         if((node.id in self.C_Pointers) and(not self.SubRef)): 
     750        if node.id in self.C_Pointers and not self.SubRef: 
    779751            self.write_c("[0]") 
    780752        name = "" 
    781753        sub = node.id.find("[") 
    782         if(sub > 0): 
     754        if sub > 0: 
    783755            name = node.id[0:sub].strip() 
    784756        else: 
    785757            name = node.id 
    786 #      add variable to C_Vars if it ins't there yet, not an argument and not a number 
    787         if ((name not in self.C_Functions) and (name not in self.C_Vars) and \ 
    788             (name not in self.C_IntVars) and (name not in self.arguments) and \ 
    789             (name not in self.C_Constants) and (name.isdigit() == False)): 
    790             if(self.InSubscript): 
     758        # add variable to C_Vars if it ins't there yet, not an argument and not a number 
     759        if (name not in self.C_Functions and name not in self.C_Vars and 
     760                name not in self.C_IntVars and name not in self.arguments and 
     761                name not in self.C_Constants and not name.isdigit()): 
     762            if self.InSubscript: 
    791763                self.C_IntVars.append(node.id) 
    792764            else: 
     
    814786            s = "" 
    815787            for idx, item in enumerate(node.elts): 
    816                 if((idx > 0) and(len(s) > 0)): 
     788                if idx > 0 and s: 
    817789                    s += ', ' 
    818                 if(hasattr(item, 'id')): 
     790                if hasattr(item, 'id'): 
    819791                    s += item.id 
    820                 elif(hasattr(item, 'n')): 
     792                elif hasattr(item, 'n'): 
    821793                    s += str(item.n) 
    822             if(len(s) > 0): 
     794            if s: 
    823795                self.C_Vectors.append(s) 
    824796                vec_name = "vec"  + str(len(self.C_Vectors)) 
    825797                self.write_c(vec_name) 
    826                 vec_name += "#" 
    827798        return visit 
    828799 
     
    844815        function_name = '' 
    845816        is_negative_exp = False 
    846         if(isevaluable(str(self.current_statement))): 
     817        if isevaluable(str(self.current_statement)): 
    847818            exponent = eval(string) 
    848819            is_negative_exp = exponent < 0 
    849820            abs_exponent = abs(exponent) 
    850             if(abs_exponent == 2): 
     821            if abs_exponent == 2: 
    851822                function_name = "square" 
    852             elif(abs_exponent == 3): 
     823            elif abs_exponent == 3: 
    853824                function_name = "cube" 
    854             elif(abs_exponent == 0.5): 
     825            elif abs_exponent == 0.5: 
    855826                function_name = "sqrt" 
    856             elif(abs_exponent == 1.0/3.0): 
     827            elif abs_exponent == 1.0/3.0: 
    857828                function_name = "cbrt" 
    858         if(function_name == ''): 
     829        if function_name == '': 
    859830            function_name = "pow" 
    860831        return function_name, is_negative_exp 
    861832 
    862833    def translate_power(self, node): 
    863 # get exponent by visiting the right hand argument. 
     834        # get exponent by visiting the right hand argument. 
    864835        function_name = "pow" 
    865836        temp_statement = self.current_statement 
    866 # 'visit' functions write the results to the 'current_statement' class memnber 
    867 # Here, a temporary variable, 'temp_statement', is used, that enables the 
    868 # use of the 'visit' function 
     837        # 'visit' functions write the results to the 'current_statement' class memnber 
     838        # Here, a temporary variable, 'temp_statement', is used, that enables the 
     839        # use of the 'visit' function 
    869840        self.current_statement = '' 
    870841        self.visit(node.right) 
     
    872843        function_name, is_negative_exp = self.get_special_power(self.current_statement) 
    873844        self.current_statement = temp_statement 
    874         if(is_negative_exp): 
     845        if is_negative_exp: 
    875846            self.write_c("1.0 /(") 
    876847        self.write_c(function_name + "(") 
    877848        self.visit(node.left) 
    878         if(function_name == "pow"): 
     849        if function_name == "pow": 
    879850            self.write_c(", ") 
    880851            self.visit(node.right) 
    881852        self.write_c(")") 
    882         if(is_negative_exp): 
     853        if is_negative_exp: 
    883854            self.write_c(")") 
    884855        self.write_c(" ") 
     
    893864    def visit_BinOp(self, node): 
    894865        self.write_c("(") 
    895         if('%s' % BINOP_SYMBOLS[type(node.op)] == BINOP_SYMBOLS[ast.Pow]): 
     866        if '%s' % BINOP_SYMBOLS[type(node.op)] == BINOP_SYMBOLS[ast.Pow]: 
    896867            self.translate_power(node) 
    897         elif('%s' % BINOP_SYMBOLS[type(node.op)] == BINOP_SYMBOLS[ast.FloorDiv]): 
     868        elif '%s' % BINOP_SYMBOLS[type(node.op)] == BINOP_SYMBOLS[ast.FloorDiv]: 
    898869            self.translate_integer_divide(node) 
    899870        else: 
     
    903874        self.write_c(")") 
    904875 
    905 #      for C 
     876    # for C 
    906877    def visit_BoolOp(self, node): 
    907878        self.write_c('(') 
     
    930901 
    931902    def visit_Subscript(self, node): 
    932         if (node.value.id not in self.C_Constants): 
    933             if(node.value.id not in self.C_Pointers): 
     903        if node.value.id not in self.C_Constants: 
     904            if node.value.id not in self.C_Pointers: 
    934905                self.C_Pointers.append(node.value.id) 
    935906        self.SubRef = True 
     
    980951                self.visit(comprehension) 
    981952            self.write_c(right) 
    982 #            self.write_python(right) 
     953            #self.write_python(right) 
    983954        return visit 
    984955 
     
    1009980 
    1010981    def visit_Repr(self, node): 
    1011         # XXX: python 2.6 only 
     982        # CRUFT: python 2.6 only 
    1012983        self.write_c('`') 
    1013984        self.visit(node.value) 
     
    1025996        self.visit(node.target) 
    1026997        self.write_C(' in ') 
    1027 #        self.write_python(' in ') 
     998        #self.write_python(' in ') 
    1028999        self.visit(node.iter) 
    10291000        if node.ifs: 
     
    10601031    return snippets 
    10611032 
    1062 def get_file_names(): 
    1063     fname_in = "" 
    1064     fname_out = "" 
    1065     if(len(sys.argv) > 1): 
    1066         fname_in = sys.argv[1] 
    1067         fname_base = os.path.splitext(fname_in) 
    1068         if(len(sys.argv) == 2): 
    1069             fname_out = str(fname_base[0]) + '.c' 
    1070         else: 
    1071             fname_out = sys.argv[2] 
    1072         if(len(fname_in) > 0): 
    1073             python_file = open(sys.argv[1], "r") 
    1074             if(len(fname_out) > 0): 
    1075                 file_out = open(fname_out, "w+") 
    1076     return len(sys.argv), fname_in, fname_out 
    1077  
    1078 if __name__ == "__main__": 
     1033def main(): 
    10791034    import os 
    10801035    print("Parsing...using Python" + sys.version) 
    1081     try: 
    1082         fname_in = "" 
    1083         fname_out = "" 
    1084         if(len(sys.argv) == 1): 
    1085             print("Usage:\npython parse01.py <infile> [<outfile>](if omitted, output file is '<infile>.c'") 
    1086         else: 
    1087             fname_in = sys.argv[1] 
    1088             fname_base = os.path.splitext(fname_in) 
    1089             if(len(sys.argv) == 2): 
    1090                 fname_out = str(fname_base[0]) + '.c' 
    1091             else: 
    1092                 fname_out = sys.argv[2] 
    1093             if(len(fname_in) > 0): 
    1094                 python_file = open(sys.argv[1], "r") 
    1095                 if(len(fname_out) > 0): 
    1096                     file_out = open(fname_out, "w+") 
    1097                 functions = ["MultAsgn", "Iq41", "Iq2"] 
    1098                 tpls = [functions, fname_in, 0] 
    1099                 c_txt = translate(tpls) 
    1100                 file_out.write(c_txt) 
    1101                 file_out.close() 
    1102     except Exception as excp: 
    1103         print("Error:\n" + str(excp.args)) 
     1036    if len(sys.argv) == 1: 
     1037        print("""\ 
     1038Usage: python py2c.py <infile> [<outfile>] 
     1039 
     1040if outfile is omitted, output file is '<infile>.c' 
     1041""") 
     1042        return 
     1043 
     1044    fname_in = sys.argv[1] 
     1045    if len(sys.argv) == 2: 
     1046        fname_base = os.path.splitext(fname_in)[0] 
     1047        fname_out = str(fname_base) + '.c' 
     1048    else: 
     1049        fname_out = sys.argv[2] 
     1050 
     1051    with open(fname_in, "r") as python_file: 
     1052        code = python_file.read() 
     1053 
     1054    translation = translate([code, fname_in, 1])[0] 
     1055 
     1056    with open(fname_out, "w") as file_out: 
     1057        file_out.write(translation) 
    11041058    print("...Done") 
     1059 
     1060if __name__ == "__main__": 
     1061    main() 
Note: See TracChangeset for help on using the changeset viewer.