You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

100 lines
4.0 KiB

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