def replace_as_expression_restrictions(self):
   template = """
     foo(a)
     bar(b)
   """
   with self.assertRaises(ValueError):
     templates.replace_as_expression(template)
   with self.assertRaises(ValueError):
     templates.replace('')
   with self.assertRaises(ValueError):
     templates.replace('a = b')
Exemple #2
0
 def replace_as_expression_restrictions(self):
     template = """
   foo(a)
   bar(b)
 """
     with self.assertRaises(ValueError):
         templates.replace_as_expression(template)
     with self.assertRaises(ValueError):
         templates.replace('')
     with self.assertRaises(ValueError):
         templates.replace('a = b')
Exemple #3
0
 def visit_IfExp(self, node):
   template = """
       py2tf_utils.run_cond(test, lambda: body, lambda: orelse)
   """
   desugared_ifexp = templates.replace_as_expression(
       template, test=node.test, body=node.body, orelse=node.orelse)
   return desugared_ifexp
 def _as_function(self, func_name, args):
   template = """
     func_name(args)
   """
   replacement = templates.replace_as_expression(
       template, func_name=parser.parse_expression(func_name), args=args)
   anno.setanno(replacement, SAFE_BOOLEAN_OPERAND, True)
   return replacement
Exemple #5
0
 def _wrap_to_py_func_single_return(self, node, fqn):
     # TODO(mdan): Properly handle varargs, kwargs, etc.
     template = """
   py2tf_utils.wrap_py_func(func, dtype, (original_args,), False)
 """
     dtype = KNOWN_NUMPY_FUNCTIONS[fqn].dtype
     return templates.replace_as_expression(
         template,
         func=node.func,
         dtype=parser.parse_expression(dtype),
         original_args=node.args)
 def _wrap_to_py_func_single_return(self, node, fqn):
   # TODO(mdan): Properly handle varargs, kwargs, etc.
   template = """
     py2tf_utils.wrap_py_func(func, dtype, (original_args,), False)
   """
   dtype = KNOWN_NUMPY_FUNCTIONS[fqn].dtype
   return templates.replace_as_expression(
       template,
       func=node.func,
       dtype=parser.parse_expression(dtype),
       original_args=node.args)
Exemple #7
0
 def _wrap_to_py_func_single_return(self, node, dtype):
   # TODO(mdan): Properly handle varargs, etc.
   template = """
     py2tf_utils.wrap_py_func(func, dtype, (args,), kwargs, False)
   """
   return templates.replace_as_expression(
       template,
       func=node.func,
       dtype=parser.parse_expression(dtype),
       args=node.args,
       kwargs=ast_util.keywords_to_dict(node.keywords))
 def _inline_tf_op(self, op_name, args):
   if 'py2tf_utils' in op_name:
     # TODO(alexbw): explicitly spelling out the attribute function name
     # until fix for issue highlighted in cl/188931581 lands.
     template = """
     py2tf_utils.op_name(args)
   """
     op_name = op_name.replace('py2tf_utils.', '')
   else:
     template = """
       tf.op_name(args)
     """
   replacement = templates.replace_as_expression(
       template, op_name=op_name, args=args)
   anno.setanno(replacement, SAFE_BOOLEAN_OPERAND, True)
   return replacement
Exemple #9
0
  def _empty_list(self, node):
    if not anno.hasanno(node, 'element_type'):
      raise NotImplementedError(
          'type inference for empty lists is not yet supported; '
          'use utils.set_element_type(<list>, <dtype>) to continue')
    dtype = anno.getanno(node, 'element_type')
    if not isinstance(dtype, dtypes.DType):
      # TODO(mdan): Allow non-TF dtypes?
      # That would be consistent with the dynamic dispatch pattern, but
      # we must make sure that doesn't become confusing.
      raise NotImplementedError('element type "%s" not yet supported' % dtype)

    dtype_name = dtype.name
    # TODO(mdan): Does it ever make sense not to use tensor lists?
    template = """
      tf.TensorArray(tf.dtype_name, size=0, dynamic_size=True)
    """
    return templates.replace_as_expression(template, dtype_name=dtype_name)
Exemple #10
0
    def _empty_list(self, node):
        if not anno.hasanno(node, 'element_type'):
            raise NotImplementedError(
                'type inference for empty lists is not yet supported; '
                'use utils.set_element_type(<list>, <dtype>) to continue')
        dtype = anno.getanno(node, 'element_type')
        if not isinstance(dtype, dtypes.DType):
            # TODO(mdan): Allow non-TF dtypes?
            # That would be consistent with the dynamic dispatch pattern, but
            # we must make sure that doesn't become confusing.
            raise NotImplementedError('element type "%s" not yet supported' %
                                      dtype)

        dtype_name = dtype.name
        # TODO(mdan): Does it ever make sense not to use tensor lists?
        template = """
      tf.TensorArray(tf.dtype_name, size=0, dynamic_size=True)
    """
        return templates.replace_as_expression(template, dtype_name=dtype_name)