本文整理汇总了Python中ast.arg函数的典型用法代码示例。如果您正苦于以下问题:Python arg函数的具体用法?Python arg怎么用?Python arg使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了arg函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:magnusmorton,项目名称:nest,代码行数:29,代码来源:transformer.py
示例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)
开发者ID:DulithaRanatunga,项目名称:victims-lib-python,代码行数:7,代码来源:test_mnfy.py
示例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])
)
开发者ID:Mause,项目名称:sea_lang,代码行数:27,代码来源:nodes.py
示例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")
开发者ID:3lnc,项目名称:cpython,代码行数:30,代码来源:test_ast.py
示例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)
开发者ID:jmanuel1,项目名称:concat,代码行数:26,代码来源:parse.py
示例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
开发者ID:biolab,项目名称:orange3,代码行数:56,代码来源:owfeatureconstructor.py
示例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
开发者ID:funkybob,项目名称:rattle,代码行数:54,代码来源:parser.py
示例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
开发者ID:jacquev6,项目名称:LowVoltage,代码行数:51,代码来源:variadic.py
示例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=[],
)
开发者ID:jnovinger,项目名称:django-compiling-loader,代码行数:14,代码来源:compiler_state.py
示例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)
开发者ID:jmanuel1,项目名称:concat,代码行数:14,代码来源:parse.py
示例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
开发者ID:fjarri,项目名称:peval,代码行数:25,代码来源:mangler.py
示例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
)
开发者ID:everilae,项目名称:pylisp,代码行数:29,代码来源:compiler.py
示例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)
开发者ID:benekastah,项目名称:jittery-python,代码行数:30,代码来源:compiler.py
示例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)
开发者ID:ChayaSt,项目名称:mdtraj,代码行数:34,代码来源:selection.py
示例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)
开发者ID:serge-sans-paille,项目名称:gast,代码行数:8,代码来源:ast3.py
示例16: _create_ast_lambda
def _create_ast_lambda(names, body):
if sys.version_info >= (3, 0): # change in AST structure for Python 3
args = [ast.arg(arg=name, annotation=None) for name in names]
else:
args = [ast.Name(id=name, ctx=ast.Load()) for name in names]
return ast.Lambda(args=ast.arguments(
args=args, vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=body)
开发者ID:Piruzzolo,项目名称:Mathics,代码行数:8,代码来源:with_numpy.py
示例17: test_lambda
def test_lambda(self):
lamb_expr = Lambda(args=ast.arguments(args=[ast.arg(arg='x')]), body=Num(n=3))
env = Scope([])
k = Done()
val = step(lamb_expr, env, k)[0]
self.assert_(isinstance(val, Lambda))
self.assertEqual(val.args.args[0].arg, 'x')
self.assertEqual(val.body.n, 3)
开发者ID:migeed-z,项目名称:small_step_interpreter,代码行数:8,代码来源:test_step.py
示例18: compile_define
def compile_define(p):
if isinstance(p[1], list):
body = [make_stmt(build_ast(x)) for x in p[2:]]
if isinstance(body[-1], ast.Expr):
body[-1] = ast.Return(body[-1].value)
return ast.FunctionDef(pydent(p[1][0].name), ast.arguments(args=[ast.arg(arg=x.name) for x in p[1][1:]], kwonlyargs=[], defaults=[], kw_defaults=[]), body, [], None)
else:
return ast.Assign([ast.Name(pydent(p[1].name), ast.Store())], build_ast(p[2]))
开发者ID:ghewgill,项目名称:psil,代码行数:8,代码来源:compiler.py
示例19: _lower_array_expr
def _lower_array_expr(lowerer, expr):
'''Lower an array expression built by RewriteArrayExprs.
'''
expr_name = "__numba_array_expr_%s" % (hex(hash(expr)).replace("-", "_"))
expr_args = sorted(set(expr.list_vars()), key=lambda x: x.name)
expr_arg_names = [arg.name for arg in expr_args]
if hasattr(ast, "arg"):
# Should be Python 3.x
ast_args = [ast.arg(arg_name, None)
for arg_name in expr_arg_names]
else:
# Should be Python 2.x
ast_args = [ast.Name(arg_name, ast.Param())
for arg_name in expr_arg_names]
# Parse a stub function to ensure the AST is populated with
# reasonable defaults for the Python version.
ast_module = ast.parse('def {0}(): return'.format(expr_name),
expr_args[0].loc.filename, 'exec')
assert hasattr(ast_module, 'body') and len(ast_module.body) == 1
ast_fn = ast_module.body[0]
ast_fn.args.args = ast_args
ast_fn.body[0].value, namespace = _arr_expr_to_ast(expr.expr)
ast.fix_missing_locations(ast_module)
code_obj = compile(ast_module, expr_args[0].loc.filename, 'exec')
six.exec_(code_obj, namespace)
impl = namespace[expr_name]
context = lowerer.context
builder = lowerer.builder
outer_sig = expr.ty(*(lowerer.typeof(name) for name in expr_arg_names))
inner_sig_args = []
for argty in outer_sig.args:
if isinstance(argty, types.Array):
inner_sig_args.append(argty.dtype)
else:
inner_sig_args.append(argty)
inner_sig = outer_sig.return_type.dtype(*inner_sig_args)
_locals = dict((name, value)
for name, value in namespace.items()
if name.startswith("__ufunc_or_dufunc_"))
cres = context.compile_only_no_cache(builder, impl, inner_sig,
locals=_locals)
class ExprKernel(npyimpl._Kernel):
def generate(self, *args):
arg_zip = zip(args, self.outer_sig.args, inner_sig.args)
cast_args = [self.cast(val, inty, outty)
for val, inty, outty in arg_zip]
result = self.context.call_internal(
builder, cres.fndesc, inner_sig, cast_args)
return self.cast(result, inner_sig.return_type,
self.outer_sig.return_type)
args = [lowerer.loadvar(name) for name in expr_arg_names]
return npyimpl.numpy_ufunc_kernel(
context, builder, outer_sig, args, ExprKernel, explicit_output=False)
开发者ID:arvindchari88,项目名称:newGitTest,代码行数:57,代码来源:array_exprs.py
示例20: _translate_python_arguments
def _translate_python_arguments(self, tvars: list) -> pyast.arguments:
"""Translate typed variable list to python 'arguments' AST"""
return pyast.arguments(
args=list(pyast.arg(arg=tv.var.name, annotation=None) for tv in tvars),
vararg=None,
kwonlyargs=[],
kw_defaults=[],
kwarg=None,
defaults=[])
开发者ID:htk16,项目名称:yapy,代码行数:9,代码来源:translator.py
注:本文中的ast.arg函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论