Package Biskit :: Package Dock :: Module FixedList
[hide private]
[frames] | no frames]

Source Code for Module Biskit.Dock.FixedList

  1  #!/usr/bin/env python 
  2  ## 
  3  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  4  ## Copyright (C) 2004-2006 Raik Gruenberg & Johan Leckner 
  5  ## 
  6  ## This program is free software; you can redistribute it and/or 
  7  ## modify it under the terms of the GNU General Public License as 
  8  ## published by the Free Software Foundation; either version 2 of the 
  9  ## License, or any later version. 
 10  ## 
 11  ## This program is distributed in the hope that it will be useful, 
 12  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 14  ## General Public License for more details. 
 15  ## 
 16  ## You find a copy of the GNU General Public License in the file 
 17  ## license.txt along with this program; if not, write to the Free 
 18  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 19  ## 
 20  ## 
 21  ## last $Author: graik $ 
 22  ## last $Date: 2007/03/05 10:28:22 $ 
 23  ## $Revision: 2.8 $ 
 24   
 25  """ 
 26  List that blocks any modifications 
 27  """ 
 28   
29 -class FixedListError( Exception ):
30 pass
31
32 -class FixedList( list ):
33 """ 34 List that blocks any modifications. Implementing lists must override 35 fixed() to allow adding of items under some circumstances (like during 36 creation of the list). 37 """ 38
39 - def fixed(self):
40 """ 41 @return: if list is fixed and modifications are prohibited 42 @rtype: true 43 """ 44 return 1
45 46
47 - def __stop(self):
48 """ 49 @raise FixedListError: if attempt to modify fixed list 50 """ 51 if self.fixed(): 52 raise FixedListError("Attempt to modify fixed list.")
53
54 - def __setitem__(self, i, v ):
55 self.__stop() 56 super( FixedList, self).__setitem__( i, v )
57
58 - def __setslice__(self, i, j, v ):
59 self.__stop() 60 super( FixedList, self).__setslice__( i, j, v )
61
62 - def __add__( self, lst ):
63 self.__stop() 64 super( FixedList, self).__add__( lst )
65
66 - def __iadd__( self, lst ):
67 self.__stop() 68 super( FixedList, self).__iadd__( lst )
69
70 - def extend( self, lst ):
71 self.__stop() 72 super( FixedList, self).extend( lst )
73
74 - def append( self, v ):
75 self.__stop() 76 super( FixedList, self).append( v )
77
78 - def __delitem__(self, i ):
79 self.__stop() 80 super( FixedList, self).__delitem__( i )
81 82 83 84 ############# 85 ## TESTING 86 ############# 87 import Biskit.test as BT 88
89 -class Test(BT.BiskitTest):
90 """Test case""" 91
92 - def test_FixedList(self):
93 """Dock.FixedList test""" 94 self.lst = range(10) 95 96 self.f = FixedList( self.lst ) 97 98 if self.local: print 'f.fixed() is %i for a FixedList'% self.f.fixed() 99 100 self.assertRaises( FixedListError, self.f.append, 6 )
101 102 103 if __name__ == '__main__': 104 105 BT.localTest() 106