Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/autograph/converters/list_comprehensions.py: 28%

25 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2016 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================== 

15"""Lowers list comprehensions into for and if statements. 

16 

17Example: 

18 

19 result = [x * x for x in xs] 

20 

21becomes 

22 

23 result = [] 

24 for x in xs: 

25 elt = x * x 

26 result.append(elt) 

27""" 

28 

29import gast 

30 

31from tensorflow.python.autograph.core import converter 

32from tensorflow.python.autograph.pyct import templates 

33 

34 

35# TODO(mdan): This should covert directly to operator calls. 

36 

37 

38class ListCompTransformer(converter.Base): 

39 """Lowers list comprehensions into standard control flow.""" 

40 

41 def visit_Assign(self, node): 

42 if not isinstance(node.value, gast.ListComp): 

43 return self.generic_visit(node) 

44 if len(node.targets) > 1: 

45 raise NotImplementedError('multiple assignments') 

46 

47 target, = node.targets 

48 list_comp_node = node.value 

49 

50 template = """ 

51 target = [] 

52 """ 

53 initialization = templates.replace(template, target=target) 

54 

55 template = """ 

56 target.append(elt) 

57 """ 

58 body = templates.replace(template, target=target, elt=list_comp_node.elt) 

59 

60 for gen in reversed(list_comp_node.generators): 

61 for gen_if in reversed(gen.ifs): 

62 template = """ 

63 if test: 

64 body 

65 """ 

66 body = templates.replace(template, test=gen_if, body=body) 

67 template = """ 

68 for target in iter_: 

69 body 

70 """ 

71 body = templates.replace( 

72 template, iter_=gen.iter, target=gen.target, body=body) 

73 

74 return initialization + body 

75 

76 

77def transform(node, ctx): 

78 return ListCompTransformer(ctx).visit(node)