Compat zope-2.12 : résolution des problèmes avec les interfaces.
[GroupUserFolder.git] / DynaList.py
1 # -*- coding: utf-8 -*-
2 ## GroupUserFolder
3 ## Copyright (C)2006 Ingeniweb
4
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 2 of the License, or
8 ## (at your option) any later version.
9
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
14
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; see the file COPYING. If not, write to the
17 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 """
19 DynaList.py => a list that has dynamic data (ie. calculated by a 'data' method).
20 Please override this class and define a data(self,) method that will return the actual list.
21 """
22 __version__ = "$Revision: $"
23 # $Source: $
24 # $Id: DynaList.py 30098 2006-09-08 12:35:01Z encolpe $
25 __docformat__ = 'restructuredtext'
26
27 class DynaList:
28 def __init__(self, initlist=None):
29 pass
30
31 def __repr__(self): return repr(self.data())
32 def __lt__(self, other): return self.data() < self.__cast(other)
33 def __le__(self, other): return self.data() <= self.__cast(other)
34 def __eq__(self, other): return self.data() == self.__cast(other)
35 def __ne__(self, other): return self.data() != self.__cast(other)
36 def __gt__(self, other): return self.data() > self.__cast(other)
37 def __ge__(self, other): return self.data() >= self.__cast(other)
38 def __cast(self, other):
39 if isinstance(other, UserList): return other.data()
40 else: return other
41 def __cmp__(self, other):
42 raise RuntimeError, "UserList.__cmp__() is obsolete"
43 def __contains__(self, item): return item in self.data()
44 def __len__(self): return len(self.data())
45 def __getitem__(self, i): return self.data()[i]
46 def __setitem__(self, i, item): self.data()[i] = item
47 def __delitem__(self, i): del self.data()[i]
48 def __getslice__(self, i, j):
49 i = max(i, 0); j = max(j, 0)
50 return self.__class__(self.data()[i:j])
51 def __setslice__(self, i, j, other):
52 i = max(i, 0); j = max(j, 0)
53 if isinstance(other, UserList):
54 self.data()[i:j] = other.data()
55 elif isinstance(other, type(self.data())):
56 self.data()[i:j] = other
57 else:
58 self.data()[i:j] = list(other)
59 def __delslice__(self, i, j):
60 i = max(i, 0); j = max(j, 0)
61 del self.data()[i:j]
62 def __add__(self, other):
63 if isinstance(other, UserList):
64 return self.__class__(self.data() + other.data())
65 elif isinstance(other, type(self.data())):
66 return self.__class__(self.data() + other)
67 else:
68 return self.__class__(self.data() + list(other))
69 def __radd__(self, other):
70 if isinstance(other, UserList):
71 return self.__class__(other.data() + self.data())
72 elif isinstance(other, type(self.data())):
73 return self.__class__(other + self.data())
74 else:
75 return self.__class__(list(other) + self.data())
76 def __iadd__(self, other):
77 raise NotImplementedError, "Not implemented"
78
79 def __mul__(self, n):
80 return self.__class__(self.data()*n)
81 __rmul__ = __mul__
82 def __imul__(self, n):
83 raise NotImplementedError, "Not implemented"
84 def append(self, item): self.data().append(item)
85 def insert(self, i, item): self.data().insert(i, item)
86 def pop(self, i=-1): return self.data().pop(i)
87 def remove(self, item): self.data().remove(item)
88 def count(self, item): return self.data().count(item)
89 def index(self, item): return self.data().index(item)
90 def reverse(self): self.data().reverse()
91 def sort(self, *args): apply(self.data().sort, args)
92 def extend(self, other):
93 if isinstance(other, UserList):
94 self.data().extend(other.data())
95 else:
96 self.data().extend(other)