当前位置: 首页>>代码示例>>Python>>正文


Python parser.parse_entity函数代码示例

本文整理汇总了Python中tensorflow.contrib.autograph.pyct.parser.parse_entity函数的典型用法代码示例。如果您正苦于以下问题:Python parse_entity函数的具体用法?Python parse_entity怎么用?Python parse_entity使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了parse_entity函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_local_scope_info_stack_checks_integrity

  def test_local_scope_info_stack_checks_integrity(self):

    class TestTransformer(transformer.Base):

      def visit_If(self, node):
        self.enter_local_scope()
        return self.generic_visit(node)

      def visit_For(self, node):
        node = self.generic_visit(node)
        self.exit_local_scope()
        return node

    tr = TestTransformer(self._context_for_testing())

    def no_exit(a):
      if a > 0:
        print(a)
      return None

    node, _ = parser.parse_entity(no_exit)
    with self.assertRaises(AssertionError):
      tr.visit(node)

    def no_entry(a):
      for _ in a:
        print(a)

    node, _ = parser.parse_entity(no_entry)
    with self.assertRaises(AssertionError):
      tr.visit(node)
开发者ID:LiuCKind,项目名称:tensorflow,代码行数:31,代码来源:transformer_test.py

示例2: parse_and_analyze

 def parse_and_analyze(self,
                       test_fn,
                       namespace,
                       namer=None,
                       arg_types=None,
                       include_type_analysis=True,
                       owner_type=None,
                       recursive=True):
   node, source = parser.parse_entity(test_fn)
   ctx = context.EntityContext(
       namer=namer or FakeNamer(),
       source_code=source,
       source_file=None,
       namespace=namespace,
       arg_values=None,
       arg_types=arg_types,
       owner_type=owner_type,
       recursive=recursive,
       type_annotation_func=utils.set_element_type)
   node = qual_names.resolve(node)
   node = activity.resolve(node, ctx)
   node = live_values.resolve(node, ctx, {})
   if include_type_analysis:
     node = type_info.resolve(node, ctx)
     node = live_values.resolve(node, ctx, {})
   self.ctx = ctx
   return node
开发者ID:bikong2,项目名称:tensorflow,代码行数:27,代码来源:converter_test_base.py

示例3: function_to_graph

def function_to_graph(f, conversion_map, arg_values, arg_types,
                      owner_type=None):
  """Specialization of `entity_to_graph` for callable functions."""
  node, source = parser.parse_entity(f)
  node = node.body[0]

  namespace = inspect_utils.getnamespace(f)
  _add_self_references(namespace, conversion_map.api_module)
  namer = conversion_map.new_namer(namespace)

  ctx = context.EntityContext(
      namer=namer,
      source_code=source,
      source_file='<fragment>',
      namespace=namespace,
      arg_values=arg_values,
      arg_types=arg_types,
      owner_type=owner_type,
      recursive=conversion_map.recursive,
      type_annotation_func=type_hints.set_element_type)
  node, deps = node_to_graph(node, ctx, conversion_map.nocompile_decorators)

  # TODO(mdan): This somewhat duplicates the call rename logic in call_treest.py
  new_name, did_rename = namer.compiled_function_name(f.__name__, f, owner_type)
  if not did_rename:
    new_name = f.__name__
    if node.name != f.__name__:
      raise NotImplementedError('Strange corner case. Send us offending code!')

  node.name = new_name
  conversion_map.update_name_map(namer)
  # TODO(mdan): Use this at compilation.
  conversion_map.additional_imports.update(deps)

  return node, new_name
开发者ID:syed-ahmed,项目名称:tensorflow,代码行数:35,代码来源:conversion.py

示例4: test_visit_block_postprocessing

  def test_visit_block_postprocessing(self):

    class TestTransformer(transformer.Base):

      def _process_body_item(self, node):
        if isinstance(node, gast.Assign) and (node.value.id == 'y'):
          if_node = gast.If(gast.Name('x', gast.Load(), None), [node], [])
          return if_node, if_node.body
        return node, None

      def visit_FunctionDef(self, node):
        node.body = self.visit_block(
            node.body, after_visit=self._process_body_item)
        return node

    def test_function(x, y):
      z = x
      z = y
      return z

    tr = TestTransformer(self._context_for_testing())

    node, _ = parser.parse_entity(test_function)
    node = tr.visit(node)
    node = node.body[0]

    self.assertEqual(len(node.body), 2)
    self.assertTrue(isinstance(node.body[0], gast.Assign))
    self.assertTrue(isinstance(node.body[1], gast.If))
    self.assertTrue(isinstance(node.body[1].body[0], gast.Assign))
    self.assertTrue(isinstance(node.body[1].body[1], gast.Return))
开发者ID:LiuCKind,项目名称:tensorflow,代码行数:31,代码来源:transformer_test.py

