Package smartcard :: Module ulist
[hide private]
[frames] | no frames]

Source Code for Module smartcard.ulist

  1  """ulist is a subclass of list where items cannot appear twice in the list. 
  2   
  3  [1,2,2,3,3,4] is a valid list, whereas in ulist we can only have [1,2,3,4]. 
  4   
  5  __author__ = "gemalto http://www.gemalto.com" 
  6   
  7  Copyright 2001-2012 gemalto 
  8  Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com 
  9   
 10  This file is part of pyscard. 
 11   
 12  pyscard is free software; you can redistribute it and/or modify 
 13  it under the terms of the GNU Lesser General Public License as published by 
 14  the Free Software Foundation; either version 2.1 of the License, or 
 15  (at your option) any later version. 
 16   
 17  pyscard is distributed in the hope that it will be useful, 
 18  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  GNU Lesser General Public License for more details. 
 21   
 22  You should have received a copy of the GNU Lesser General Public License 
 23  along with pyscard; if not, write to the Free Software 
 24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 25  """ 
 26   
 27   
28 -class ulist(list):
29 """ulist ensures that all items are unique and provides an __onadditem__ 30 hook to perform custom action in subclasses.""" 31 32 # 33 # override list methods 34 # 35
36 - def __init__(self, initlist=None):
37 if initlist is not None and initlist != []: 38 list.__init__(self, [initlist[0]]) 39 for item in initlist[1:]: 40 if not list.__contains__(self, item): 41 list.append(self, item) 42 else: 43 list.__init__(self, initlist)
44
45 - def __add__(self, other):
46 newother = self.__remove_duplicates(other) 47 self.__appendother__(newother) 48 return self.__class__(list(self) + list(newother))
49
50 - def __iadd__(self, other):
51 newother = self.__remove_duplicates(other) 52 self.__appendother__(newother) 53 list.__iadd__(self, newother) 54 return self
55
56 - def __radd__(self, other):
57 newother = self.__remove_duplicates(other) 58 return list.__add__(self, newother)
59
60 - def append(self, item):
61 if not list.__contains__(self, item): 62 list.append(self, item) 63 self.__onadditem__(item)
64
65 - def insert(self, i, item):
66 if not list.__contains__(self, item): 67 list.insert(self, i, item) 68 self.__onadditem__(item)
69
70 - def pop(self, i=-1):
71 item = list.pop(self, i) 72 self.__onremoveitem__(item) 73 return item
74
75 - def remove(self, item):
76 list.remove(self, item) 77 self.__onremoveitem__(item)
78 79 # 80 # non list methods 81 # 82
83 - def __remove_duplicates(self, _other):
84 """Remove from other items already in list.""" 85 if not isinstance(_other, type(self)) \ 86 and not isinstance(_other, type(list)) \ 87 and not isinstance(_other, type([])): 88 other = [_other] 89 else: 90 other = list(_other) 91 92 # remove items already in self 93 newother = [] 94 for i in range(0, len(other)): 95 item = other.pop(0) 96 if not list.__contains__(self, item): 97 newother.append(item) 98 99 # remove duplicate items in other 100 other = [] 101 if newother != []: 102 other.append(newother[0]) 103 for i in range(1, len(newother)): 104 item = newother.pop() 105 if not other.__contains__(item): 106 other.append(item) 107 return other
108
109 - def __appendother__(self, other):
110 """Append other to object.""" 111 for item in other: 112 self.__onadditem__(item)
113
114 - def __onadditem__(self, item):
115 """Called for each item added. Override in subclasses for adding 116 custom action.""" 117 pass
118
119 - def __onremoveitem__(self, item):
120 """Called for each item removed. Override in subclasses for 121 adding custom action.""" 122 pass
123