def test_complex_statement_walker():
    code = "\n".join([
        "from math import sin",
        "x = 10",
        "y = 10 + x",
        "z = sin(y) + b",
    ])
    walker = walk(parse(code), StatementWalker())
    assert len(walker.names) == 4
    assert set(walker.names) == set(['x', 'y', 'b', 'sin'])
    assert len(walker.imported_names) == 1
    assert set(walker.imported_names) == set(['sin'])
    assert len(walker.expressions) == 3
def test_simple_statement_walker():
    const_code = "a = 10"
    walker = walk(parse(const_code), StatementWalker())
    assert len(walker.names) == 0
    assert len(walker.imported_names) == 0
    assert len(walker.expressions) == 1

    const_code = "a, b = 10"
    walker = walk(parse(const_code), StatementWalker())
    assert len(walker.names) == 0
    assert len(walker.imported_names) == 0
    assert len(walker.expressions) == 1

    const_code = "a = 10; b = 20"
    walker = walk(parse(const_code), StatementWalker())
    assert len(walker.names) == 0
    assert len(walker.imported_names) == 0
    assert len(walker.expressions) == 2

    const_code = "a = 10 + 20"
    walker = walk(parse(const_code), StatementWalker())
    assert len(walker.names) == 0
    assert len(walker.imported_names) == 0
    assert len(walker.expressions) == 1
Пример #3
0
def _retrieve_inputs_and_outputs(code = "", reserved_inputs=None, \
                                 reserved_outputs=None):
    """ Parse code to retrieve inputs and outputs 
        taking into account who are the reserved_inputs and 
        reserved_outputs. 
        This function returns list of InputVariable, OutputVariable.
    """
      
    if code =="":
        return
    
    ast = parse(code)
    walker = walk(ast, StatementWalker())
    imported_names = walker.imported_names
    outputs = []
    notinputs = set()
    
    for assigns, _ in walker.expressions:
        for name in assigns:
            if reserved_outputs is None or name not in reserved_outputs:
                outputs.append(OutputVariable(name=name, binding=name))
            notinputs.add(name)

    notinputs.update(imported_names)

    # Add function definition to the list of notinputs
    function_calls = [expr for assigns, expr in walker.expressions \
                           if isinstance(expr, CallFunc) ]
    
    [notinputs.add(call.node.name) for call in function_calls]   
    
    # Add the builtins, too.
    notinputs.update(builtin_names)
    inputs = []
    for name in walker.names:
        if name not in notinputs:
            if reserved_inputs is None or name not in reserved_inputs:
                inputs.append(InputVariable(name=name, binding=name))
    
    return inputs,outputs
Пример #4
0
def _retrieve_inputs_and_outputs(code = "", reserved_inputs=None, \
                                 reserved_outputs=None):
    """ Parse code to retrieve inputs and outputs 
        taking into account who are the reserved_inputs and 
        reserved_outputs. 
        This function returns list of InputVariable, OutputVariable.
    """

    if code == "":
        return

    ast = parse(code)
    walker = walk(ast, StatementWalker())
    imported_names = walker.imported_names
    outputs = []
    notinputs = set()

    for assigns, _ in walker.expressions:
        for name in assigns:
            if reserved_outputs is None or name not in reserved_outputs:
                outputs.append(OutputVariable(name=name, binding=name))
            notinputs.add(name)

    notinputs.update(imported_names)

    # Add function definition to the list of notinputs
    function_calls = [expr for assigns, expr in walker.expressions \
                           if isinstance(expr, CallFunc) ]

    [notinputs.add(call.node.name) for call in function_calls]

    # Add the builtins, too.
    notinputs.update(builtin_names)
    inputs = []
    for name in walker.names:
        if name not in notinputs:
            if reserved_inputs is None or name not in reserved_inputs:
                inputs.append(InputVariable(name=name, binding=name))

    return inputs, outputs
Пример #5
0
def retrieve_inputs_and_outputs(code="",reserved_inputs=[],reserved_outputs=[]):
    """ Parse code to retrieve inputs and outputs 
        taking into account who are reserved_inputs and 
        reserved_outputs.
        FIXME: fix the StatementWalker object to manage function_call 
               without returned value (no assignment).
    """
       
    ast = parse(code)
    walker = walk(ast, StatementWalker())
    imported_names = walker.imported_names
    outputs = []
    notinputs = set()
    for assigns, _ in walker.expressions:
        for name in assigns:
            if reserved_outputs is None or name not in reserved_outputs:
                outputs.append(name)
            notinputs.add(name)

    notinputs.update(imported_names)
    
    # Add function definition to the list of notinputs
    function_calls = [expr for assigns, expr in walker.expressions \
                           if isinstance(expr, CallFunc) ]
    
    [notinputs.add(call.node.name) for call in function_calls]   
        
    # Add the builtins, too.
    notinputs.update(builtin_names)
    inputs = []
    for name in walker.names:
        if name not in notinputs:
            if reserved_inputs is None or name not in reserved_inputs:
                # Icky. We don't know that the user gave code in the correct
                # order.
                inputs.append(name)
    
    return inputs,outputs
Пример #6
0
 def _update_code(self, code):
     """ Update the expression code.
     """
     ast = parse(code)
     walker = walk(ast, StatementWalker())
     self.imported_names = walker.imported_names
     outputs = []
     notinputs = set()
     for assigns, expr in walker.expressions:
         for name in assigns:
             outputs.append(OutputVariable(name=name, binding=name))
             notinputs.add(name)
     notinputs.update(self.imported_names)
     # Add the builtins, too.
     notinputs.update(builtin_names)
     inputs = []
     for name in walker.names:
         if name not in notinputs:
             # Icky. We don't know that the user gave code in the correct
             # order.
             inputs.append(InputVariable(name=name, binding=name))
     self.inputs = inputs
     self.outputs = outputs
 def _update_code(self, code):
     """ Update the expression code.
     """
     ast = parse(code)
     walker = walk(ast, StatementWalker())
     self.imported_names = walker.imported_names
     outputs = []
     notinputs = set()
     for assigns, expr in walker.expressions:
         for name in assigns:
             outputs.append(OutputVariable(name=name, binding=name))
             notinputs.add(name)
     notinputs.update(self.imported_names)
     # Add the builtins, too.
     notinputs.update(builtin_names)
     inputs = []
     for name in walker.names:
         if name not in notinputs:
             # Icky. We don't know that the user gave code in the correct
             # order.
             inputs.append(InputVariable(name=name, binding=name))
     self.inputs = inputs
     self.outputs = outputs