mailr18160 - /trunk/generic_fns/selection.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on December 18, 2012 - 11:22:
Author: bugman
Date: Tue Dec 18 11:22:11 2012
New Revision: 18160

URL: http://svn.gna.org/viewcvs/relax?rev=18160&view=rev
Log:
Shifted the boolean selection operations of the generic_fns.selection module 
into two new functions.

These are the boolean_select() and boolean_deselect() functions.  The change 
removes much duplicated
code which could be a source of bugs in the future.


Modified:
    trunk/generic_fns/selection.py

Modified: trunk/generic_fns/selection.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/generic_fns/selection.py?rev=18160&r1=18159&r2=18160&view=diff
==============================================================================
--- trunk/generic_fns/selection.py (original)
+++ trunk/generic_fns/selection.py Tue Dec 18 11:22:11 2012
@@ -51,6 +51,68 @@
 boolean_doc.add_table(table.label)
 
 
+def boolean_deselect(current=None, boolean=None):
+    """Return the new boolean deselection result using the current selection.
+
+    @keyword current:   The current selection state.
+    @type current:      bool
+    @keyword boolean:   The boolean operator used to select with.  It can be 
one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'.
+    @type boolean:      str
+    @return:            The new selection state.
+    @rtype:             bool
+    """
+
+    # Boolean selections.
+    if boolean == 'OR':
+        state = current or False
+    elif boolean == 'NOR':
+        state = not (current or False)
+    elif boolean == 'AND':
+        state = current and False
+    elif boolean == 'NAND':
+        state = not (current and False)
+    elif boolean == 'XOR':
+        state = not (current and False) and (current or False)
+    elif boolean == 'XNOR':
+        state = (current and False) or not (current or False)
+    else:
+        raise RelaxError("Unknown boolean operator " + repr(boolean))
+
+    # Return the new selection state.
+    return state
+
+
+def boolean_select(current=None, boolean=None):
+    """Return the new boolean selection result using the current selection.
+
+    @keyword current:   The current selection state.
+    @type current:      bool
+    @keyword boolean:   The boolean operator used to select with.  It can be 
one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'.
+    @type boolean:      str
+    @return:            The new selection state.
+    @rtype:             bool
+    """
+
+    # Boolean selections.
+    if boolean == 'OR':
+        state = current or True
+    elif boolean == 'NOR':
+        state = not (current or True)
+    elif boolean == 'AND':
+        state = current and True
+    elif boolean == 'NAND':
+        state = not (current and True)
+    elif boolean == 'XOR':
+        state = not (current and True) and (current or True)
+    elif boolean == 'XNOR':
+        state = (current and True) or not (current or True)
+    else:
+        raise RelaxError("Unknown boolean operator " + repr(boolean))
+
+    # Return the new selection state.
+    return state
+
+
 def desel_all():
     """Deselect all spins.
 
@@ -105,20 +167,7 @@
 
         # Boolean selections.
         else:
-            if boolean == 'OR':
-                interatom.select = interatom.select or False
-            elif boolean == 'NOR':
-                interatom.select = not (interatom.select or False)
-            elif boolean == 'AND':
-                interatom.select = interatom.select and False
-            elif boolean == 'NAND':
-                interatom.select = not (interatom.select and False)
-            elif boolean == 'XOR':
-                interatom.select = not (interatom.select and False) and 
(interatom.select or False)
-            elif boolean == 'XNOR':
-                interatom.select = (interatom.select and False) or not 
(interatom.select or False)
-            else:
-                raise RelaxError("Unknown boolean operator " + repr(boolean))
+            interatom.select = boolean_deselect(current=interatom.select, 
boolean=boolean)
 
 
 def desel_read(file=None, dir=None, file_data=None, spin_id_col=None, 
mol_name_col=None, res_num_col=None, res_name_col=None, spin_num_col=None, 
spin_name_col=None, sep=None, spin_id=None, boolean='AND', change_all=False):
@@ -199,20 +248,7 @@
 
         # Boolean selections.
         else:
-            if boolean == 'OR':
-                spin.select = spin.select or False
-            elif boolean == 'NOR':
-                spin.select = not (spin.select or False)
-            elif boolean == 'AND':
-                spin.select = spin.select and False
-            elif boolean == 'NAND':
-                spin.select = not (spin.select and False)
-            elif boolean == 'XOR':
-                spin.select = not (spin.select and False) and (spin.select 
or False)
-            elif boolean == 'XNOR':
-                spin.select = (spin.select and False) or not (spin.select or 
False)
-            else:
-                raise RelaxError("Unknown boolean operator " + repr(boolean))
+            spin.select = boolean_deselect(current=spin.select, 
boolean=boolean)
 
 
 def desel_spin(spin_id=None, boolean='AND', change_all=False):
@@ -253,20 +289,7 @@
 
         # Boolean selections.
         else:
-            if boolean == 'OR':
-                spin.select = spin.select or False
-            elif boolean == 'NOR':
-                spin.select = not (spin.select or False)
-            elif boolean == 'AND':
-                spin.select = spin.select and False
-            elif boolean == 'NAND':
-                spin.select = not (spin.select and False)
-            elif boolean == 'XOR':
-                spin.select = not (spin.select and False) and (spin.select 
or False)
-            elif boolean == 'XNOR':
-                spin.select = (spin.select and False) or not (spin.select or 
False)
-            else:
-                raise RelaxError("Unknown boolean operator " + repr(boolean))
+            spin.select = boolean_deselect(current=spin.select, 
boolean=boolean)
 
 
 def is_mol_selected(selection=None):
@@ -397,20 +420,7 @@
 
         # Boolean selections.
         else:
-            if boolean == 'OR':
-                interatom.select = interatom.select or True
-            elif boolean == 'NOR':
-                interatom.select = not (interatom.select or True)
-            elif boolean == 'AND':
-                interatom.select = interatom.select and True
-            elif boolean == 'NAND':
-                interatom.select = not (interatom.select and True)
-            elif boolean == 'XOR':
-                interatom.select = not (interatom.select and True) and 
(interatom.select or True)
-            elif boolean == 'XNOR':
-                interatom.select = (interatom.select and True) or not 
(interatom.select or True)
-            else:
-                raise RelaxError("Unknown boolean operator " + repr(boolean))
+            interatom.select = boolean_select(current=interatom.select, 
boolean=boolean)
 
 
 def sel_read(file=None, dir=None, file_data=None, spin_id_col=None, 
mol_name_col=None, res_num_col=None, res_name_col=None, spin_num_col=None, 
spin_name_col=None, sep=None, spin_id=None, boolean='OR', change_all=False):
@@ -491,20 +501,7 @@
 
         # Boolean selections.
         else:
-            if boolean == 'OR':
-                spin.select = spin.select or True
-            elif boolean == 'NOR':
-                spin.select = not (spin.select or True)
-            elif boolean == 'AND':
-                spin.select = spin.select and True
-            elif boolean == 'NAND':
-                spin.select = not (spin.select and True)
-            elif boolean == 'XOR':
-                spin.select = not (spin.select and True) and (spin.select or 
True)
-            elif boolean == 'XNOR':
-                spin.select = (spin.select and True) or not (spin.select or 
True)
-            else:
-                raise RelaxError("Unknown boolean operator " + repr(boolean))
+            spin.select = boolean_select(current=spin.select, 
boolean=boolean)
 
 
 def sel_spin(spin_id=None, boolean='OR', change_all=False):
@@ -546,17 +543,4 @@
 
         # Boolean selections.
         else:
-            if boolean == 'OR':
-                spin.select = spin.select or True
-            elif boolean == 'NOR':
-                spin.select = not (spin.select or True)
-            elif boolean == 'AND':
-                spin.select = spin.select and True
-            elif boolean == 'NAND':
-                spin.select = not (spin.select and True)
-            elif boolean == 'XOR':
-                spin.select = not (spin.select and True) and (spin.select or 
True)
-            elif boolean == 'XNOR':
-                spin.select = (spin.select and True) or not (spin.select or 
True)
-            else:
-                raise RelaxError("Unknown boolean operator " + repr(boolean))
+            spin.select = boolean_select(current=spin.select, 
boolean=boolean)




Related Messages


Powered by MHonArc, Updated Tue Dec 18 11:40:02 2012