• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/opal/ZSI/ZSI/wstools/UserTuple.py

00001 """
00002 A more or less complete user-defined wrapper around tuple objects.
00003 Adapted version of the standard library's UserList.
00004 
00005 Taken from Stefan Schwarzer's ftputil library, available at
00006 <http://www.ndh.net/home/sschwarzer/python/python_software.html>, and used under this license:
00007 
00008 
00009 
00010 
00011 Copyright (C) 1999, Stefan Schwarzer 
00012 All rights reserved.
00013 
00014 Redistribution and use in source and binary forms, with or without
00015 modification, are permitted provided that the following conditions are
00016 met:
00017 
00018 - Redistributions of source code must retain the above copyright
00019   notice, this list of conditions and the following disclaimer.
00020 
00021 - Redistributions in binary form must reproduce the above copyright
00022   notice, this list of conditions and the following disclaimer in the
00023   documentation and/or other materials provided with the distribution.
00024 
00025 - Neither the name of the above author nor the names of the
00026   contributors to the software may be used to endorse or promote
00027   products derived from this software without specific prior written
00028   permission.
00029 
00030 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00031 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00032 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00033 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
00034 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00035 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00036 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00037 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00038 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00039 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00040 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00041 
00042 """
00043 
00044 
00045 
00046 
00047 # $Id: UserTuple.py 277 2003-07-21 14:18:54Z warnes $
00048 
00049 #XXX tuple instances (in Python 2.2) contain also:
00050 #   __class__, __delattr__, __getattribute__, __hash__, __new__,
00051 #   __reduce__, __setattr__, __str__
00052 # What about these?
00053 
00054 class UserTuple:
00055     def __init__(self, inittuple=None):
00056         self.data = ()
00057         if inittuple is not None:
00058             # XXX should this accept an arbitrary sequence?
00059             if type(inittuple) == type(self.data):
00060                 self.data = inittuple
00061             elif isinstance(inittuple, UserTuple):
00062                 # this results in
00063                 #   self.data is inittuple.data
00064                 # but that's ok for tuples because they are
00065                 # immutable. (Builtin tuples behave the same.)
00066                 self.data = inittuple.data[:]
00067             else:
00068                 # the same applies here; (t is tuple(t)) == 1
00069                 self.data = tuple(inittuple)
00070     def __repr__(self): return repr(self.data)
00071     def __lt__(self, other): return self.data <  self.__cast(other)
00072     def __le__(self, other): return self.data <= self.__cast(other)
00073     def __eq__(self, other): return self.data == self.__cast(other)
00074     def __ne__(self, other): return self.data != self.__cast(other)
00075     def __gt__(self, other): return self.data >  self.__cast(other)
00076     def __ge__(self, other): return self.data >= self.__cast(other)
00077     def __cast(self, other):
00078         if isinstance(other, UserTuple): return other.data
00079         else: return other
00080     def __cmp__(self, other):
00081         return cmp(self.data, self.__cast(other))
00082     def __contains__(self, item): return item in self.data
00083     def __len__(self): return len(self.data)
00084     def __getitem__(self, i): return self.data[i]
00085     def __getslice__(self, i, j):
00086         i = max(i, 0); j = max(j, 0)
00087         return self.__class__(self.data[i:j])
00088     def __add__(self, other):
00089         if isinstance(other, UserTuple):
00090             return self.__class__(self.data + other.data)
00091         elif isinstance(other, type(self.data)):
00092             return self.__class__(self.data + other)
00093         else:
00094             return self.__class__(self.data + tuple(other))
00095     # dir( () ) contains no __radd__ (at least in Python 2.2)
00096     def __mul__(self, n):
00097         return self.__class__(self.data*n)
00098     __rmul__ = __mul__
00099 

Generated on Wed Oct 20 2010 11:12:16 for APBS by  doxygen 1.7.2