1 # -*- coding: utf-8 -*-
3 ## Copyright (C)2006 Ingeniweb
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.
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.
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.
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.
22 __version__
= "$Revision: $"
24 # $Id: DynaList.py 30098 2006-09-08 12:35:01Z encolpe $
25 __docformat__
= 'restructuredtext'
28 def __init__(self
, initlist
=None):
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()
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
58 self
.data()[i
:j
] = list(other
)
59 def __delslice__(self
, i
, j
):
60 i
= max(i
, 0); j
= max(j
, 0)
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
)
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())
75 return self
.__class
__(list(other
) + self
.data())
76 def __iadd__(self
, other
):
77 raise NotImplementedError, "Not implemented"
80 return self
.__class
__(self
.data()*n
)
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())
96 self
.data().extend(other
)