示例5: test_resolve

  def test_resolve(self):

    def test_fn(x):
      """Docstring."""
      return x  # comment

    node, source = parser.parse_entity(test_fn)
    fn_node = node.body[0]
    origin_info.resolve(fn_node, source)

    origin = anno.getanno(fn_node, anno.Basic.ORIGIN)
    self.assertEqual(origin.loc.lineno, 1)
    self.assertEqual(origin.loc.col_offset, 0)
    self.assertEqual(origin.source_code_line, 'def test_fn(x):')
    self.assertIsNone(origin.comment)

    origin = anno.getanno(fn_node.body[0], anno.Basic.ORIGIN)
    self.assertEqual(origin.loc.lineno, 2)
    self.assertEqual(origin.loc.col_offset, 2)
    self.assertEqual(origin.source_code_line, '  """Docstring."""')
    self.assertIsNone(origin.comment)

    origin = anno.getanno(fn_node.body[1], anno.Basic.ORIGIN)
    self.assertEqual(origin.loc.lineno, 3)
    self.assertEqual(origin.loc.col_offset, 2)
    self.assertEqual(origin.source_code_line, '  return x  # comment')
    self.assertEqual(origin.comment, 'comment')
开发者ID:ZhangXinNan,项目名称:tensorflow,代码行数:27,代码来源:origin_info_test.py

示例6: prepare

 def prepare(self,
             test_fn,
             namespace,
             namer=None,
             arg_types=None,
             owner_type=None,
             recursive=True,
             autograph_decorators=()):
   node, source = parser.parse_entity(test_fn)
   if namer is None:
     namer = FakeNamer()
   program_ctx = converter.ProgramContext(
       recursive=recursive,
       autograph_decorators=autograph_decorators,
       partial_types=None,
       autograph_module=None,
       uncompiled_modules=config.DEFAULT_UNCOMPILED_MODULES)
   entity_info = transformer.EntityInfo(
       source_code=source,
       source_file='<fragment>',
       namespace=namespace,
       arg_values=None,
       arg_types=arg_types,
       owner_type=owner_type)
   ctx = converter.EntityContext(namer, entity_info, program_ctx)
   node = converter.standard_analysis(node, ctx, is_initial=True)
   return node, ctx
开发者ID:Brandon1016,项目名称:tensorflow,代码行数:27,代码来源:converter_testing.py

示例7: test_parse_entity

  def test_parse_entity(self):

    def f(x):
      return x + 1

    mod, _ = parser.parse_entity(f)
    self.assertEqual('f', mod.body[0].name)
开发者ID:BhaskarNallani,项目名称:tensorflow,代码行数:7,代码来源:parser_test.py

示例8: function_to_graph

def function_to_graph(f, program_ctx, arg_values, arg_types, owner_type=None):
  """Specialization of `entity_to_graph` for callable functions."""
  node, source = parser.parse_entity(f)
  node = node.body[0]

  namespace = inspect_utils.getnamespace(f)
  _add_self_references(namespace, program_ctx.autograph_module)
  namer = program_ctx.new_namer(namespace)

  entity_info = transformer.EntityInfo(
      source_code=source,
      source_file='<fragment>',
      namespace=namespace,
      arg_values=arg_values,
      arg_types=arg_types,
      owner_type=owner_type)
  context = converter.EntityContext(namer, entity_info, program_ctx)
  node = node_to_graph(node, context)

  # TODO(mdan): This somewhat duplicates the call rename logic in call_treest.py
  new_name, did_rename = namer.compiled_function_name(f.__name__, f, owner_type)
  if not did_rename:
    new_name = f.__name__
    if node.name != f.__name__:
      raise NotImplementedError('Strange corner case. Send us offending code!')

  node.name = new_name
  program_ctx.update_name_map(namer)
  # TODO(mdan): Use this at compilation.

  return node, new_name, namespace
开发者ID:BhaskarNallani,项目名称:tensorflow,代码行数:31,代码来源:conversion.py

示例9: test_basic

 def test_basic(self):
   def test_function():
     a = 0
     return a
   node, _ = parser.parse_entity(test_function)
   node = anf.transform(node.body[0], self._simple_source_info())
   result, _ = compiler.ast_to_object(node)
   self.assertEqual(test_function(), result.test_function())
开发者ID:ZhangXinNan,项目名称:tensorflow,代码行数:8,代码来源:anf_test.py

