• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python compiler.ast_to_object函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: test_ast_to_object

  def test_ast_to_object(self):
    node = gast.FunctionDef(
        name='f',
        args=gast.arguments(
            args=[gast.Name('a', gast.Param(), None)],
            vararg=None,
            kwonlyargs=[],
            kwarg=None,
            defaults=[],
            kw_defaults=[]),
        body=[
            gast.Return(
                gast.BinOp(
                    op=gast.Add(),
                    left=gast.Name('a', gast.Load(), None),
                    right=gast.Num(1)))
        ],
        decorator_list=[],
        returns=None)

    module, source = compiler.ast_to_object(node)

    expected_source = """
      def f(a):
        return a + 1
    """
    self.assertEqual(
        textwrap.dedent(expected_source).strip(),
        source.strip())
    self.assertEqual(2, module.f(1))
    with open(module.__file__, 'r') as temp_output:
      self.assertEqual(
          textwrap.dedent(expected_source).strip(),
          temp_output.read().strip())
开发者ID:AnishShah,项目名称:tensorflow,代码行数:34,代码来源:compiler_test.py


示例2: compiled

  def compiled(self, node, namespace, *symbols):
    source = None

    self.dynamic_calls = []
    def converted_call(*args):
      """Mock version of api.converted_call."""
      self.dynamic_calls.append(args)
      return 7

    try:
      result, source = compiler.ast_to_object(node, include_source_map=True)

      # TODO(mdan): Move this into self.prepare()
      result.tf = self.make_fake_mod('fake_tf', *symbols)
      fake_ag = self.make_fake_mod('fake_ag', converted_call,
                                   converter.ConversionOptions)
      fake_ag.__dict__.update(operators.__dict__)
      fake_ag.__dict__.update(special_functions.__dict__)
      fake_ag.__dict__['utils'] = utils
      fake_ag.__dict__['rewrite_graph_construction_error'] = (
          errors.rewrite_graph_construction_error)
      fake_ag.__dict__['function_scope'] = function_wrapping.function_scope
      result.__dict__['ag__'] = fake_ag
      for k, v in namespace.items():
        result.__dict__[k] = v
      yield result
    except Exception:  # pylint:disable=broad-except
      if source is None:
        print('Offending AST:\n%s' % pretty_printer.fmt(node, color=False))
      else:
        print('Offending compiled code:\n%s' % source)
      raise
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:32,代码来源:converter_testing.py


示例3: compiled

  def compiled(self, node, namespace, *symbols):
    source = None

    self.dynamic_calls = []
    def converted_call(*args):
      """Mock version of api.converted_call."""
      self.dynamic_calls.append(args[3:])  # args only; see api.converted_call
      return RESULT_OF_MOCK_CONVERTED_CALL

    try:
      result, source, source_map = compiler.ast_to_object(
          node, include_source_map=True)
      # TODO(mdan): Move the unparsing from converter into pyct and reuse here.

      # TODO(mdan): Move this into self.prepare()
      result.tf = self.make_fake_mod('fake_tf', *symbols)
      fake_ag = self.make_fake_mod('fake_ag', converted_call,
                                   converter.ConversionOptions)
      fake_ag.__dict__.update(operators.__dict__)
      fake_ag.__dict__.update(special_functions.__dict__)
      fake_ag.ConversionOptions = converter.ConversionOptions
      fake_ag.Feature = converter.Feature
      fake_ag.utils = utils
      fake_ag.function_scope = function_wrapping.function_scope
      result.ag__ = fake_ag
      result.ag_source_map__ = source_map
      for k, v in namespace.items():
        result.__dict__[k] = v
      yield result
    except Exception:  # pylint:disable=broad-except
      if source is None:
        print('Offending AST:\n%s' % pretty_printer.fmt(node, color=False))
      else:
        print('Offending compiled code:\n%s' % source)
      raise
开发者ID:aritratony,项目名称:tensorflow,代码行数:35,代码来源:converter_testing.py


示例4: test_codegen_gens

 def test_codegen_gens(self):
   np.random.seed(0)
   for _ in range(1000):
     node = codegen.generate_random_functiondef()
     fn = compiler.ast_to_object(node)
     self.assertIsNotNone(
         fn, 'Generated invalid AST that could not convert to source.')
开发者ID:AnishShah,项目名称:tensorflow,代码行数:7,代码来源:codegen_test.py


示例5: 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:AnishShah,项目名称:tensorflow,代码行数:8,代码来源:anf_test.py


示例6: test_keywords_to_dict

 def test_keywords_to_dict(self):
   keywords = parser.parse_expression('f(a=b, c=1, d=\'e\')').keywords
   d = ast_util.keywords_to_dict(keywords)
   # Make sure we generate a usable dict node by attaching it to a variable and
   # compiling everything.
   node = parser.parse_str('def f(b): pass').body[0]
   node.body.append(ast.Return(d))
   result, _ = compiler.ast_to_object(node)
   self.assertDictEqual(result.f(3), {'a': 3, 'c': 1, 'd': 'e'})
