Coverage for /usr/lib/python3/dist-packages/sympy/printing/tree.py: 11%

38 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1def pprint_nodes(subtrees): 

2 """ 

3 Prettyprints systems of nodes. 

4 

5 Examples 

6 ======== 

7 

8 >>> from sympy.printing.tree import pprint_nodes 

9 >>> print(pprint_nodes(["a", "b1\\nb2", "c"])) 

10 +-a 

11 +-b1 

12 | b2 

13 +-c 

14 

15 """ 

16 def indent(s, type=1): 

17 x = s.split("\n") 

18 r = "+-%s\n" % x[0] 

19 for a in x[1:]: 

20 if a == "": 

21 continue 

22 if type == 1: 

23 r += "| %s\n" % a 

24 else: 

25 r += " %s\n" % a 

26 return r 

27 if not subtrees: 

28 return "" 

29 f = "" 

30 for a in subtrees[:-1]: 

31 f += indent(a) 

32 f += indent(subtrees[-1], 2) 

33 return f 

34 

35 

36def print_node(node, assumptions=True): 

37 """ 

38 Returns information about the "node". 

39 

40 This includes class name, string representation and assumptions. 

41 

42 Parameters 

43 ========== 

44 

45 assumptions : bool, optional 

46 See the ``assumptions`` keyword in ``tree`` 

47 """ 

48 s = "%s: %s\n" % (node.__class__.__name__, str(node)) 

49 

50 if assumptions: 

51 d = node._assumptions 

52 else: 

53 d = None 

54 

55 if d: 

56 for a in sorted(d): 

57 v = d[a] 

58 if v is None: 

59 continue 

60 s += "%s: %s\n" % (a, v) 

61 

62 return s 

63 

64 

65def tree(node, assumptions=True): 

66 """ 

67 Returns a tree representation of "node" as a string. 

68 

69 It uses print_node() together with pprint_nodes() on node.args recursively. 

70 

71 Parameters 

72 ========== 

73 

74 asssumptions : bool, optional 

75 The flag to decide whether to print out all the assumption data 

76 (such as ``is_integer`, ``is_real``) associated with the 

77 expression or not. 

78 

79 Enabling the flag makes the result verbose, and the printed 

80 result may not be determinisitic because of the randomness used 

81 in backtracing the assumptions. 

82 

83 See Also 

84 ======== 

85 

86 print_tree 

87 

88 """ 

89 subtrees = [] 

90 for arg in node.args: 

91 subtrees.append(tree(arg, assumptions=assumptions)) 

92 s = print_node(node, assumptions=assumptions) + pprint_nodes(subtrees) 

93 return s 

94 

95 

96def print_tree(node, assumptions=True): 

97 """ 

98 Prints a tree representation of "node". 

99 

100 Parameters 

101 ========== 

102 

103 asssumptions : bool, optional 

104 The flag to decide whether to print out all the assumption data 

105 (such as ``is_integer`, ``is_real``) associated with the 

106 expression or not. 

107 

108 Enabling the flag makes the result verbose, and the printed 

109 result may not be determinisitic because of the randomness used 

110 in backtracing the assumptions. 

111 

112 Examples 

113 ======== 

114 

115 >>> from sympy.printing import print_tree 

116 >>> from sympy import Symbol 

117 >>> x = Symbol('x', odd=True) 

118 >>> y = Symbol('y', even=True) 

119 

120 Printing with full assumptions information: 

121 

122 >>> print_tree(y**x) 

123 Pow: y**x 

124 +-Symbol: y 

125 | algebraic: True 

126 | commutative: True 

127 | complex: True 

128 | even: True 

129 | extended_real: True 

130 | finite: True 

131 | hermitian: True 

132 | imaginary: False 

133 | infinite: False 

134 | integer: True 

135 | irrational: False 

136 | noninteger: False 

137 | odd: False 

138 | rational: True 

139 | real: True 

140 | transcendental: False 

141 +-Symbol: x 

142 algebraic: True 

143 commutative: True 

144 complex: True 

145 even: False 

146 extended_nonzero: True 

147 extended_real: True 

148 finite: True 

149 hermitian: True 

150 imaginary: False 

151 infinite: False 

152 integer: True 

153 irrational: False 

154 noninteger: False 

155 nonzero: True 

156 odd: True 

157 rational: True 

158 real: True 

159 transcendental: False 

160 zero: False 

161 

162 Hiding the assumptions: 

163 

164 >>> print_tree(y**x, assumptions=False) 

165 Pow: y**x 

166 +-Symbol: y 

167 +-Symbol: x 

168 

169 See Also 

170 ======== 

171 

172 tree 

173 

174 """ 

175 print(tree(node, assumptions=assumptions))