design a Condition class

joh12005 at yahoo.fr joh12005 at yahoo.fr
Wed May 10 17:40:17 EDT 2006


Hello,

i posted for suggestions a little idea even if it still needs further
thoughts but as i'm sure you could help :)

if would like to implement some kind of Condition class which i coud
use to build bricks of more complex condition, conditions are based on
fields by using regexp

class Condition:
	def __init__(self, field0, field1, field2):
		self.field0 = field0
		self.field1 = field1
		self.field2 = field2
	def match(self, against):
		w, t, l = against
		f0 = False
		if self.field0 is None:
			f0 = True
		else:
			f0 = self.field0.search(w)
		if self.field1 is None:
			f1 = True
		else:
			f1 = self.field1.search(t)
		if self.field2 is None:
			f2 = True
		else:
			f2 = self.field2.search(l)
		return f0 and f1 and f2

c0 = Condition(re.compile("something"), None,
re.compile("somethingelse"))
c1 = Condition(re.compile("another"), None, None)

i can use

if c0.search(myitem)

but i would like to be able to have composition such as :

c2 = c0 | c1

which should be understood as defining a new c2 which is able to match
(c0 or c1) from c0 and c1 already defined.

actually i can imagine a

def OR(c0, c1):
     # here => missing None support 'a or None' is 'a'
     return Condition(re.compile("|".join((c0.field0.pattern,
c1.field0.pattern)),
              re.compile("|".join((c0.field1.pattern,
c1.field1.pattern)),
              re.compile("|".join((c0.field2.pattern,
c1.field2.pattern))

the idea is to build c2 = Condition(re.compile("something|another"),
None, re.compile("somethingelse"))
c2 = OR(c0, c1)

but maybe have you clever suggestions ?

best regards.




More information about the Python-list mailing list