[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gnue/common/src GConditions.py
From: |
Jason Cater |
Subject: |
gnue/common/src GConditions.py |
Date: |
Sat, 28 Sep 2002 01:47:45 -0400 |
CVSROOT: /home/cvs
Module name: gnue
Changes by: Jason Cater <address@hidden> 02/09/28 01:47:43
Modified files:
common/src : GConditions.py
Log message:
cleaned up GConditions file; added LOGIC construct to
buildConditionFromDict()
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/common/src/GConditions.py.diff?cvsroot=OldCVS&tr1=1.17&tr2=1.18&r1=text&r2=text
Patches:
Index: gnue/common/src/GConditions.py
diff -c gnue/common/src/GConditions.py:1.17 gnue/common/src/GConditions.py:1.18
*** gnue/common/src/GConditions.py:1.17 Sun Jun 30 20:30:50 2002
--- gnue/common/src/GConditions.py Sat Sep 28 01:47:43 2002
***************
*** 39,201 ****
class ConditionNotSupported (ConditionError):
pass
- #
- # Build a condition tree using a dict
- # as a source. Assumes keys are field
- # names and values are constants.
- #
- def buildConditionFromDict (dict, comparison=None):
- cond = GCondition()
- lastParent = cond
-
- if len(dict.keys()):
- lastParent = GCand(lastParent)
-
- for key in dict.keys():
- eq = (comparison or GCeq)(lastParent)
- GCField(eq, key)
- GCConst(eq, dict[key])
-
- return cond
-
- #
- # Combine two conditions with an and clause.
- # NOTE: This modifies cond1 (and also returns it)
- #
- def combineConditions (cond1, cond2):
- if cond1 == None or cond1 == {}:
- return cond2
- elif cond2 == None or cond2 == {}:
- return cond1
-
- if type(cond1) == type({}):
- cond1 = buildConditionFromDict(cond1)
- if type(cond2) == type({}):
- cond1 = buildConditionFromDict(cond2)
-
- if not len(cond1._children):
- cond1._children = cond2._children
- return cond1
- elif len(cond2._children):
- children = cond1._children[:]
- cond1._children = []
- _and = GCand(cond1)
- _and._children = children
- if len(cond2._children) > 1:
- _and2 = GCand(cond1)
- _and2._children = cond2._children[:]
- else:
- cond1._children.append(cond2._children[0])
-
- return cond1
-
-
- # creates an GCondition Tree out of an list of tokens in a prefix
- # order.
-
- def buildTreeFromPrefix(term):
-
- # create a GCondition object as top object in the object stack
- parent={0:(GCondition())}
-
- # GCondition will have only one parameter
- # add paramcount=1 to the parameter count stack
- paramcount={0:1}
-
- # set start level for stack to zero
- level=0
- for i in term:
-
- # convert type into an object
- if conditionElements.has_key(i[0]):
- e=conditionElements[i[0]][2](parent[level])
- level=level+1
- # get parameter count
- paramcount[level]=conditionElements[i[0]][0]
- parent[level]=e
- elif i[0]=="field":
- e=GCField(parent[level], i[1])
- paramcount[level]=paramcount[level]-1
- if paramcount[level]==0:
- level=level-1
- elif i[0]=="const":
- e=GCConst(parent[level], i[1])
- paramcount.update({level:(paramcount[level]-1)})
- if paramcount[level]==0:
- level=level-1
- # print "NAME: %s VALUE: %s LEVEL: %s PCOUNT: %s" % \
- # (i[0],i[1],level,paramcount[level])
-
- return parent[0];
-
-
- def buildPrefixFromTree(conditionTree):
- if type(conditionTree) != types.InstanceType:
- raise ConditionError, "No valid condition tree"
- else:
- otype = string.lower(conditionTree._type[2:])
-
- #
- # care for objects without children
- #
- if otype == 'cfield':
- return [('field',"%s" % conditionTree.name)]
-
- elif otype == 'cconst':
- return [('const',conditionTree.value)]
-
- elif otype == 'param':
- return [('param', conditionTree.getValue())]
-
- #
- # if its an conditional object, then process it's children
- #
- elif conditionElements.has_key(otype):
- result=[]
-
- # first add operator to the list
- result.append((otype,'')); # ,None));
-
-
- # change operations with more than there minimal element no into
- # multiple operations with minimal elements
- # reason: to prevent a b c d AND OR being not well defined
- # because it can be a"a b c d AND AND OR" or "a b c d AND OR OR"
- paramcount=len(conditionTree._children)
- while (paramcount > \
- conditionElements[otype][0]):
- paramcount=paramcount-1
- result.append((otype,''));
-
-
- # then add children
- for i in range(0, len(conditionTree._children)):
- result = result + \
- buildPrefixFromTree(conditionTree._children[i])
-
- #
- # check for integrity of condition
- #
- if len(conditionTree._children) < conditionElements[otype][0]:
- raise GConditions.ConditionError, \
- _('Condition element "%s" expects at least %s arguments; found
%s') % \
- (otype, conditionElements[otype][0],
len(conditionTree._children))
-
- if len(conditionTree._children) > conditionElements[otype][1]:
- raise GConditions.ConditionError, \
- _('Condition element "%s" expects at most %s arguments; found
%s') % \
- (otype, conditionElements[otype][1],
len(conditionTree._children))
-
-
- # return combination
- return result;
-
- else:
- raise GConditions.ConditionNotSupported, \
- _('Condition clause "%s" is not supported '+
- 'by the condition to prefix table conversion.') % otype
-
-
class GCondition(GObj):
def __init__(self, parent=None, type="GCCondition"):
--- 39,44 ----
***************
*** 431,433 ****
--- 274,442 ----
'notlike': (2, 2, GCnotlike ),
'between': (3, 3, GCbetween )
}
+
+
+ #############################################################################
+ #############################################################################
+ #### Convenience Methods ##
+ #############################################################################
+ #############################################################################
+
+
+ #
+ # Build a condition tree using a dict
+ # as a source. Assumes keys are field
+ # names and values are constants.
+ #
+ def buildConditionFromDict (dict, comparison=GCeq, logic=GCand):
+ cond = GCondition()
+ lastParent = cond
+
+ if len(dict.keys()):
+ lastParent = logic(lastParent)
+
+ for key in dict.keys():
+ eq = comparison(lastParent)
+ GCField(eq, key)
+ GCConst(eq, dict[key])
+
+ return cond
+
+ #
+ # Combine two conditions with an and clause.
+ # NOTE: This modifies cond1 (and also returns it)
+ #
+ def combineConditions (cond1, cond2):
+ if cond1 == None or cond1 == {}:
+ return cond2
+ elif cond2 == None or cond2 == {}:
+ return cond1
+
+ if type(cond1) == type({}):
+ cond1 = buildConditionFromDict(cond1)
+ if type(cond2) == type({}):
+ cond1 = buildConditionFromDict(cond2)
+
+ if not len(cond1._children):
+ cond1._children = cond2._children
+ return cond1
+ elif len(cond2._children):
+ children = cond1._children[:]
+ cond1._children = []
+ _and = GCand(cond1)
+ _and._children = children
+ if len(cond2._children) > 1:
+ _and2 = GCand(cond1)
+ _and2._children = cond2._children[:]
+ else:
+ cond1._children.append(cond2._children[0])
+
+ return cond1
+
+
+ # creates an GCondition Tree out of an list of tokens in a prefix
+ # order.
+
+ def buildTreeFromPrefix(term):
+
+ # create a GCondition object as top object in the object stack
+ parent={0:(GCondition())}
+
+ # GCondition will have only one parameter
+ # add paramcount=1 to the parameter count stack
+ paramcount={0:1}
+
+ # set start level for stack to zero
+ level=0
+ for i in term:
+
+ # convert type into an object
+ if conditionElements.has_key(i[0]):
+ e=conditionElements[i[0]][2](parent[level])
+ level=level+1
+ # get parameter count
+ paramcount[level]=conditionElements[i[0]][0]
+ parent[level]=e
+ elif i[0]=="field":
+ e=GCField(parent[level], i[1])
+ paramcount[level]=paramcount[level]-1
+ if paramcount[level]==0:
+ level=level-1
+ elif i[0]=="const":
+ e=GCConst(parent[level], i[1])
+ paramcount.update({level:(paramcount[level]-1)})
+ if paramcount[level]==0:
+ level=level-1
+ # print "NAME: %s VALUE: %s LEVEL: %s PCOUNT: %s" % \
+ # (i[0],i[1],level,paramcount[level])
+
+ return parent[0];
+
+
+ def buildPrefixFromTree(conditionTree):
+ if type(conditionTree) != types.InstanceType:
+ raise ConditionError, "No valid condition tree"
+ else:
+ otype = string.lower(conditionTree._type[2:])
+
+ #
+ # care for objects without children
+ #
+ if otype == 'cfield':
+ return [('field',"%s" % conditionTree.name)]
+
+ elif otype == 'cconst':
+ return [('const',conditionTree.value)]
+
+ elif otype == 'param':
+ return [('param', conditionTree.getValue())]
+
+ #
+ # if its an conditional object, then process it's children
+ #
+ elif conditionElements.has_key(otype):
+ result=[]
+
+ # first add operator to the list
+ result.append((otype,'')); # ,None));
+
+
+ # change operations with more than there minimal element no into
+ # multiple operations with minimal elements
+ # reason: to prevent a b c d AND OR being not well defined
+ # because it can be a"a b c d AND AND OR" or "a b c d AND OR OR"
+ paramcount=len(conditionTree._children)
+ while (paramcount > \
+ conditionElements[otype][0]):
+ paramcount=paramcount-1
+ result.append((otype,''));
+
+
+ # then add children
+ for i in range(0, len(conditionTree._children)):
+ result = result + \
+ buildPrefixFromTree(conditionTree._children[i])
+
+ #
+ # check for integrity of condition
+ #
+ if len(conditionTree._children) < conditionElements[otype][0]:
+ raise GConditions.ConditionError, \
+ _('Condition element "%s" expects at least %s arguments; found
%s') % \
+ (otype, conditionElements[otype][0],
len(conditionTree._children))
+
+ if len(conditionTree._children) > conditionElements[otype][1]:
+ raise GConditions.ConditionError, \
+ _('Condition element "%s" expects at most %s arguments; found
%s') % \
+ (otype, conditionElements[otype][1],
len(conditionTree._children))
+
+
+ # return combination
+ return result;
+
+ else:
+ raise GConditions.ConditionNotSupported, \
+ _('Condition clause "%s" is not supported '+
+ 'by the condition to prefix table conversion.') % otype
+
+
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- gnue/common/src GConditions.py,
Jason Cater <=