MCPcopy
hub / github.com/google-deepmind/mathematics_dataset / bracketed

Function bracketed

mathematics_dataset/sample/ops.py:64–87  ·  view source on GitHub ↗

Returns string representation of `child`, possibly bracketed. Args: child: Instance of `Op` or a valid value for `ConstantOp`. parent: Instance of `Op`. Used to determine whether `child` needs to be bracketed first before appearing in the parent op's expression. bracket_if_sam

(child, parent, bracket_if_same_precedence)

Source from the content-addressed store, hash-verified

62
63
64def bracketed(child, parent, bracket_if_same_precedence):
65 """Returns string representation of `child`, possibly bracketed.
66
67 Args:
68 child: Instance of `Op` or a valid value for `ConstantOp`.
69 parent: Instance of `Op`. Used to determine whether `child` needs to be
70 bracketed first before appearing in the parent op's expression.
71 bracket_if_same_precedence: Whether to bracket if the child has the same
72 operator precedence as the parent.
73
74 Returns:
75 String representation of `child`.
76 """
77 if not isinstance(child, Op):
78 child = Constant(child)
79
80 child_precedence = child.precedence
81 parent_precedence = parent.precedence
82 if (parent_precedence > child_precedence
83 or (parent_precedence == child_precedence
84 and not bracket_if_same_precedence)):
85 return str(child)
86 else:
87 return '({})'.format(child)
88
89
90def _flatten(iterable):

Callers 5

__str__Method · 0.85
__str__Method · 0.85
__str__Method · 0.85
__str__Method · 0.85
__str__Method · 0.85

Calls 1

ConstantClass · 0.85

Tested by

no test coverage detected