Combine this tree and the data represented by data using the connector conn_type. The combine is done by squashing the node other away if possible. This tree (self) will never be pushed to a child node of the combined tree, nor will the connector or negated
(self, data, conn_type)
| 87 | ) |
| 88 | |
| 89 | def add(self, data, conn_type): |
| 90 | """ |
| 91 | Combine this tree and the data represented by data using the |
| 92 | connector conn_type. The combine is done by squashing the node other |
| 93 | away if possible. |
| 94 | |
| 95 | This tree (self) will never be pushed to a child node of the |
| 96 | combined tree, nor will the connector or negated properties change. |
| 97 | |
| 98 | Return a node which can be used in place of data regardless if the |
| 99 | node other got squashed or not. |
| 100 | """ |
| 101 | if self.connector != conn_type: |
| 102 | obj = self.copy() |
| 103 | self.connector = conn_type |
| 104 | self.children = [obj, data] |
| 105 | return data |
| 106 | elif ( |
| 107 | isinstance(data, Node) |
| 108 | and not data.negated |
| 109 | and (data.connector == conn_type or len(data) == 1) |
| 110 | ): |
| 111 | # We can squash the other node's children directly into this node. |
| 112 | # We are just doing (AB)(CD) == (ABCD) here, with the addition that |
| 113 | # if the length of the other node is 1 the connector doesn't |
| 114 | # matter. However, for the len(self) == 1 case we don't want to do |
| 115 | # the squashing, as it would alter self.connector. |
| 116 | self.children.extend(data.children) |
| 117 | return self |
| 118 | else: |
| 119 | # We could use perhaps additional logic here to see if some |
| 120 | # children could be used for pushdown here. |
| 121 | self.children.append(data) |
| 122 | return data |
| 123 | |
| 124 | def negate(self): |
| 125 | """Negate the sense of the root connector.""" |