Python _ast.Attribute() Examples

The following are 5 code examples of _ast.Attribute(). 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: stmt.py    From equip with Apache License 2.0 6 votes vote down vote up
def make_attribute(i, bytecode, context=None):
    arg = bytecode[i][3]
    attr_path = []
    j = i
    while True:
      prev_op, prev_arg =  bytecode[j][2], bytecode[j][3]
      if prev_op not in (LOAD_ATTR, STORE_ATTR, DELETE_ATTR):
        break
      attr_path.append(prev_arg)
      if j < 0:
        break
      j -= 1
    # The parent of the ATTR can be whatever expression...
    i, name = Statement.make_expr(j, bytecode)
    attr = name
    while True:
      if not attr_path:
        break
      attr_name = attr_path.pop()
      attr = _ast.Attribute(attr, attr_name, context)
    return i, attr 
Example #2
Source File: calc.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def visit_attribute(self, node: _ast.Attribute):  # value, attr, ctx
        value = self._run(node.value)
        t = type(value)
        try:
            if value in self.allowed_modules:
                if node.attr in self.allowed_modules[value]:
                    return getattr(value, node.attr)
                raise BadSyntax(f'You can not access `{node.attr}` attribute')
            if value in self.allowed_class_properties:
                if node.attr in self.allowed_class_properties[value]:
                    return getattr(value, node.attr)
                raise BadSyntax(f'You can not access `{node.attr}` attribute')
        except TypeError:
            pass
        if t in self.allowed_instance_properties:
            if node.attr in self.allowed_instance_properties[t]:
                return getattr(value, node.attr)
            raise BadSyntax(f'You can not access `{node.attr}` attribute')
        raise BadSyntax(f'You can not access attributes of {t}') 
Example #3
Source File: ast_transformer.py    From pymtl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def visit_Name( self, node ):
    new_node = LocalVar( id=node.id )
    new_node._name = node.id
    return ast.copy_location( new_node, node )


#-------------------------------------------------------------------------
# ReorderAST
#-------------------------------------------------------------------------
# Reorders an AST branch beginning with the indicated node.  Intended
# for inverting the order of Name/Attribute chains so that the Name
# node comes first, followed by chains of Attribute/Subscript nodes.
#
# This visitor will also insert Self nodes to represent references to the
# self variable, and remove Index nodes which seem to serve no useful
# purpose. 
Example #4
Source File: ast_transformer.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Num( self, node ):
    # Name generation
    node._name = str( node.n )
    self.stack.append( node )

#------------------------------------------------------------------------
# Self
#------------------------------------------------------------------------
# New AST Node for references to self. Based on Attribute node. 
Example #5
Source File: grammar.py    From runa with MIT License 5 votes vote down vote up
def get_rules():
	
	with open(PARSER_FILE) as f:
		src = f.read()
	
	rules = collections.OrderedDict()
	for node in ast.parse(src).body:
		
		if not isinstance(node, _ast.FunctionDef):
			continue
		
		if not node.decorator_list:
			continue
		
		assert len(node.decorator_list) == 1
		decorator = node.decorator_list[0]
		if not isinstance(decorator, _ast.Call):
			continue
		
		func = decorator.func
		if not isinstance(func, _ast.Attribute):
			continue
		
		assert func.attr == 'production'
		ln = decorator.args[0].s
		name, match = ln.split(' : ', 1)
		rules.setdefault(name, []).append(tuple(match.split()))
	
	return rules