Mercurial > p > roundup > code
comparison roundup/mlink_expr.py @ 6960:7b090bb35c25
flake8 fixes - exception handling changed
flake was flagging the bare except: expression. From testing, it
looks like that exception handler only gets ValueError. So changed it
to
except ValueError:
and added an
except BaseExpression
that just re-raises the exception. But I think this better expresses
the expected flow.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 12 Sep 2022 22:20:43 -0400 |
| parents | a0c0ee3ed8b1 |
| children | 87af08c75695 |
comparison
equal
deleted
inserted
replaced
| 6959:3211745e8d7c | 6960:7b090bb35c25 |
|---|---|
| 1 # | 1 # |
| 2 # Copyright: 2010 Intevation GmbH. | 2 # Copyright: 2010 Intevation GmbH. |
| 3 # 2021 Ralf Schlatterbeck, rsc@runtux.com. | 3 # 2021 Ralf Schlatterbeck, rsc@runtux.com. |
| 4 # | 4 # |
| 5 | 5 |
| 6 # This module is Free Software under the Roundup licensing, | 6 # This module is Free Software under the Roundup licensing, |
| 7 # see the COPYING.txt file coming with Roundup. | 7 # see the COPYING.txt file coming with Roundup. |
| 8 # | 8 # |
| 9 | 9 |
| 15 | 15 |
| 16 def visit(self, visitor): | 16 def visit(self, visitor): |
| 17 self.x.visit(visitor) | 17 self.x.visit(visitor) |
| 18 self.y.visit(visitor) | 18 self.y.visit(visitor) |
| 19 | 19 |
| 20 | |
| 20 class Unary: | 21 class Unary: |
| 21 | 22 |
| 22 def __init__(self, x): | 23 def __init__(self, x): |
| 23 self.x = x | 24 self.x = x |
| 24 | 25 |
| 26 return atom(self) | 27 return atom(self) |
| 27 | 28 |
| 28 def visit(self, visitor): | 29 def visit(self, visitor): |
| 29 self.x.visit(visitor) | 30 self.x.visit(visitor) |
| 30 | 31 |
| 32 | |
| 31 class Equals(Unary): | 33 class Equals(Unary): |
| 32 | 34 |
| 33 def evaluate(self, v): | 35 def evaluate(self, v): |
| 34 return self.x in v | 36 return self.x in v |
| 35 | 37 |
| 36 def visit(self, visitor): | 38 def visit(self, visitor): |
| 37 visitor(self) | 39 visitor(self) |
| 40 | |
| 38 | 41 |
| 39 class Empty(Unary): | 42 class Empty(Unary): |
| 40 | 43 |
| 41 def evaluate(self, v): | 44 def evaluate(self, v): |
| 42 return not v | 45 return not v |
| 43 | 46 |
| 44 def visit(self, visitor): | 47 def visit(self, visitor): |
| 45 visitor(self) | 48 visitor(self) |
| 46 | 49 |
| 50 | |
| 47 class Not(Unary): | 51 class Not(Unary): |
| 48 | 52 |
| 49 def evaluate(self, v): | 53 def evaluate(self, v): |
| 50 return not self.x.evaluate(v) | 54 return not self.x.evaluate(v) |
| 51 | 55 |
| 52 def generate(self, atom): | 56 def generate(self, atom): |
| 53 return "NOT(%s)" % self.x.generate(atom) | 57 return "NOT(%s)" % self.x.generate(atom) |
| 58 | |
| 54 | 59 |
| 55 class Or(Binary): | 60 class Or(Binary): |
| 56 | 61 |
| 57 def evaluate(self, v): | 62 def evaluate(self, v): |
| 58 return self.x.evaluate(v) or self.y.evaluate(v) | 63 return self.x.evaluate(v) or self.y.evaluate(v) |
| 60 def generate(self, atom): | 65 def generate(self, atom): |
| 61 return "(%s)OR(%s)" % ( | 66 return "(%s)OR(%s)" % ( |
| 62 self.x.generate(atom), | 67 self.x.generate(atom), |
| 63 self.y.generate(atom)) | 68 self.y.generate(atom)) |
| 64 | 69 |
| 70 | |
| 65 class And(Binary): | 71 class And(Binary): |
| 66 | 72 |
| 67 def evaluate(self, v): | 73 def evaluate(self, v): |
| 68 return self.x.evaluate(v) and self.y.evaluate(v) | 74 return self.x.evaluate(v) and self.y.evaluate(v) |
| 69 | 75 |
| 70 def generate(self, atom): | 76 def generate(self, atom): |
| 71 return "(%s)AND(%s)" % ( | 77 return "(%s)AND(%s)" % ( |
| 72 self.x.generate(atom), | 78 self.x.generate(atom), |
| 73 self.y.generate(atom)) | 79 self.y.generate(atom)) |
| 74 | 80 |
| 81 | |
| 75 def compile_expression(opcodes): | 82 def compile_expression(opcodes): |
| 76 | 83 |
| 77 stack = [] | 84 stack = [] |
| 78 push, pop = stack.append, stack.pop | 85 push, pop = stack.append, stack.pop |
| 79 for opcode in opcodes: | 86 for opcode in opcodes: |
| 80 if opcode == -1: push(Empty(opcode)) | 87 if opcode == -1: push(Empty(opcode)) # noqa: E271,E701 |
| 81 elif opcode == -2: push(Not(pop())) | 88 elif opcode == -2: push(Not(pop())) # noqa: E701 |
| 82 elif opcode == -3: push(And(pop(), pop())) | 89 elif opcode == -3: push(And(pop(), pop())) # noqa: E701 |
| 83 elif opcode == -4: push(Or(pop(), pop())) | 90 elif opcode == -4: push(Or(pop(), pop())) # noqa: E701 |
| 84 else: push(Equals(opcode)) | 91 else: push(Equals(opcode)) # noqa: E701 |
| 85 | 92 |
| 86 return pop() | 93 return pop() |
| 94 | |
| 87 | 95 |
| 88 class Expression: | 96 class Expression: |
| 89 | 97 |
| 90 def __init__(self, v, is_link=False): | 98 def __init__(self, v, is_link=False): |
| 91 try: | 99 try: |
| 97 if is_link: | 105 if is_link: |
| 98 self.evaluate = lambda x: compiled.evaluate( | 106 self.evaluate = lambda x: compiled.evaluate( |
| 99 x and [int(x)] or []) | 107 x and [int(x)] or []) |
| 100 else: | 108 else: |
| 101 self.evaluate = lambda x: compiled.evaluate([int(y) for y in x]) | 109 self.evaluate = lambda x: compiled.evaluate([int(y) for y in x]) |
| 102 except: | 110 except ValueError: |
| 103 if is_link: | 111 if is_link: |
| 104 v = [None if x == '-1' else x for x in v] | 112 v = [None if x == '-1' else x for x in v] |
| 105 self.evaluate = lambda x: x in v | 113 self.evaluate = lambda x: x in v |
| 106 elif '-1' in v: | 114 elif '-1' in v: |
| 107 v = [x for x in v if int(x) > 0] | 115 v = [x for x in v if int(x) > 0] |
| 108 self.evaluate = lambda x: bool(set(x) & set(v)) or not x | 116 self.evaluate = lambda x: bool(set(x) & set(v)) or not x |
| 109 else: | 117 else: |
| 110 self.evaluate = lambda x: bool(set(x) & set(v)) | 118 self.evaluate = lambda x: bool(set(x) & set(v)) |
| 119 except BaseException: | |
| 120 raise |
