本文整理汇总了Python中ast.arg函数的典型用法代码示例。如果您正苦于以下问题:Python arg函数的具体用法?Python arg怎么用?Python arg使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了arg函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_parallel_function
def generate_parallel_function(loop):
# need to generate random string here
name = "nest_fn" + str(id(loop))
args = []
for arg in loop.non_locals:
args.append(ast.arg(arg=arg, annotation=None))
args.append(ast.arg(arg="proc_id", annotation=None))
args = ast.arguments(args=args, vararg=None, kwarg=None, defaults=[], kwonlyargs=[], kw_defaults = [])
# return_values = (ast.parse(str(loop.non_locals))).body[0]
return_values = [ast.Name(id=arg, ctx=ast.Load(), lineno=1, col_offset=0) for arg in loop.non_locals]
print(return_values)
return_stmt = ast.fix_missing_locations(ast.Return(value=return_values, lineno=1, col_offset=0))
return_template = "return ["
for i,arg in enumerate(loop.non_locals):
if i ==0:
return_template += "%s" % arg
else:
return_template += " ,%s" % arg
return_template += "]"
print("return values")
print(return_stmt)
transformed_tree = BoundsTransformer(loop).visit(loop.node)
# transformed_tree = AccessTransformer(loop).visit(transformed_tree)
# transformed_tree.lineno=0
# transformed_tree.col_offset=0
body = [transformed_tree, ast.parse(return_template).body[0]]
dectorator_list = []
fun_def = ast.FunctionDef(name=name, args=args, body=body, decorator_list=[], returns=None)
return ast.fix_missing_locations(fun_def)
示例2: create_arguments
def create_arguments(self, args=[], vararg=None, varargannotation=None,
kwonlyargs=[], kwarg=None, kwargannotation=None, defaults=[],
kw_defaults=[None]):
args = [ast.arg(x, None) for x in args]
kwonlyargs = [ast.arg(x, None) for x in kwonlyargs]
return ast.arguments(args, vararg, varargannotation, kwonlyargs,
kwarg, kwargannotation, defaults, kw_defaults)
示例3: build_init
def build_init(cls):
# build arguments objects
args = [ast.arg('self', None)] + [
ast.arg(arg, None)
for arg in cls._fields
]
body = [
ast.Assign(
[ast.Attribute(
ast.Name('self', ast.Load()),
arg, ast.Store()
)],
ast.Name(arg, ast.Load())
)
for arg in cls._fields
]
function = ast.FunctionDef(
'__init__',
ast.arguments(args, None, [], [], None, []),
body, [], None
)
return ast.fix_missing_locations(
ast.Module([function])
)
示例4: _check_arguments
def _check_arguments(self, fac, check):
def arguments(args=None, vararg=None,
kwonlyargs=None, kwarg=None,
defaults=None, kw_defaults=None):
if args is None:
args = []
if kwonlyargs is None:
kwonlyargs = []
if defaults is None:
defaults = []
if kw_defaults is None:
kw_defaults = []
args = ast.arguments(args, vararg, kwonlyargs, kw_defaults,
kwarg, defaults)
return fac(args)
args = [ast.arg("x", ast.Name("x", ast.Store()))]
check(arguments(args=args), "must have Load context")
check(arguments(kwonlyargs=args), "must have Load context")
check(arguments(defaults=[ast.Num(3)]),
"more positional defaults than args")
check(arguments(kw_defaults=[ast.Num(4)]),
"length of kwonlyargs is not the same as kw_defaults")
args = [ast.arg("x", ast.Name("x", ast.Load()))]
check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]),
"must have Load context")
args = [ast.arg("a", ast.Name("x", ast.Load())),
ast.arg("b", ast.Name("y", ast.Load()))]
check(arguments(kwonlyargs=args,
kw_defaults=[None, ast.Name("x", ast.Store())]),
"must have Load context")
示例5: p_push_primary
def p_push_primary(p): # noqa
"""push_primary : DOLLARSIGN primary"""
arg_list = ast.arguments(
args=[ast.arg(arg='stack', annotation=None),
ast.arg(arg='stash', annotation=None)],
vararg=None,
kwonlyargs=[],
kwarg=None,
defaults=[],
kw_defaults=[])
# print(p[2])
if isinstance(p[2], ast.Name):
pass
# TODO: not a very good check
elif 'stack.pop().' in astunparse.unparse(p[2][0]):
# we are pushing an attributeref
# get rid of the _call to leave the stack.pop().<attr> and concatify it
p[2] = ast.Call(func=ast.Name(id='concatify', ctx=ast.Load()),
args=p[2][0].value.args[0:1], keywords=[])
else:
# print(p[2])
p[2] = ast.Call(func=ast.Name(id='ConcatFunction', ctx=ast.Load()),
args=[ast.Lambda(arg_list, _combine_exprs(p[2]))],
keywords=[])
p[0] = [ast.Expr(_push(p[2]))]
_set_line_info(p)
示例6: make_lambda
def make_lambda(expression, args, env=None):
# type: (ast.Expression, List[str], Dict[str, Any]) -> types.FunctionType
"""
Create an lambda function from a expression AST.
Parameters
----------
expression : ast.Expression
The body of the lambda.
args : List[str]
A list of positional argument names
env : Optional[Dict[str, Any]]
Extra environment to capture in the lambda's closure.
Returns
-------
func : types.FunctionType
"""
# lambda *{args}* : EXPRESSION
lambda_ = ast.Lambda(
args=ast.arguments(
args=[ast.arg(arg=arg, annotation=None) for arg in args],
varargs=None,
varargannotation=None,
kwonlyargs=[],
kwarg=None,
kwargannotation=None,
defaults=[],
kw_defaults=[]),
body=expression.body,
)
lambda_ = ast.copy_location(lambda_, expression.body)
# lambda **{env}** : lambda *{args}*: EXPRESSION
outer = ast.Lambda(
args=ast.arguments(
args=[ast.arg(arg=name, annotation=None) for name in (env or {})],
varargs=None,
varargannotation=None,
kwonlyargs=[],
kwarg=None,
kwargannotation=None,
defaults=[],
kw_defaults=[],
),
body=lambda_,
)
exp = ast.Expression(body=outer, lineno=1, col_offset=0)
ast.fix_missing_locations(exp)
GLOBALS = __GLOBALS.copy()
GLOBALS["__builtins__"] = {}
# pylint: disable=eval-used
fouter = eval(compile(exp, "<lambda>", "eval"), GLOBALS)
assert isinstance(fouter, types.FunctionType)
finner = fouter(**env)
assert isinstance(finner, types.FunctionType)
return finner
示例7: build_class
def build_class():
"""
Constructs a :class:`ast.ClassDef` node that wraps the entire template
file. The class will have an entry function ``root`` with:
.. function:: root(context)
Starts the template parsing with the given context.
:returns: Returns a generator of strings that can be joined to the
rendered template.
:returns: a 2-tuple with the class and the entry function
"""
args = {}
if PY3:
args.update({
'args': [
ast.arg(arg='self', annotation=None),
ast.arg(arg='context', annotation=None),
],
'kwonlyargs': [],
'kw_defaults': [],
})
else:
args['args'] = [
ast.Name(id='self', ctx=ast.Param()),
ast.Name(id='context', ctx=ast.Param())
]
root_func = ast.FunctionDef(
name='root',
args=ast.arguments(
vararg=None,
kwarg=None,
defaults=[],
**args
),
body=[
# we add an empty string to guarantee for a string and generator on
# root level
build_yield(ast.Str(s=''))
],
decorator_list=[]
)
klass = ast.ClassDef(
name='Template',
bases=[ast.Name(id='object', ctx=ast.Load())],
keywords=[],
starargs=None,
kwargs=None,
body=[root_func],
decorator_list=[]
)
return klass, root_func
示例8: decorator
def decorator(wrapped):
spec = inspect.getargspec(wrapped)
name = wrapped.__name__
assert spec.varargs is not None
# Example was generated with print ast.dump(ast.parse("def f(a, b, *args, **kwds): return call_wrapped((a, b), args, kwds)"), include_attributes=True)
# http://code.activestate.com/recipes/578353-code-to-source-and-back/ helped a lot
# http://stackoverflow.com/questions/10303248#29927459
if sys.hexversion < 0x03000000:
wrapper_ast_args = ast.arguments(
args=[ast.Name(id=a, ctx=ast.Param(), lineno=1, col_offset=0) for a in spec.args],
vararg=spec.varargs,
kwarg=spec.keywords,
defaults=[]
)
else:
wrapper_ast_args = ast.arguments(
args=[ast.arg(arg=a, annotation=None, lineno=1, col_offset=0) for a in spec.args],
vararg=None if spec.varargs is None else ast.arg(arg=spec.varargs, annotation=None, lineno=1, col_offset=0),
kwonlyargs=[],
kw_defaults=[],
kwarg=None if spec.keywords is None else ast.arg(arg=spec.keywords, annotation=None, lineno=1, col_offset=0),
defaults=[]
)
wrapper_ast = ast.Module(body=[ast.FunctionDef(
name=name,
args=wrapper_ast_args,
body=[ast.Return(value=ast.Call(
func=ast.Name(id="wrapped", ctx=ast.Load(), lineno=1, col_offset=0),
args=[ast.Name(id=a, ctx=ast.Load(), lineno=1, col_offset=0) for a in spec.args],
keywords=[],
starargs=ast.Call(
func=ast.Name(id="flatten", ctx=ast.Load(), lineno=1, col_offset=0),
args=[ast.Name(id=spec.varargs, ctx=ast.Load(), lineno=1, col_offset=0)],
keywords=[], starargs=None, kwargs=None, lineno=1, col_offset=0
),
kwargs=None if spec.keywords is None else ast.Name(id=spec.keywords, ctx=ast.Load(), lineno=1, col_offset=0),
lineno=1, col_offset=0
), lineno=1, col_offset=0)],
decorator_list=[],
lineno=1,
col_offset=0
)])
wrapper_code = [c for c in compile(wrapper_ast, "<ast_in_variadic_py>", "exec").co_consts if isinstance(c, types.CodeType)][0]
wrapper = types.FunctionType(wrapper_code, {"wrapped": wrapped, "flatten": flatten}, argdefs=spec.defaults)
functools.update_wrapper(wrapper, wrapped)
if wrapper.__doc__ is not None:
wrapper.__doc__ = "Note that this function is variadic. See :ref:`variadic-functions`.\n\n" + wrapper.__doc__
return wrapper
示例9: make_function_def
def make_function_def(self, body, name):
return ast.FunctionDef(
name=name,
body=body,
args=ast.arguments(
args=[
ast.arg(arg='self'),
ast.arg(arg=CONTEXT_ARG_NAME),
],
kwonlyargs=[],
kw_defaults=[],
defaults=[]),
decorator_list=[],
)
示例10: p_funcdef
def p_funcdef(p): # noqa
"""funcdef : DEF funcname COLON suite"""
arg_list = ast.arguments(
args=[ast.arg(arg='stack', annotation=None),
ast.arg(arg='stash', annotation=None)],
vararg=None,
kwonlyargs=[],
kwarg=None,
defaults=[],
kw_defaults=[])
p[0] = ast.FunctionDef(p[2], arg_list, p[4],
[ast.Name(id='ConcatFunction', ctx=ast.Load())],
None)
_set_line_info(p)
示例11: _visit_local
def _visit_local(gen_sym, node, to_mangle, mangled):
"""
Replacing known variables with literal values
"""
is_name = type(node) == ast.Name
node_id = node.id if is_name else node.arg
if node_id in to_mangle:
if node_id in mangled:
mangled_id = mangled[node_id]
else:
mangled_id, gen_sym = gen_sym('mangled')
mangled = mangled.set(node_id, mangled_id)
if is_name:
new_node = ast.Name(id=mangled_id, ctx=node.ctx)
else:
new_node = ast.arg(arg=mangled_id, annotation=node.annotation)
else:
new_node = node
return gen_sym, new_node, mangled
示例12: _functiondef
def _functiondef(self, name, args, body, lineno, col_offset):
args = [ast.arg(arg=c.car.name, annotation=None) for c in args]
body = self._body(c.car for c in body)
# Rewrite return
body[-1] = ast.Return(
value=body[-1].value,
lineno=body[-1].lineno,
col_offset=body[-1].col_offset
)
return ast.FunctionDef(
name=name,
args=ast.arguments(
args=args,
defaults=[],
kw_defaults=[],
kwarg=None,
kwargannotation=None,
kwonlyargs=[],
vararg=None,
varargannotation=None
),
body=body,
returns=None,
decorator_list=[],
lineno=lineno,
col_offset=col_offset
)
示例13: compile_Module
def compile_Module(self, node):
self.is_builtins = self.module_name == "builtins"
body = node.body
if self.print_module_result:
try:
last_body_item = body[-1]
except IndexError:
last_body_item = ast.Name("None", ast.Load())
print_fn = ast.Name("print", ast.Load())
last_body_item = ast.Call(print_fn, [last_body_item], None, None, None)
body = body[:-1] + [last_body_item]
if not self.bare and not self.is_builtins:
module_name = ast.Str(self.module_name)
args = ast.arguments([ast.arg(self.local_module_name.id, None)], None, None, None, None, None, None, None)
func = ast.FunctionDef(name = '', args = args, body = body, decorator_list = [], returns = None)
to_call = ast.Name("__registermodule__", ast.Load())
call = ast.Call(to_call, [module_name, func], None, None, None)
result = self.compile_node(call)
else:
context = self.context_stack.new()
result = self.compile_node(body)
result = self.compile_statement_list([context.get_vars(True), JSCode(result)])
self.context_stack.pop()
if self.is_builtins:
self.main_compiler.modules = [result] + self.main_compiler.modules
else:
self.main_compiler.modules.append(result)
示例14: __call__
def __call__(self, selection):
if not self.is_initialized:
self._initialize()
try:
parse_result = self.expression.parseString(selection, parseAll=True)
except ParseException as e:
msg = str(e)
lines = ["%s: %s" % (msg, selection),
" " * (12 + len("%s: " % msg) + (e.loc)) + "^^^"]
raise ValueError('\n'.join(lines))
# Change __ATOM__ in function bodies. It must bind to the arg
# name specified below (i.e. 'atom')
astnode = self.transformer.visit(deepcopy(parse_result[0].ast()))
if PY2:
args = [ast.Name(id='atom', ctx=ast.Param())]
signature = ast.arguments(args=args, vararg=None, kwarg=None,
defaults=[])
else:
args = [ast.arg(arg='atom', annotation=None)]
signature = ast.arguments(args=args, vararg=None, kwarg=None,
kwonlyargs=[], defaults=[],
kw_defaults=[])
func = ast.Expression(body=ast.Lambda(signature, astnode))
source = codegen.to_source(astnode)
expr = eval(
compile(ast.fix_missing_locations(func), '<string>', mode='eval'),
SELECTION_GLOBALS)
return _ParsedSelection(expr, source, astnode)
示例15: _make_arg
def _make_arg(self, node):
if node is None:
return None
new_node = ast.arg(
self._visit(node.id),
self._visit(node.annotation),
)
return ast.copy_location(new_node, node)