本文整理汇总了Python中tensorflow.contrib.py2tf.pyct.anno.hasanno函数的典型用法代码示例。如果您正苦于以下问题:Python hasanno函数的具体用法?Python hasanno怎么用?Python hasanno使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了hasanno函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: visit_Name
def visit_Name(self, node):
self.generic_visit(node)
if isinstance(node.ctx, gast.Load):
assert anno.hasanno(node, NodeAnno.IS_LOCAL), node
symbol_is_local = anno.getanno(node, NodeAnno.IS_LOCAL)
assert anno.hasanno(node, NodeAnno.IS_MODIFIED_SINCE_ENTRY), node
symbol_is_modified = anno.getanno(node, NodeAnno.IS_MODIFIED_SINCE_ENTRY)
assert anno.hasanno(node, NodeAnno.IS_PARAM), node
symbol_is_param = anno.getanno(node, NodeAnno.IS_PARAM)
if not symbol_is_local and not symbol_is_param:
if node.id in self.literals:
anno.setanno(node, 'live_val', self.literals[node.id])
# TODO(mdan): Could live values have FQNs? i.e. 'a'.join()
elif node.id in self.context.namespace:
obj = self.context.namespace[node.id]
anno.setanno(node, 'live_val', obj)
anno.setanno(node, 'fqn', (obj.__name__,))
else:
pass
# TODO(mdan): Should we raise an error here?
# Can encounter this when:
# * a symbol truly lacks reference
# * a symbol is new, like the new name of a function we just renamed.
else:
pass
# TODO(mdan): Attempt to trace its value through the local chain.
# TODO(mdan): Use type annotations as fallback.
if not symbol_is_modified:
if node.id in self.context.arg_values:
obj = self.context.arg_values[node.id]
anno.setanno(node, 'live_val', obj)
anno.setanno(node, 'fqn', (obj.__class__.__name__,))
return node
开发者ID:DILASSS,项目名称:tensorflow,代码行数:35,代码来源:live_values.py
示例2: visit_Call
def visit_Call(self, node):
# If the function is wrapped by one of the marker decorators,
# consider it graph ready.
if anno.hasanno(node.func, 'live_val'):
target_entity = anno.getanno(node.func, 'live_val')
if target_entity in self.nocompile_decorators:
if len(node.args) < 1:
raise ValueError(
'Found call to decorator function "%s", but it had no arguments. '
'A decorator needs at least an argument.')
anno.setanno(node.args[0], 'graph_ready', True)
self.generic_visit(node)
if anno.hasanno(node.func, 'live_val'):
target_entity = anno.getanno(node.func, 'live_val')
if anno.hasanno(node.func, 'fqn'):
target_fqn = anno.getanno(node.func, 'fqn')
if self._function_is_compilable(target_entity):
node = self._rename_compilable_function(node)
elif target_fqn in KNOWN_NUMPY_FUNCTIONS:
# TODO(mdan): Should we replace these with equivalent TF ops instead?
node = self._wrap_to_py_func_single_return(node, target_fqn)
else:
raise NotImplementedError(
'py_func with return values (unknown function)')
else:
if self.context.recursive:
node = self._insert_dynamic_conversion(node)
else:
# Unresolved functions are allowed in non-recursive mode.
pass
return node
开发者ID:houhaichao830,项目名称:tensorflow,代码行数:32,代码来源:call_trees.py
示例3: _rename_compilable_function
def _rename_compilable_function(self, node):
assert anno.hasanno(node.func, 'live_val')
assert anno.hasanno(node.func, 'fqn')
target_entity = anno.getanno(node.func, 'live_val')
target_fqn = anno.getanno(node.func, 'fqn')
if not self._should_compile(node, target_fqn):
return node
if anno.hasanno(node, 'is_constructor'):
new_name = self.context.namer.compiled_class_name(
target_fqn, live_entity=target_entity)
do_rename = True
else:
owner_type = self._determine_function_owner(target_entity)
new_name, do_rename = self.context.namer.compiled_function_name(
target_fqn, live_entity=target_entity, owner_type=owner_type)
if do_rename:
if target_entity is not None:
if tf_inspect.ismethod(target_entity):
# The renaming process will transform it into a regular function.
# TODO(mdan): Is this complete? How does it work with nested members?
node.args = [node.func.value] + node.args
node.func = templates.replace('func_name', func_name=new_name)[0]
return node
开发者ID:dananjayamahesh,项目名称:tensorflow,代码行数:26,代码来源:call_trees.py
示例4: visit_Call
def visit_Call(self, node):
# If the function is wrapped by one of the marker decorators,
# consider it graph ready.
if anno.hasanno(node.func, 'live_val'):
target_obj = anno.getanno(node.func, 'live_val')
if target_obj in self.nocompile_decorators:
if len(node.args) < 1:
raise ValueError(
'Found call to decorator function "%s", but it had no arguments. '
'A decorator needs at least an argument.')
anno.setanno(node.args[0], 'graph_ready', True)
self.generic_visit(node)
if anno.hasanno(node.func, 'live_val'):
target_obj = anno.getanno(node.func, 'live_val')
if self._function_is_compilable(target_obj):
node = self._rename_compilable_function(node)
else:
raise NotImplementedError('py_func with return values')
elif anno.hasanno(node.func, 'type_fqn'):
node = self._rename_member_function_of_known_type(node)
else:
raise NotImplementedError(
'Member function call (of unknown type): %s.' % node.func.id)
return node
开发者ID:craffel,项目名称:tensorflow,代码行数:25,代码来源:call_trees.py
示例5: visit_Call
def visit_Call(self, node):
target = node.func
if not anno.hasanno(target, 'live_val'):
if not isinstance(target, gast.Attribute):
# Suspecting this pattern would reach here:
# foo = bar
# foo()
raise ValueError('Dont know how to handle dynamic functions.')
if not isinstance(target.value, gast.Name):
# Possible example of this kind:
# foo = module.Foo()
# foo.bar.baz()
# TODO(mdan): This should be doable by using the FQN.
raise ValueError('Dont know how to handle object properties yet.')
# In the example below, object_source is 'tr.train.Optimizer()':
# opt = tf.train.Optimizer()
# opt.foo()
if self.scope.hasval(target.value.id):
object_source = self.scope.getval(target.value.id)
if not anno.hasanno(object_source, 'type'):
raise ValueError('Could not determine type of "%s". Is it dynamic?' %
(target.value.id))
anno.setanno(target, 'type', anno.getanno(object_source, 'type'))
anno.setanno(target, 'type_fqn', anno.getanno(object_source,
'type_fqn'))
else:
# TODO(mdan): Figure out what could the user do to get past this.
raise ValueError('No info on "%s". Is it dynamically built?' %
(target.value.id))
self.generic_visit(node)
return node
开发者ID:craffel,项目名称:tensorflow,代码行数:31,代码来源:type_info.py
示例6: visit_Call
def visit_Call(self, node):
if anno.hasanno(node.func, 'live_val'):
# Symbols targeted by the "set_type" marker function are assigned the data
# type that it specified.
if (anno.getanno(node.func, 'live_val') is
self.context.type_annotation_func):
# Expecting the actual type to be the second argument.
if len(node.args) != 2:
raise ValueError('"%s" must have exactly two parameters'
% self.context.type_annotation_func)
if not anno.hasanno(node.args[0], anno.Basic.QN):
raise ValueError('the first argument of "%s" must by a symbol'
% self.context.type_annotation_func)
if not anno.hasanno(node.args[1], 'live_val'):
raise ValueError(
'the second argument of "%s" must be statically resolvable' %
self.context.type_annotation_func)
target_symbol = anno.getanno(node.args[0], anno.Basic.QN)
element_type = anno.getanno(node.args[1], 'live_val')
# Find the definition of this symbol and annotate it with the given
# data type. That in turn will cause future uses of the symbol
# to receive the same type annotation.
definition = self.scope.getval(target_symbol)
anno.setanno(node, 'element_type', element_type)
anno.setanno(definition, 'element_type', element_type)
# TODO(mdan): Should we update references between definition and here?
return self.generic_visit(node)
开发者ID:AndrewTwinz,项目名称:tensorflow,代码行数:27,代码来源:type_info.py
示例7: visit_Attribute
def visit_Attribute(self, node):
self.generic_visit(node)
if anno.hasanno(node.value, 'live_val'):
assert anno.hasanno(node.value, 'fqn')
parent_object = anno.getanno(node.value, 'live_val')
if not hasattr(parent_object, node.attr):
raise AttributeError('%s has no attribute %s' % (parent_object,
node.attr))
anno.setanno(node, 'parent_type', type(parent_object))
anno.setanno(node, 'live_val', getattr(parent_object, node.attr))
anno.setanno(node, 'fqn', anno.getanno(node.value, 'fqn') + (node.attr,))
# TODO(mdan): Investigate the role built-in annotations can play here.
elif anno.hasanno(node.value, 'type'):
parent_type = anno.getanno(node.value, 'type')
if hasattr(parent_type, node.attr):
# This should hold for static members like methods.
# This would not hold for dynamic members like function attributes.
# For the dynamic case, we simply leave the node without an annotation,
# and let downstream consumers figure out what to do.
anno.setanno(node, 'parent_type', parent_type)
anno.setanno(node, 'live_val', getattr(parent_type, node.attr))
anno.setanno(node, 'fqn',
anno.getanno(node.value, 'type_fqn') + (node.attr,))
elif isinstance(node.value, gast.Name):
stem_name = node.value
# All nonlocal symbols should be fully resolved.
assert anno.hasanno(stem_name, NodeAnno.IS_LOCAL), stem_name
# TODO(mdan): Figure out what to do when calling attribute on local object
# Maybe just leave as-is?
return node
开发者ID:AndrewTwinz,项目名称:tensorflow,代码行数:30,代码来源:live_values.py
示例8: visit_Name
def visit_Name(self, node):
self.generic_visit(node)
if isinstance(node.ctx, gast.Load):
assert anno.hasanno(node, 'is_local'), node
symbol_is_local = anno.getanno(node, 'is_local')
assert anno.hasanno(node, 'is_modified_since_entry'), node
symbol_is_modified = anno.getanno(node, 'is_modified_since_entry')
assert anno.hasanno(node, 'is_param'), node
symbol_is_param = anno.getanno(node, 'is_param')
if not symbol_is_local and not symbol_is_param:
if node.id in self.literals:
anno.setanno(node, 'live_val', self.literals[node.id])
# TODO(mdan): Could live values have FQNs? i.e. 'a'.join()
elif node.id in self.context.namespace:
obj = self.context.namespace[node.id]
anno.setanno(node, 'live_val', obj)
anno.setanno(node, 'fqn', (obj.__name__,))
else:
raise ValueError('Could not resolve symbol "%s".' % node.id)
else:
pass
# TODO(mdan): Attempt to trace its value through the local chain.
# TODO(mdan): Use type annotations as fallback.
if not symbol_is_modified:
if node.id in self.context.arg_values:
obj = self.context.arg_values[node.id]
anno.setanno(node, 'live_val', obj)
anno.setanno(node, 'fqn', (obj.__class__.__name__,))
return node
开发者ID:ClowJ,项目名称:tensorflow,代码行数:31,代码来源:live_values.py
示例9: visit_Call
def visit_Call(self, node):
# If the function is wrapped by one of the marker decorators,
# consider it graph ready.
if anno.hasanno(node.func, 'live_val'):
target_entity = anno.getanno(node.func, 'live_val')
if target_entity in self.nocompile_decorators:
if len(node.args) < 1:
raise ValueError(
'Found call to decorator function "%s", but it had no arguments. '
'A decorator needs at least an argument.')
anno.setanno(node.args[0], 'graph_ready', True)
self.generic_visit(node)
if anno.hasanno(node.func, 'live_val'):
target_entity = anno.getanno(node.func, 'live_val')
if self._function_is_compilable(target_entity):
node = self._rename_compilable_function(node)
else:
raise NotImplementedError('py_func with return values')
else:
if self.context.recursive:
raise NotImplementedError('Could not resolve target function.')
else:
# TODO(mdan): Double check. Is this reachable code?
pass
return node
开发者ID:dananjayamahesh,项目名称:tensorflow,代码行数:26,代码来源:call_trees.py
示例10: _try_resolve_target
def _try_resolve_target(self, node):
"""Works for methods of objects of known type."""
if anno.hasanno(node, 'live_val'):
return anno.getanno(node, 'live_val')
if isinstance(node, gast.Attribute) and anno.hasanno(node, 'type'):
member = getattr(anno.getanno(node, 'type'), node.attr)
return member
return None
开发者ID:craffel,项目名称:tensorflow,代码行数:8,代码来源:call_trees.py
示例11: test_copyanno
def test_copyanno(self):
node_1 = ast.Name()
anno.setanno(node_1, 'foo', 3)
node_2 = ast.Name()
anno.copyanno(node_1, node_2, 'foo')
anno.copyanno(node_1, node_2, 'bar')
self.assertTrue(anno.hasanno(node_2, 'foo'))
self.assertFalse(anno.hasanno(node_2, 'bar'))
开发者ID:AndrewTwinz,项目名称:tensorflow,代码行数:10,代码来源:anno_test.py
示例12: test_basic
def test_basic(self):
node = ast.Name()
self.assertFalse(anno.hasanno(node, 'foo'))
with self.assertRaises(AttributeError):
anno.getanno(node, 'foo')
anno.setanno(node, 'foo', 3)
self.assertTrue(anno.hasanno(node, 'foo'))
self.assertEqual(3, anno.getanno(node, 'foo'))
开发者ID:ClowJ,项目名称:tensorflow,代码行数:10,代码来源:anno_test.py
示例13: _try_resolve_target
def _try_resolve_target(self, node):
"""Works for methods of objects of known type."""
if anno.hasanno(node, 'live_val'):
return anno.getanno(node, 'live_val')
if isinstance(node, gast.Attribute) and anno.hasanno(node, 'type'):
owner_type = anno.getanno(node, 'type')
if hasattr(owner_type, node.attr):
return getattr(owner_type, node.attr)
else:
raise ValueError('Type "%s" has not attribute "%s". Is it dynamic?' %
(owner_type, node.attr))
return None
开发者ID:dananjayamahesh,项目名称:tensorflow,代码行数:12,代码来源:call_trees.py
示例14: _rename_compilable_function
def _rename_compilable_function(self, node):
assert anno.hasanno(node.func, 'live_val')
assert anno.hasanno(node.func, 'fqn')
target_obj = anno.getanno(node.func, 'live_val')
target_fqn = anno.getanno(node.func, 'fqn')
if not self._should_compile(target_fqn):
return node
new_name = self.namer.compiled_function_name(
'.'.join(target_fqn), live_object=target_obj)
node.func = gast.Name(id=new_name, ctx=gast.Load(), annotation=None)
return node
开发者ID:Lin-jipeng,项目名称:tensorflow,代码行数:13,代码来源:call_trees.py
示例15: visit_Call
def visit_Call(self, node):
self.generic_visit(node)
if anno.hasanno(node.func, 'live_val'):
target_obj = anno.getanno(node.func, 'live_val')
if self._function_is_compilable(target_obj):
node = self._rename_compilable_function(node)
else:
raise NotImplementedError('py_func with return values')
elif anno.hasanno(node.func, 'type_fqn'):
node = self._rename_member_function_of_known_type(node)
else:
raise NotImplementedError(
'Member function call (of unknown type): %s.' % node.func.id)
return node
开发者ID:andrewharp,项目名称:tensorflow,代码行数:14,代码来源:call_trees.py
示例16: _track_symbol
def _track_symbol(self, node):
# This can happen when we have an attribute (or subscript) on a function
# call. Example: a().b
if not anno.hasanno(node, anno.Basic.QN):
return
qn = anno.getanno(node, anno.Basic.QN)
if isinstance(node.ctx, gast.Store):
self.scope.mark_write(qn)
elif isinstance(node.ctx, gast.Load):
self.scope.mark_read(qn)
elif isinstance(node.ctx, gast.Param):
# Param contexts appear in function defs, so they have the meaning of
# defining a variable.
# TODO(mdan): This bay be incorrect with nested functions.
# For nested functions, we'll have to add the notion of hiding args from
# the parent scope, not writing to them.
self.scope.mark_creation(qn)
self.scope.mark_param(qn)
else:
raise ValueError('Unknown context %s for node %s.' % (type(node.ctx), qn))
anno.setanno(node, NodeAnno.IS_LOCAL, self.scope.has(qn))
anno.setanno(node, NodeAnno.IS_MODIFIED_SINCE_ENTRY,
self.scope.is_modified_since_entry(qn))
anno.setanno(node, NodeAnno.IS_PARAM, self.scope.is_param(qn))
if self._in_return_statement:
self.scope.mark_returned(qn)
开发者ID:AndrewTwinz,项目名称:tensorflow,代码行数:29,代码来源:activity.py
示例17: _process_variable_assignment
def _process_variable_assignment(self, source, targets):
if isinstance(source, gast.Call):
func = source.func
if anno.hasanno(func, 'live_val'):
func_obj = anno.getanno(func, 'live_val')
if tf_inspect.isclass(func_obj):
anno.setanno(source, 'is_constructor', True)
anno.setanno(source, 'type', func_obj)
anno.setanno(source, 'type_fqn', anno.getanno(func, 'fqn'))
# TODO(mdan): Raise an error if constructor has side effects.
# We can have a whitelist of no-side-effects constructors.
# We can also step inside the constructor and further analyze.
for t in targets:
if isinstance(t, gast.Tuple):
for i, e in enumerate(t.elts):
self.scope.setval(e.id,
gast.Subscript(
source, gast.Index(i), ctx=gast.Store()))
elif isinstance(t, gast.Name):
self.scope.setval(t.id, source)
elif isinstance(t, gast.Attribute):
if not (isinstance(t.value, gast.Name) and t.value.id == 'self'):
raise ValueError(
'Dont know how to handle assignment to attributes of objects'
' other than "self": [%s].%s' % (t.value, t.attr))
else:
raise ValueError('Dont know how to handle assignment to %s' % t)
开发者ID:craffel,项目名称:tensorflow,代码行数:28,代码来源:type_info.py
示例18: _wrap_to_py_func_no_return
def _wrap_to_py_func_no_return(self, node):
func_qn = anno.getanno(node.func, anno.Basic.QN)
args_scope = anno.getanno(node, NodeAnno.ARGS_SCOPE)
wrapper_name = self.context.namer.new_symbol(func_qn.ssf(),
args_scope.referenced)
wrapper_args = []
for arg in node.args:
if anno.hasanno(arg, anno.Basic.QN):
arg_qn = anno.getanno(arg, anno.Basic.QN)
else:
arg_qn = qual_names.QN('arg')
wrapper_args.append(
self.context.namer.new_symbol(arg_qn.ssf(), args_scope.referenced))
# TODO(mdan): Properly handle varargs, kwargs, etc.
# TODO(mdan): This is best handled as a dynamic dispatch.
# That way we can separate tensors from non-tensor args.
template = """
def wrapper(wrapper_args):
call(wrapper_args)
return 1
tf.py_func(wrapper, original_args, [tf.int64])
"""
wrapper_def, call_expr = templates.replace(
template,
call=node.func,
wrapper=wrapper_name,
original_args=gast.List(elts=node.args, ctx=None),
wrapper_args=wrapper_args)
anno.setanno(wrapper_def, anno.Basic.SKIP_PROCESSING, True)
return (wrapper_def, call_expr)
开发者ID:japrogramer,项目名称:tensorflow,代码行数:31,代码来源:call_trees.py
示例19: test_nested_assignment
def test_nested_assignment(self):
def test_fn(foo):
a, (b, c) = foo
return a, b, c
node = self._parse_and_analyze(test_fn, {'foo': (1, 2, 3)})
lhs = node.body[0].body[1].value.elts
a = lhs[0]
b = lhs[1]
c = lhs[2]
# TODO(mdan): change these once we have the live values propagating
# correctly
self.assertFalse(anno.hasanno(a, 'live_val'))
self.assertFalse(anno.hasanno(b, 'live_val'))
self.assertFalse(anno.hasanno(c, 'live_val'))
开发者ID:AndrewTwinz,项目名称:tensorflow,代码行数:16,代码来源:type_info_test.py
示例20: _visit_and_reindent
def _visit_and_reindent(self, nodes):
new_nodes = []
current_dest = new_nodes
alias_map = {}
reindent_requested = False
for n in nodes:
n = self.visit(n)
# NOTE: the order in which these statements execute is important; in
# particular, watch out for ending up with cycles in the AST.
if alias_map:
n = ast_util.rename_symbols(n, alias_map)
if isinstance(n, (list, tuple)):
current_dest.extend(n)
else:
current_dest.append(n)
if anno.hasanno(n, anno.Basic.INDENT_BLOCK_REMAINDER):
reindent_requested = True
new_dest, new_alias_map = anno.getanno(
n, anno.Basic.INDENT_BLOCK_REMAINDER)
anno.delanno(n, anno.Basic.INDENT_BLOCK_REMAINDER)
new_alias_map.update(alias_map)
alias_map = new_alias_map
current_dest = new_dest
if reindent_requested and not current_dest:
# TODO(mdan): There may still be something that could be done.
raise ValueError('Unable to insert statement into the computation flow: '
'it is not followed by any computation which '
'the statement could gate.')
return new_nodes
开发者ID:AndrewTwinz,项目名称:tensorflow,代码行数:29,代码来源:side_effect_guards.py
注:本文中的tensorflow.contrib.py2tf.pyct.anno.hasanno函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论