Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(6)  |  Call(5)  |  Derive(0)  |  Import(1)
Make a checker function for :class:`ast.Name` or :class:`ast.Attribute`.

These are often used in similar ways - depending on how you do imports,
objects will be referenced as names or as attributes of a module. By using
this function to build your template, you can allow either.

        def name_or_attr(name):
    """Make a checker function for :class:`ast.Name` or :class:`ast.Attribute`.
    
    These are often used in similar ways - depending on how you do imports,
    objects will be referenced as names or as attributes of a module. By using
    this function to build your template, you can allow either.
    """
    def checker(node, path):
        if isinstance(node, ast.Name):
            if node.id != name:
                raise ASTPlainObjMismatch(path+['id'], node.id, name)
        elif isinstance(node, ast.Attribute):
            if node.attr != name:
                raise ASTPlainObjMismatch(path+['attr'], node.attr, name)
        else:
            raise ASTNodeTypeMismatch(path, node, "Name or Attribute")

    return checker
        


src/a/s/astcheck-0.2.1/test_astcheck.py   astcheck(Download)
import ast
import astcheck
from astcheck import (assert_ast_like, is_ast_like, mkarg, format_path,
                      listmiddle, name_or_attr,
                     )
 
template4 = ast.Expression(body=ast.BinOp(
    left=ast.BinOp(left=name_or_attr('b'), op=ast.Mult(), right=name_or_attr('c')),
    op = ast.Add(), right=ast.Num(n=4)
    )
)
 
template4_not_name_or_attr = ast.Expression(body=ast.BinOp(right=name_or_attr('x')))
 
template4_name_wrong = ast.Expression(body=ast.BinOp(
                            left=ast.BinOp(right=name_or_attr('d'))
))