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


Python templates.replace函数代码示例

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


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

示例1: _create_cond_expr

 def _create_cond_expr(self, results, test, body_name, orelse_name,
                       state_getter_name,
                       state_setter_name):
   if results is not None:
     template = """
       results = ag__.if_stmt(test, body_name, orelse_name,
                              state_getter_name, state_setter_name)
     """
     return templates.replace(
         template,
         test=test,
         results=results,
         body_name=body_name,
         orelse_name=orelse_name,
         state_getter_name=state_getter_name,
         state_setter_name=state_setter_name)
   else:
     template = """
       ag__.if_stmt(test, body_name, orelse_name, getter_name, setter_name)
     """
     return templates.replace(
         template,
         test=test,
         body_name=body_name,
         orelse_name=orelse_name,
         getter_name=state_getter_name,
         setter_name=state_setter_name)
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:27,代码来源:control_flow.py

示例2: _create_state_functions

  def _create_state_functions(self, composites,
                              state_getter_name, state_setter_name):
    if composites:
      composite_tuple = tuple(composites)
      template = """
        def state_getter_name():
          return composite_tuple,
        def state_setter_name(vals):
          composite_tuple, = vals
      """
      node = templates.replace(
          template,
          state_getter_name=state_getter_name,
          state_setter_name=state_setter_name,
          composite_tuple=composite_tuple)
    else:
      template = """
        def state_getter_name():
          return ()
        def state_setter_name(_):
          pass
        """
      node = templates.replace(
          template,
          state_getter_name=state_getter_name,
          state_setter_name=state_setter_name)

    return node
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:28,代码来源:control_flow.py

示例3: _create_cond_branch

  def _create_cond_branch(self, body_name, aliased_orig_names,
                          aliased_new_names, body, returns):
    if len(returns) == 1:
      template = """
        return retval
      """
      return_stmt = templates.replace(template, retval=returns[0])
    else:
      template = """
        return (retvals,)
      """
      return_stmt = templates.replace(template, retvals=returns)

    if aliased_orig_names:
      template = """
        def body_name():
          aliased_new_names, = aliased_orig_names,
          body
          return_stmt
      """
      return templates.replace(
          template,
          body_name=body_name,
          body=body,
          aliased_orig_names=aliased_orig_names,
          aliased_new_names=aliased_new_names,
          return_stmt=return_stmt)
    else:
      template = """
        def body_name():
          body
          return_stmt
      """
      return templates.replace(
          template, body_name=body_name, body=body, return_stmt=return_stmt)
开发者ID:ThunderQi,项目名称:tensorflow,代码行数:35,代码来源:control_flow.py

示例4: visit_For

  def visit_For(self, node):
    self.generic_visit(node)

    loop_state, reserved_symbols = self._get_loop_state(node)
    loop_state, state_ssf, state_ast_tuple, ssf_map = self._state_constructs(
        loop_state, reserved_symbols)
    node_body = ast_util.rename_symbols(node.body, ssf_map)
    if anno.hasanno(node, 'extra_test'):
      extra_test = anno.getanno(node, 'extra_test')
      extra_test = ast_util.rename_symbols(extra_test, ssf_map)
    else:
      extra_test = parser.parse_expression('True')

    if loop_state:
      template = """
        def extra_test_name(state_ssf):
          return extra_test_expr
        def body_name(loop_vars, state_ssf):
          # Workaround for PEP-3113
          iterate = loop_vars
          body
          return state_ssf,
        state_ast_tuple = ag__.for_stmt(
            iter_, extra_test_name, body_name, (state,))
      """
      node = templates.replace(
          template,
          state=loop_state,
          state_ssf=state_ssf,
          state_ast_tuple=state_ast_tuple,
          iter_=node.iter,
          iterate=node.target,
          extra_test_name=self.ctx.namer.new_symbol('extra_test',
                                                    reserved_symbols),
          extra_test_expr=extra_test,
          body_name=self.ctx.namer.new_symbol('loop_body', reserved_symbols),
          body=node_body)
    else:
      template = """
        def extra_test_name():
          return extra_test_expr
        def body_name(loop_vars):
          # Workaround for PEP-3113
          iterate = loop_vars
          body
          return ()
        ag__.for_stmt(iter_, extra_test_name, body_name, ())
      """
      node = templates.replace(
          template,
          iter_=node.iter,
          iterate=node.target,
          extra_test_name=self.ctx.namer.new_symbol('extra_test',
                                                    reserved_symbols),
          extra_test_expr=extra_test,
          body_name=self.ctx.namer.new_symbol('loop_body', reserved_symbols),
          body=node_body)

    return node