开发者ID:AnishShah,项目名称:tensorflow,代码行数:9,代码来源:ast_util_test.py


示例7: test_replace_tuple

  def test_replace_tuple(self):
    template = """
      def test_fn(a, c):
        return b,
    """

    node = templates.replace(template, b=('a', 'c'))[0]
    result, _ = compiler.ast_to_object(node)

    self.assertEquals((2, 3), result.test_fn(2, 3))
开发者ID:ziky90,项目名称:tensorflow,代码行数:10,代码来源:templates_test.py


示例8: test_replace_name_with_dict

  def test_replace_name_with_dict(self):
    template = """
      def test_fn():
        return foo['bar']
    """

    source = parser.parse_expression('{\'bar\': 3}')
    node = templates.replace(template, foo=source)[0]
    result, _ = compiler.ast_to_object(node)
    self.assertEquals(3, result.test_fn())
开发者ID:ziky90,项目名称:tensorflow,代码行数:10,代码来源:templates_test.py


示例9: test_replace_variable

  def test_replace_variable(self):
    template = """
      def test_fn(a):
        a += 1
        a = 2 * a + 1
        return b
    """

    node = templates.replace(template, a='b')[0]
    result, _ = compiler.ast_to_object(node)
    self.assertEquals(7, result.test_fn(2))
开发者ID:ziky90,项目名称:tensorflow,代码行数:11,代码来源:templates_test.py


示例10: test_replace_function_name

  def test_replace_function_name(self):
    template = """
      def fname(a):
        a += 1
        a = 2 * a + 1
        return a
    """

    node = templates.replace(template, fname='test_fn')[0]
    result, _ = compiler.ast_to_object(node)
    self.assertEquals(7, result.test_fn(2))
开发者ID:ziky90,项目名称:tensorflow,代码行数:11,代码来源:templates_test.py


示例11: _transform

 def _transform(self, f, strip_decorators):
   namespace = {
       'self_transform_decorator': self_transform_decorator,
       'simple_decorator': simple_decorator,
       'converter_testing': converter_testing,
   }
   node, ctx = self.prepare(
       f, namespace, recursive=False, strip_decorators=strip_decorators)
   node = decorators.transform(node, ctx)
   import_line = '\n'.join(ctx.program.additional_imports)
   result, _ = compiler.ast_to_object(node, source_prefix=import_line)
   return getattr(result, f.__name__)
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:12,代码来源:decorators_test.py


示例12: 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:AnishShah,项目名称:tensorflow,代码行数:14,代码来源:compiler_test.py


示例13: test_replace_attribute

  def test_replace_attribute(self):
    template = """
      def test_fn(a):
        return a.foo
    """

    node = templates.replace(template, foo='b')[0]
    result, _ = compiler.ast_to_object(node)
    mod = imp.new_module('test')
    mod.b = 3
    self.assertEquals(3, result.test_fn(mod))

    with self.assertRaises(ValueError):
      templates.replace(template, foo=1)
开发者ID:ziky90,项目名称:tensorflow,代码行数:14,代码来源:templates_test.py


示例14: test_replace_name_with_call

  def test_replace_name_with_call(self):
    template = """
      def test_fn():
        b = 5
        def g(a):
          return 3 * a
        def f():
          return g
        return foo
    """

    source = parser.parse_expression('f()(b)')
    node = templates.replace(template, foo=source)[0]
    result, _ = compiler.ast_to_object(node)
    self.assertEquals(15, result.test_fn())
开发者ID:ziky90,项目名称:tensorflow,代码行数:15,代码来源:templates_test.py


示例15: test_parser_compile_identity

  def test_parser_compile_identity(self):

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

    node, _ = parser.parse_entity(test_fn, future_features=())
    module, _, _ = compiler.ast_to_object(node)

    self.assertEqual(
        textwrap.dedent(tf_inspect.getsource(test_fn)),
        tf_inspect.getsource(module.test_fn))
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:15,代码来源:compiler_test.py


示例16: _convert_with_cache

