commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r7753 - trunk/gnue-common/src/datasources


From: johannes
Subject: [gnue] r7753 - trunk/gnue-common/src/datasources
Date: Wed, 27 Jul 2005 03:52:04 -0500 (CDT)

Author: johannes
Date: 2005-07-27 03:52:03 -0500 (Wed, 27 Jul 2005)
New Revision: 7753

Modified:
   trunk/gnue-common/src/datasources/GConditions.py
Log:
combineConditions () has no side-effect anymore


Modified: trunk/gnue-common/src/datasources/GConditions.py
===================================================================
--- trunk/gnue-common/src/datasources/GConditions.py    2005-07-26 18:57:59 UTC 
(rev 7752)
+++ trunk/gnue-common/src/datasources/GConditions.py    2005-07-27 08:52:03 UTC 
(rev 7753)
@@ -1395,75 +1395,47 @@
 
 
 # =============================================================================
-# Combine two conditions with an AND clause. Side-effect: cond1 will be changed
+# Combine two conditions with an AND clause
 # =============================================================================
 
 def combineConditions (cond1, cond2):
   """
-  This function combines the two condition trees @cond1 and @cond2 using an AND
-  clause. Both arguments can be given as conditiontrees or as dictionaries. In
-  the latter case they would be converted using buildConditionFromDict (). As a
-  side effect of this function, @cond1 would be changed (if neccessary). If
-  neither of the conditions has a single top-level element of type GCand a new
-  element would be created.
+  Combine two conditions using an AND operator. Both arguments can be given as
+  condition trees (GCondition), dictionaries or prefix sequences. The resulting
+  combination is a *new* condition tree. None of the arguments will be changed.
+
+  @param cond1: condition-tree, -dictionary or -sequence (prefix list)
+  @param cond2: condition-tree, -dictionary or -sequence (prefix list)
+  @return: new GCondition instance with an AND-combination of both conditions
   """
 
-  # TODO: replace this code with a cleaner implementation. Avoid that
-  # side-effect !!! But since we do *not* use weak references for GObj trees
-  # replacing this code with another one produces memory leaks
+  # First check for the trivial cases. If there is only one part defined we can
+  # return a *copy* of that condition
+  if not cond1:
+    return buildCondition (buildCondition (cond2).prefixNotation ())
 
-  # check for the trivial cases
-  if cond1 is None or cond1 == {}:
-    return buildCondition (cond2)
+  elif not cond2:
+    return buildCondition (buildCondition (cond1).prefixNotation ())
 
-  elif cond2 is None or cond2 == {}:
-    return buildCondition (cond1)
+  # otherwise make sure to have GCondition instances on both sides
+  cond1 = buildCondition (cond1)
+  cond2 = buildCondition (cond2)
 
-  # make sure both parts are condition trees
-  if isinstance (cond1, dict):
-    cond1 = buildConditionFromDict (cond1)
+  # If the condition starts with an AND operator we start at that point,
+  # otherwise use the condition as it is
+  top1 = (cond1.findChildOfType ('GCand') or cond1).prefixNotation ()
+  top2 = (cond2.findChildOfType ('GCand') or cond2).prefixNotation ()
 
-  if isinstance (cond2, dict):
-    cond2 = buildConditionFromDict (cond2)
+  if top1 and top1 [0] == 'and': top1 = top1 [1:]
+  if top2 and top2 [0] == 'and': top2 = top2 [1:]
 
-  # if the master condition has no elements, assign the second condition's
-  # children
-  if not len (cond1._children):
-    cond1._children = cond2._children
-    update = cond1
+  ncond = ['and']
+  if top1: ncond.append (top1)
+  if top2: ncond.append (top2)
 
-  elif len (cond2._children):
-    # First normalize the master condition. This means we make sure to have a
-    # valid logical operator as single top element
-    if len (cond1._children) > 1 or not isinstance (cond1._children [0], 
GCand):
-      oldChildren = cond1._children [:]
-      cond1._children = []
-      parent = GCand (cond1)
-      parent._children = oldChildren
+  return buildCondition (ncond)
 
-    else:
-      parent = cond1._children [0]
 
-    # determine where to start in the second condition tree
-    buddy = cond2
-    while len (buddy._children) == 1 and \
-          isinstance (buddy._children [0], GCand):
-      buddy = buddy._children [0]
-
-    # and append all of the buddy's children to the top-level-and
-    parent._children.extend (buddy._children)
-
-    update = parent
-  else:
-    update = None
-
-  if update is not None:
-    for child in update._children:
-      child.setParent (update)
-
-  return cond1
-
-
 # =============================================================================
 # Unify all elements in values to the same type
 # =============================================================================





reply via email to

[Prev in Thread] Current Thread [Next in Thread]