示例10: assert_body_anfs_as_expected

 def assert_body_anfs_as_expected(self, expected_fn, test_fn):
   # Testing the code bodies only.  Wrapping them in functions so the
   # syntax highlights nicely, but Python doesn't try to execute the
   # statements.
   exp_node, _ = parser.parse_entity(expected_fn)
   node, _ = parser.parse_entity(test_fn)
   node = anf.transform(
       node, self._simple_source_info(), gensym_source=DummyGensym)
   exp_name = exp_node.body[0].name
   # Ignoring the function names in the result because they can't be
   # the same (because both functions have to exist in the same scope
   # at the same time).
   node.body[0].name = exp_name
   self.assert_same_ast(exp_node, node)
   # Check that ANF is idempotent
   node_repeated = anf.transform(
       node, self._simple_source_info(), gensym_source=DummyGensym)
   self.assert_same_ast(node_repeated, node)
开发者ID:ZhangXinNan,项目名称:tensorflow,代码行数:18,代码来源:anf_test.py

示例11: test_entity_scope_tracking

  def test_entity_scope_tracking(self):

    class TestTransformer(transformer.Base):

      # The choice of note to assign to is arbitrary. Using Assign because it's
      # easy to find in the tree.
      def visit_Assign(self, node):
        anno.setanno(node, 'enclosing_entities', self.enclosing_entities)
        return self.generic_visit(node)

      # This will show up in the lambda function.
      def visit_BinOp(self, node):
        anno.setanno(node, 'enclosing_entities', self.enclosing_entities)
        return self.generic_visit(node)

    tr = TestTransformer(self._context_for_testing())

    def test_function():
      a = 0

      class TestClass(object):

        def test_method(self):
          b = 0
          def inner_function(x):
            c = 0
            d = lambda y: (x + y)
            return c, d
          return b, inner_function
      return a, TestClass

    node, _ = parser.parse_entity(test_function)
    node = tr.visit(node)

    test_function_node = node.body[0]
    test_class = test_function_node.body[1]
    test_method = test_class.body[0]
    inner_function = test_method.body[1]
    lambda_node = inner_function.body[1].value

    a = test_function_node.body[0]
    b = test_method.body[0]
    c = inner_function.body[0]
    lambda_expr = lambda_node.body

    self.assertEqual(
        (test_function_node,), anno.getanno(a, 'enclosing_entities'))
    self.assertEqual((test_function_node, test_class, test_method),
                     anno.getanno(b, 'enclosing_entities'))
    self.assertEqual(
        (test_function_node, test_class, test_method, inner_function),
        anno.getanno(c, 'enclosing_entities'))
    self.assertEqual((test_function_node, test_class, test_method,
                      inner_function, lambda_node),
                     anno.getanno(lambda_expr, 'enclosing_entities'))
开发者ID:LiuCKind,项目名称:tensorflow,代码行数:55,代码来源:transformer_test.py

示例12: _parse_and_analyze

 def _parse_and_analyze(self, test_fn):
   node, source = parser.parse_entity(test_fn)
   entity_info = transformer.EntityInfo(
       source_code=source,
       source_file=None,
       namespace={},
       arg_values=None,
       arg_types=None,
       owner_type=None)
   node = qual_names.resolve(node)
   return node, entity_info
开发者ID:BhaskarNallani,项目名称:tensorflow,代码行数:11,代码来源:cfg_test.py

示例13: test_parser_compile_idempotent

  def test_parser_compile_idempotent(self):

    def test_fn(x):
      a = True
      b = ''
      if a:
        b = x + 1
      return b

    self.assertEqual(
        textwrap.dedent(tf_inspect.getsource(test_fn)),
        tf_inspect.getsource(
            compiler.ast_to_object(
                parser.parse_entity(test_fn)[0].body[0])[0].test_fn))
开发者ID:StephenOman,项目名称:tensorflow,代码行数:14,代码来源:compiler_test.py

示例14: _parse_and_analyze

 def _parse_and_analyze(self, test_fn, namespace, arg_types=None):
   arg_types = arg_types or {}
   node, source = parser.parse_entity(test_fn)
   ctx = context.EntityContext(
       namer=None,
       source_code=source,
       source_file=None,
       namespace=namespace,
       arg_values=None,
       arg_types=arg_types,
       owner_type=None,
       recursive=True)
   node = qual_names.resolve(node)
   return node, ctx
开发者ID:KiaraStarlab,项目名称:tensorflow,代码行数:14,代码来源:cfg_test.py

示例15: _parse_and_analyze

 def _parse_and_analyze(self, test_fn):
   node, source = parser.parse_entity(test_fn)
   entity_info = transformer.EntityInfo(
       source_code=source,
       source_file=None,
       namespace={},
       arg_values=None,
       arg_types=None,
       owner_type=None)
   node = qual_names.resolve(node)
   node = activity.resolve(node, entity_info)
   graphs = cfg.build(node)
   node = reaching_definitions.resolve(node, entity_info, graphs,
                                       reaching_definitions.Definition)
   return node
开发者ID:ChristinaEricka,项目名称:tensorflow,代码行数:15,代码来源:reaching_definitions_test.py


注:本文中的tensorflow.contrib.autograph.pyct.parser.parse_entity函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。