11#
22# Copyright: 2010 Intevation GmbH.
33# 2021 Ralf Schlatterbeck, [email protected] . 4- #
4+ #
55
66# This module is Free Software under the Roundup licensing,
77# see the COPYING.txt file coming with Roundup.
@@ -17,6 +17,7 @@ def visit(self, visitor):
1717 self .x .visit (visitor )
1818 self .y .visit (visitor )
1919
20+
2021class Unary :
2122
2223 def __init__ (self , x ):
@@ -28,6 +29,7 @@ def generate(self, atom):
2829 def visit (self , visitor ):
2930 self .x .visit (visitor )
3031
32+
3133class Equals (Unary ):
3234
3335 def evaluate (self , v ):
@@ -36,6 +38,7 @@ def evaluate(self, v):
3638 def visit (self , visitor ):
3739 visitor (self )
3840
41+
3942class Empty (Unary ):
4043
4144 def evaluate (self , v ):
@@ -44,6 +47,7 @@ def evaluate(self, v):
4447 def visit (self , visitor ):
4548 visitor (self )
4649
50+
4751class Not (Unary ):
4852
4953 def evaluate (self , v ):
@@ -52,6 +56,7 @@ def evaluate(self, v):
5256 def generate (self , atom ):
5357 return "NOT(%s)" % self .x .generate (atom )
5458
59+
5560class Or (Binary ):
5661
5762 def evaluate (self , v ):
@@ -62,6 +67,7 @@ def generate(self, atom):
6267 self .x .generate (atom ),
6368 self .y .generate (atom ))
6469
70+
6571class And (Binary ):
6672
6773 def evaluate (self , v ):
@@ -72,19 +78,21 @@ def generate(self, atom):
7278 self .x .generate (atom ),
7379 self .y .generate (atom ))
7480
81+
7582def compile_expression (opcodes ):
7683
7784 stack = []
7885 push , pop = stack .append , stack .pop
7986 for opcode in opcodes :
80- if opcode == - 1 : push (Empty (opcode ))
81- elif opcode == - 2 : push (Not (pop ()))
82- elif opcode == - 3 : push (And (pop (), pop ()))
83- elif opcode == - 4 : push (Or (pop (), pop ()))
84- else : push (Equals (opcode ))
87+ if opcode == - 1 : push (Empty (opcode )) # noqa: E271,E701
88+ elif opcode == - 2 : push (Not (pop ())) # noqa: E701
89+ elif opcode == - 3 : push (And (pop (), pop ())) # noqa: E701
90+ elif opcode == - 4 : push (Or (pop (), pop ())) # noqa: E701
91+ else : push (Equals (opcode )) # noqa: E701
8592
8693 return pop ()
8794
95+
8896class Expression :
8997
9098 def __init__ (self , v , is_link = False ):
@@ -99,7 +107,7 @@ def __init__(self, v, is_link=False):
99107 x and [int (x )] or [])
100108 else :
101109 self .evaluate = lambda x : compiled .evaluate ([int (y ) for y in x ])
102- except :
110+ except ValueError :
103111 if is_link :
104112 v = [None if x == '-1' else x for x in v ]
105113 self .evaluate = lambda x : x in v
@@ -108,3 +116,5 @@ def __init__(self, v, is_link=False):
108116 self .evaluate = lambda x : bool (set (x ) & set (v )) or not x
109117 else :
110118 self .evaluate = lambda x : bool (set (x ) & set (v ))
119+ except BaseException :
120+ raise
0 commit comments