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
29 """ulist ensures that all items are unique and provides an __onadditem__
30 hook to perform custom action in subclasses."""
31
32
33
34
35
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
49
55
59
61 if not list.__contains__(self, item):
62 list.append(self, item)
63 self.__onadditem__(item)
64
66 if not list.__contains__(self, item):
67 list.insert(self, i, item)
68 self.__onadditem__(item)
69
70 - def pop(self, i=-1):
74
78
79
80
81
82
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
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
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
110 """Append other to object."""
111 for item in other:
112 self.__onadditem__(item)
113
115 """Called for each item added. Override in subclasses for adding
116 custom action."""
117 pass
118
120 """Called for each item removed. Override in subclasses for
121 adding custom action."""
122 pass
123