added orphan share rate graph
[p2pool.git] / wstools / UserTuple.py
1 """
2 A more or less complete user-defined wrapper around tuple objects.
3 Adapted version of the standard library's UserList.
4
5 Taken from Stefan Schwarzer's ftputil library, available at
6 <http://www.ndh.net/home/sschwarzer/python/python_software.html>, and used under this license:
7
8
9
10
11 Copyright (C) 1999, Stefan Schwarzer 
12 All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are
16 met:
17
18 - Redistributions of source code must retain the above copyright
19   notice, this list of conditions and the following disclaimer.
20
21 - Redistributions in binary form must reproduce the above copyright
22   notice, this list of conditions and the following disclaimer in the
23   documentation and/or other materials provided with the distribution.
24
25 - Neither the name of the above author nor the names of the
26   contributors to the software may be used to endorse or promote
27   products derived from this software without specific prior written
28   permission.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
34 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
42 """
43
44
45
46
47 # $Id$
48
49 #XXX tuple instances (in Python 2.2) contain also:
50 #   __class__, __delattr__, __getattribute__, __hash__, __new__,
51 #   __reduce__, __setattr__, __str__
52 # What about these?
53
54 class UserTuple:
55     def __init__(self, inittuple=None):
56         self.data = ()
57         if inittuple is not None:
58             # XXX should this accept an arbitrary sequence?
59             if type(inittuple) == type(self.data):
60                 self.data = inittuple
61             elif isinstance(inittuple, UserTuple):
62                 # this results in
63                 #   self.data is inittuple.data
64                 # but that's ok for tuples because they are
65                 # immutable. (Builtin tuples behave the same.)
66                 self.data = inittuple.data[:]
67             else:
68                 # the same applies here; (t is tuple(t)) == 1
69                 self.data = tuple(inittuple)
70     def __repr__(self): return repr(self.data)
71     def __lt__(self, other): return self.data <  self.__cast(other)
72     def __le__(self, other): return self.data <= self.__cast(other)
73     def __eq__(self, other): return self.data == self.__cast(other)
74     def __ne__(self, other): return self.data != self.__cast(other)
75     def __gt__(self, other): return self.data >  self.__cast(other)
76     def __ge__(self, other): return self.data >= self.__cast(other)
77     def __cast(self, other):
78         if isinstance(other, UserTuple): return other.data
79         else: return other
80     def __cmp__(self, other):
81         return cmp(self.data, self.__cast(other))
82     def __contains__(self, item): return item in self.data
83     def __len__(self): return len(self.data)
84     def __getitem__(self, i): return self.data[i]
85     def __getslice__(self, i, j):
86         i = max(i, 0); j = max(j, 0)
87         return self.__class__(self.data[i:j])
88     def __add__(self, other):
89         if isinstance(other, UserTuple):
90             return self.__class__(self.data + other.data)
91         elif isinstance(other, type(self.data)):
92             return self.__class__(self.data + other)
93         else:
94             return self.__class__(self.data + tuple(other))
95     # dir( () ) contains no __radd__ (at least in Python 2.2)
96     def __mul__(self, n):
97         return self.__class__(self.data*n)
98     __rmul__ = __mul__
99