开发者ID:terrytangyuan,项目名称:tensorflow,代码行数:59,代码来源:control_flow.py

示例5: test_replace_name_mixed_attr_subscript

  def test_replace_name_mixed_attr_subscript(self, expression_source):
    template = 'foo = bar'
    replacement = _parse_with_unset_ctx(expression_source)

    target_node = templates.replace(template, foo=replacement)[0].targets[0]
    self.assertExpectedCtxSet(target_node, gast.Store)

    value_node = templates.replace(template, bar=replacement)[0].value
    self.assertExpectedCtxSet(value_node, gast.Load)
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:9,代码来源:templates_test.py

示例6: visit_With

  def visit_With(self, node):
    # Depth-first traversal of syntax
    node = self.generic_visit(node)

    # If the with statement returns, lift the return
    if isinstance(node.body[-1], gast.Return):
      node.body[-1] = templates.replace(
          'a = b', a=self.common_return_name, b=node.body[-1].value)[0]
      return_node = templates.replace('return a', a=self.common_return_name)[0]
      node = self.generic_visit(node)
      self.changes_made = True
      return [node, return_node]
    else:
      return node
开发者ID:AnishShah,项目名称:tensorflow,代码行数:14,代码来源:return_statements.py

示例7: 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

示例8: visit_Assert

  def visit_Assert(self, node):
    self.generic_visit(node)

    # Note: The lone tf.Assert call will be wrapped with control_dependencies
    # by side_effect_guards.
    template = """
      tf.Assert(test, (msg,))
    """

    if node.msg is None:
      return templates.replace(
          template, test=node.test, msg=gast.Str('Assertion error'))
    elif isinstance(node.msg, gast.Str):
      return templates.replace(template, test=node.test, msg=node.msg)
    else:
      raise NotImplementedError('can only convert string messages for now.')
开发者ID:AnishShah,项目名称:tensorflow,代码行数:16,代码来源:asserts.py

示例9: _insert_dynamic_conversion

 def _insert_dynamic_conversion(self, node):
   """Inlines a dynamic conversion for a dynamic function."""
   # TODO(mdan): Pass information on the statically compiled functions.
   # Having access to the statically compiled functions can help avoid
   # unnecessary compilation.
   # For example, this would lead to function `a` being compiled twice:
   #
   #   def a():
   #     v = b
   #     b()
   #   def b():
   #     a()
   #
   # This is really a problem with recursive calls, which currently can
   # only be gated by a static condition, and should be rare.
   # TODO(mdan): It probably makes sense to use dynamic conversion every time.
   # Before we could convert all the time though, we'd need a reasonable
   # caching mechanism.
   template = """
     ag__.converted_call(func, options, args)
   """
   call_expr = templates.replace(
       template,
       func=node.func,
       options=self.ctx.program.options.to_ast(self.ctx.info.namespace),
       args=node.args)
   new_call = call_expr[0].value
   # TODO(mdan): Improve the template mechanism to better support this.
   new_call.keywords = node.keywords
   return new_call
开发者ID:ThunderQi,项目名称:tensorflow,代码行数:30,代码来源:call_trees.py

示例10: _for_loop_with_extra_test

 def _for_loop_with_extra_test(self, loop_state, state_ssf, state_ast_tuple,
                               original_node, extra_test_name, extra_test,
                               body_name, loop_body, ssf_map):
   target_nodes = ast_util.rename_symbols(original_node.target, ssf_map)
   template = """
     def extra_test_name(state_ssf):
       return extra_test_expr
     def body_name(loop_vars, state_ssf):
       # Workaround for PEP-3113
       target = loop_vars
       body
       return state_ssf,
     state_ast_tuple = ag__.for_stmt(
         iter_, extra_test_name, body_name, (state,))
   """
   return templates.replace(
       template,
       state=loop_state,
       state_ssf=state_ssf,
       state_ast_tuple=state_ast_tuple,
       iter_=original_node.iter,
       target=target_nodes,
       extra_test_name=extra_test_name,
       extra_test_expr=extra_test,
       body_name=body_name,
       body=loop_body)
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:26,代码来源:control_flow.py