def _convert_with_cache(entity, program_ctx, free_nonglobal_var_names):
  """Returns a (possibly cached) factory for the converted result of entity."""
  # The cache key is the entity's code object if it defined one, otherwise it's
  # the entity itself. Keying by the code object allows caching of functions
  # that are dynamically created e.g. in a loop.
  if hasattr(entity, '__code__'):
    key = entity.__code__
  else:
    key = entity

  # The cache subkey encompases any conversion options on which the generated
  # code may depend.
  # The cached factory includes the necessary definitions to distinguish
  # between the global and non-global free variables. For this reason, the
  # cache subkey includes the names of the free non-globals.
  subkey = (program_ctx.options, frozenset(free_nonglobal_var_names))

  # The cache values are _ConvertedEntityFactoryInfo objects.
  if _CACHE.has(key, subkey):
    # TODO(mdan): Check whether the module is still loaded.
    converted_entity_info = _CACHE[key][subkey]
    logging.log(3, 'Cache hit for entity %s key %s subkey %s: %s', entity, key,
                subkey, converted_entity_info)
    return converted_entity_info

  logging.log(1, 'Entity %s is not cached for key %s subkey %s', entity, key,
              subkey)

  nodes, converted_name, entity_info = convert_entity_to_ast(
      entity, program_ctx)

  namer = naming.Namer(entity_info.namespace)
  factory_factory_name = namer.new_symbol('create_converted_entity_factory', ())
  factory_name = namer.new_symbol('create_converted_entity', ())
  nodes = _wrap_into_dynamic_factory(
      nodes, converted_name, factory_factory_name, factory_name,
      free_nonglobal_var_names, entity_info.future_features)

  module, _, source_map = compiler.ast_to_object(nodes, include_source_map=True)
  module_name = module.__name__

  converted_entity_info = _ConvertedEntityFactoryInfo(
      module_name=module_name,
      converted_name=converted_name,
      factory_factory_name=factory_factory_name,
      source_map=source_map)
  _CACHE[key][subkey] = converted_entity_info
  return converted_entity_info
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:48,代码来源:conversion.py


示例17: test_replace_code_block

  def test_replace_code_block(self):
    template = """
      def test_fn(a):
        block
        return a
    """

    node = templates.replace(
        template,
        block=[
            gast.Assign([
                gast.Name('a', None, None)
            ], gast.BinOp(gast.Name('a', None, None), gast.Add(), gast.Num(1))),
        ] * 2)[0]
    result, _ = compiler.ast_to_object(node)
    self.assertEquals(3, result.test_fn(1))
开发者ID:ziky90,项目名称:tensorflow,代码行数:16,代码来源:templates_test.py


示例18: test_replace_call_keyword

  def test_replace_call_keyword(self):
    template = """
      def test_fn():
        def f(a, d, f):
          return a + d + f
        return f(1, kws=None)
    """

    source = parser.parse_expression('f(d=3, f=5)')
    node = templates.replace(template, kws=source.keywords)[0]
    result, _ = compiler.ast_to_object(node)
    self.assertEquals(9, result.test_fn())

    with self.assertRaises(ValueError):
      templates.replace(template, kws=[])
      templates.replace(template, kws=1)
开发者ID:ziky90,项目名称:tensorflow,代码行数:16,代码来源:templates_test.py


示例19: test_to_ast

  def test_to_ast(self):
    opts = converter.ConversionOptions()
    opts_ast = opts.to_ast()

    template = '''
    def test_fn():
      return opts_ast
    '''
    opts_packed = templates.replace(template, opts_ast=opts_ast)

    reparsed, _, _ = compiler.ast_to_object(opts_packed)
    reparsed.__dict__['ag__'] = self.make_fake_mod(
        'fake_ag', converter.ConversionOptions, converter.Feature)

    reparsed_opts = reparsed.test_fn()

    self.assertEqual(opts.recursive, reparsed_opts.recursive)
    self.assertEqual(opts.force_conversion, reparsed_opts.force_conversion)
    self.assertEqual(
        opts.internal_convert_user_code,
        reparsed_opts.internal_convert_user_code)
    self.assertEqual(opts.optional_features, reparsed_opts.optional_features)
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:22,代码来源:converter_test.py


示例20: test_to_ast

  def test_to_ast(self):
    opts = converter.ConversionOptions()

    namer = converter_testing.FakeNamer()
    program_ctx = converter.ProgramContext(
        options=opts,
        partial_types=None,
        autograph_module=None,
        uncompiled_modules=())
    entity_info = transformer.EntityInfo(
        source_code='',
        source_file='<fragment>',
        namespace={},
        arg_values=None,
        arg_types={},
        owner_type=None)
    ctx = converter.EntityContext(namer, entity_info, program_ctx)
    opts_ast = opts.to_ast(ctx)

    template = '''
    def test_fn():
      return opts_ast
    '''
    opts_packed = templates.replace(template, opts_ast=opts_ast)

    reparsed, _ = compiler.ast_to_object(opts_packed)
    reparsed.__dict__['ag__'] = self.make_fake_mod(
        'fake_ag', converter.ConversionOptions, converter.Feature)

    reparsed_opts = reparsed.test_fn()

    self.assertEqual(opts.recursive, reparsed_opts.recursive)
    self.assertEqual(opts.verbose, reparsed_opts.verbose)
    self.assertEqual(opts.force_conversion, reparsed_opts.force_conversion)
    self.assertEqual(
        opts.internal_convert_user_code,
        reparsed_opts.internal_convert_user_code)
    self.assertEqual(opts.optional_features, reparsed_opts.optional_features)
开发者ID:kylin9872,项目名称:tensorflow,代码行数:38,代码来源:converter_test.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python compiler.ast_to_source函数代码示例发布时间:2022-05-27
下一篇:
Python anno.setanno函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap