Python ast.Print() Examples

The following are 10 code examples of ast.Print(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ast , or try the search function .
Example #1
Source File: parser.py    From PythonCompiler with GNU General Public License v3.0 6 votes vote down vote up
def parse(self):
        @self.pg.production('program : PRINT OPEN_PAREN expression CLOSE_PAREN SEMI_COLON')
        def program(p):
            return Print(self.builder, self.module, self.printf, p[2])

        @self.pg.production('expression : expression SUM expression')
        @self.pg.production('expression : expression SUB expression')
        def expression(p):
            left = p[0]
            right = p[2]
            operator = p[1]
            if operator.gettokentype() == 'SUM':
                return Sum(self.builder, self.module, left, right)
            elif operator.gettokentype() == 'SUB':
                return Sub(self.builder, self.module, left, right)

        @self.pg.production('expression : NUMBER')
        def number(p):
            return Number(self.builder, self.module, p[0].value)

        @self.pg.error
        def error_handle(token):
            raise ValueError(token) 
Example #2
Source File: onelinerizer.py    From onelinerizer with MIT License 6 votes vote down vote up
def onelinerize(original):
    # original :: string
    # :: string
    t = ast.parse(original)
    table = symtable.symtable(original, '<string>', 'exec')

    original = original.strip()

    # If there's only one line anyways, be lazy
    if len(original.splitlines()) == 1 and \
       len(t.body) == 1 and \
       type(t.body[0]) in (ast.Delete, ast.Assign, ast.AugAssign, ast.Print,
                           ast.Raise, ast.Assert, ast.Import, ast.ImportFrom,
                           ast.Exec, ast.Global, ast.Expr, ast.Pass):
        return original

    return get_init_code(t, table) 
Example #3
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_print_stmt_1(p):
    '''print_stmt : PRINT'''
    #                   1
    p[0] = ast.Print(None, [], True, rule=inspect.currentframe().f_code.co_name, **p[1][1]) 
Example #4
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_print_stmt_2(p):
    '''print_stmt : PRINT test'''
    #                   1    2
    p[0] = ast.Print(None, [p[2]], True, rule=inspect.currentframe().f_code.co_name, **p[1][1]) 
Example #5
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_print_stmt_3(p):
    '''print_stmt : PRINT test COMMA'''
    #                   1    2     3
    p[0] = ast.Print(None, [p[2]], False, rule=inspect.currentframe().f_code.co_name, **p[1][1]) 
Example #6
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_print_stmt_4(p):
    '''print_stmt : PRINT test print_stmt_plus'''
    #                   1    2               3
    p[0] = ast.Print(None, [p[2]] + p[3], True, rule=inspect.currentframe().f_code.co_name, **p[1][1]) 
Example #7
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_print_stmt_5(p):
    '''print_stmt : PRINT test print_stmt_plus COMMA'''
    #                   1    2               3     4
    p[0] = ast.Print(None, [p[2]] + p[3], False, rule=inspect.currentframe().f_code.co_name, **p[1][1]) 
Example #8
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_print_stmt_7(p):
    '''print_stmt : PRINT RIGHTSHIFT test print_stmt_plus'''
    #                   1          2    3               4
    p[0] = ast.Print(p[3], p[4], True, rule=inspect.currentframe().f_code.co_name, **p[1][1]) 
Example #9
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_print_stmt_8(p):
    '''print_stmt : PRINT RIGHTSHIFT test print_stmt_plus COMMA'''
    #                   1          2    3               4     5
    p[0] = ast.Print(p[3], p[4], False, rule=inspect.currentframe().f_code.co_name, **p[1][1]) 
Example #10
Source File: custom_rules.py    From warriorframework with Apache License 2.0 4 votes vote down vote up
def func_check(node, kw=False):
    '''
    Function specific check
        Action - check return type
        Action - check pstep/psubstep buddy with report_step/report_substep_status
        check docstring
        check print statement - should use print_utils
        scan for class - class check
        scan for function - recursive function check
    '''
    status = True
    have_return = False
    substep_count = 0

    # investigate everything in a function
    for child in ast.walk(node):
        if child != node and isinstance(child, ast.FunctionDef):
            # check for private method in action file
            if kw and child.name.startswith("_"):
                print node.name, child.name, "should move to utils"
                status = False
            tmp_status = func_check(child, kw)
            status &= tmp_status
        elif child != node and isinstance(child, ast.ClassDef):
            tmp_status = class_check(child, kw)
            status &= tmp_status
        elif 'war_print_class.py' not in sys.argv[1] and isinstance(child, ast.Print):
            # check for print statement
            status = False
            print "Please use print_Utils instead of print in {}: {}".format(
                                                    sys.argv[1], child.lineno)
        elif isinstance(child, ast.Return):
            # check for return statement
            have_return = True
        elif isinstance(child, ast.Attribute) and child.attr == 'pSubStep':
            # check for Substep and report substep pair
            substep_count += 1
        elif isinstance(child, ast.Attribute) and child.attr == 'report_substep_status':
            substep_count -= 1

    if ast.get_docstring(node) is None:
        print node.name, "doesn't contain any docstring"
        status = False
    if kw and not have_return and node.name != "__init__":
        print node.name, "doesn't contain a return statement"
        status = False
    if kw and substep_count:
        print node.name, "have non-pair pSubStepreport_substep_status"
        status = False
    return status