示例11: visit_Continue

 def visit_Continue(self, node):
   self.set_local(CONTINUE_USED, True)
   template = """
     var_name = tf.constant(True)
   """
   return templates.replace(
       template, var_name=self.get_local(CONTROL_VAR_NAME))
开发者ID:AnishShah,项目名称:tensorflow,代码行数:7,代码来源:continue_statements.py

示例12: visit_Continue

 def visit_Continue(self, node):
   self.state[_Continue].used = True
   template = """
     var_name = True
   """
   return templates.replace(
       template, var_name=self.state[_Continue].control_var_name)
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:7,代码来源:continue_statements.py

示例13: visit_While

  def visit_While(self, node):
    scope = anno.getanno(node, NodeAnno.BODY_SCOPE)
    break_var = self.ctx.namer.new_symbol('break_', scope.referenced)

    node.test = self.visit(node.test)
    node.body, break_used = self._process_body(node.body, break_var)
    # A break in the else clause applies to the containing scope.
    node.orelse = self.visit_block(node.orelse)

    if break_used:
      # Python's else clause only triggers if the loop exited cleanly (e.g.
      # break did not trigger).
      guarded_orelse = self._guard_if_present(node.orelse, break_var)

      template = """
        var_name = tf.constant(False)
        while test and not var_name:
          body
        else:
          orelse
      """
      node = templates.replace(
          template,
          var_name=break_var,
          test=node.test,
          body=node.body,
          orelse=guarded_orelse)

    return node
开发者ID:AnishShah,项目名称:tensorflow,代码行数:29,代码来源:break_statements.py

示例14: 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

示例15: to_ast

  def to_ast(self, ctx, internal_convert_user_code=None):
    """Returns a representation of this object as an AST node.

    The AST node encodes a constructor that would create an object with the
    same contents.

    Args:
      ctx: EntityContext, the entity with which this AST needs to be consistent.
      internal_convert_user_code: Optional[bool], allows ovrriding the
        corresponding value.

    Returns:
      ast.Node
    """
    template = """
      ag__.ConversionOptions(
          recursive=recursive_val,
          verbose=verbose_val,
          strip_decorators=strip_decorators_val,
          force_conversion=force_conversion_val,
          optional_features=optional_features_val,
          internal_convert_user_code=internal_convert_user_code_val)
    """

    def as_qualified_name(o):
      name = inspect_utils.getqualifiedname(ctx.info.namespace, o, max_depth=1)
      if not name:
        if isinstance(o, weakref.ref):
          # `o` might already be a weak reference, if this object was
          # constructed from code generated by `to_ast` itself.
          # If so, unpack it.
          o = o()
        # TODO(mdan): This needs to account for the symbols defined locally.
        name = ctx.namer.new_symbol(o.__name__, ())
        ctx.program.add_symbol(name, weakref.ref(o))
      return name

    def list_of_names(values):
      return parser.parse_expression('({})'.format(', '.join(
          tuple(as_qualified_name(v) for v in values))))

    def list_of_features(values):
      return parser.parse_expression('({})'.format(', '.join(
          'ag__.{}'.format(str(v)) for v in values)))

    if internal_convert_user_code is None:
      internal_convert_user_code = self.internal_convert_user_code

    expr_ast = templates.replace(
        template,
        recursive_val=parser.parse_expression(str(self.recursive)),
        verbose_val=parser.parse_expression(str(int(self.verbose))),
        strip_decorators_val=list_of_names(self._strip_decorators),
        force_conversion_val=parser.parse_expression(
            str(self.force_conversion)),
        internal_convert_user_code_val=parser.parse_expression(
            str(internal_convert_user_code)),
        optional_features_val=list_of_features(self.optional_features))
    return expr_ast[0].value
开发者ID:kylin9872,项目名称:tensorflow,代码行数:59,代码来源:converter.py


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