Returns a list containing the top-level AND or OR operands in the expression 'expr', in the same (left-to-right) order as they appear in the expression. This can be handy e.g. for splitting (weak) reverse dependencies from 'select' and 'imply' into individual selects/implies.
(expr, op)
| 6170 | |
| 6171 | |
| 6172 | def split_expr(expr, op): |
| 6173 | """ |
| 6174 | Returns a list containing the top-level AND or OR operands in the |
| 6175 | expression 'expr', in the same (left-to-right) order as they appear in |
| 6176 | the expression. |
| 6177 | |
| 6178 | This can be handy e.g. for splitting (weak) reverse dependencies |
| 6179 | from 'select' and 'imply' into individual selects/implies. |
| 6180 | |
| 6181 | op: |
| 6182 | Either AND to get AND operands, or OR to get OR operands. |
| 6183 | |
| 6184 | (Having this as an operand might be more future-safe than having two |
| 6185 | hardcoded functions.) |
| 6186 | |
| 6187 | |
| 6188 | Pseudo-code examples: |
| 6189 | |
| 6190 | split_expr( A , OR ) -> [A] |
| 6191 | split_expr( A && B , OR ) -> [A && B] |
| 6192 | split_expr( A || B , OR ) -> [A, B] |
| 6193 | split_expr( A || B , AND ) -> [A || B] |
| 6194 | split_expr( A || B || (C && D) , OR ) -> [A, B, C && D] |
| 6195 | |
| 6196 | # Second || is not at the top level |
| 6197 | split_expr( A || (B && (C || D)) , OR ) -> [A, B && (C || D)] |
| 6198 | |
| 6199 | # Parentheses don't matter as long as we stay at the top level (don't |
| 6200 | # encounter any non-'op' nodes) |
| 6201 | split_expr( (A || B) || C , OR ) -> [A, B, C] |
| 6202 | split_expr( A || (B || C) , OR ) -> [A, B, C] |
| 6203 | """ |
| 6204 | res = [] |
| 6205 | |
| 6206 | def rec(subexpr): |
| 6207 | if subexpr.__class__ is tuple and subexpr[0] is op: |
| 6208 | rec(subexpr[1]) |
| 6209 | rec(subexpr[2]) |
| 6210 | else: |
| 6211 | res.append(subexpr) |
| 6212 | |
| 6213 | rec(expr) |
| 6214 | return res |
| 6215 | |
| 6216 | |
| 6217 | def escape(s): |
no test coverage detected