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)
| 62 | |
| 63 | |
| 64 | def 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 | |
| 90 | def _flatten(iterable): |