1#************************************************************************* 2# 3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4# 5# Copyright 2000, 2010 Oracle and/or its affiliates. 6# 7# OpenOffice.org - a multi-platform office productivity suite 8# 9# This file is part of OpenOffice.org. 10# 11# OpenOffice.org is free software: you can redistribute it and/or modify 12# it under the terms of the GNU Lesser General Public License version 3 13# only, as published by the Free Software Foundation. 14# 15# OpenOffice.org is distributed in the hope that it will be useful, 16# but WITHOUT ANY WARRANTY; without even the implied warranty of 17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18# GNU Lesser General Public License version 3 for more details 19# (a copy is included in the LICENSE file that accompanied this code). 20# 21# You should have received a copy of the GNU Lesser General Public License 22# version 3 along with OpenOffice.org. If not, see 23# <http://www.openoffice.org/license.html> 24# for a copy of the LGPLv3 License. 25# 26#************************************************************************* 27 28# to support macosx baseline machines from Cretaceous period 29 30# incomplete set() class implementation of Python 2.4 31class PseudoSet: 32 _list = [] 33 34 def __str__(self): 35 return str(self._list) 36 37 def __init__(self, newlist=[]): 38 self._list = self._remove_dupes(newlist) 39 40 def __or__(self, other): 41 tmplist = [] 42 if self._list != None and other != None: 43 tmplist.extend(self._list) 44 tmplist.extend(other) 45 return PseudoSet(self._remove_dupes(tmplist)) 46 else: 47 print "__or__(None)" 48 49 def __sub__(self,other): 50 tmplist = [] 51 if self._list != None and other != None: 52 tmplist.extend(self._list) 53 [tmplist.remove(key) for key in other if key in tmplist] 54 else: 55 print "__sub__(none)" 56 return PseudoSet(tmplist) 57 58 def __and__(self, other): 59 tmplist = [] 60 if other != None and self._list != None: 61 [tmplist.append(key) for key in self._list if key in other] 62 return PseudoSet(tmplist) 63 else: 64 print "__and__(None)" 65 66 def __iter__(self): 67 return self._list.__iter__() 68 69 def __items__(self): 70 return self._list.items() 71 72 def __keys__(self): 73 return keys(self._list) 74 75 def _remove_dupes(self, list): 76 tmpdict = {} 77 for key in list: 78 tmpdict[key] = 1 79 return tmpdict.keys() 80 81# incomplete OrderedDict() class implementation 82class PseudoOrderedDict(dict): 83 _keylist = [] 84 _valuelist = [] 85 86 def __init__(self, defaults={}): 87 dict.__init__(self) 88 for n,v in defaults.items(): 89 self[n] = v 90 91 def __setitem__(self, key, value): 92 self._keylist.append(key) 93 self._valuelist.append(value) 94 return dict.__setitem__(self, key, value) 95 96 def __delattr__(self, key): 97 self._keylist.__delattr__(key) 98 self._valuelist.__delattr__(dict[key]) 99 return dict.__delattr__(self, key) 100 101 def __delitem__(self, key): 102 self._keylist.__delitem__(key) 103 self._valuelist.__delitem__(dict[key]) 104 return dict.__delitem__(self, key) 105 106 def __iter__(self): 107 raise NotImplementedError("__iter__") 108 109 def __iterkeys__(self): 110 return self._keylist 111 112 def iteritems(self): 113 #return self._valuelist 114 return zip(self._keylist, self._valuelist) 115 116 def items(self): 117 return zip(self._keylist,self._valuelist) 118 119 def __keys__(self): 120 return self._keylist 121 122 def keys(self): 123 return self._keylist 124 125 def __keysattr__(self): 126 return self._keylist 127 128 def pop(self, key): 129 self._keylist.pop(key) 130 self._valuelist.pop(key) 131 return dict.__pop__(self, key) 132 133 def popitem(self): 134 raise NotImplementedError("popitem") 135 136def _testdriver_set(): 137 list, list1 = [] ,[] 138 list.append("a") 139 list.append("b") 140 list.append("c") 141 142 list1.append("a") 143 list1.append("b") 144 list1.append("d") 145 list1.append("e") 146 list1.append("e") 147 148 if "a" in list: 149 print "YEAH!" 150 151 a = PseudoSet(list) 152 b = PseudoSet(list1) 153 154 print "a="+str(a) 155 print "b="+str(b) 156 print "a|b=" + str(a|b) 157 print "a="+str(a) 158 print "b="+str(b) 159 print "a&b=" + str(a&b) 160 print "a="+str(a) 161 print "b="+str(b) 162 print "a-b" + str(a-b) 163 164 for key in a: 165 print key 166 167def _testdriver_dict(): 168 d = PseudoOrderedDict() 169 d["a"] = 1 170 d["b"] = 2 171 d["c"] = 3 172 d["d"] = 4 173 d["e"] = 5 174 d["f"] = 6 175 176 print "a="+str(d["a"]) 177 print "e="+str(d["e"]) 178 for key,value in d.iteritems(): 179 print "d["+key+"]="+str(d[key]) 180 print "key="+str(key)+" value="+str(value) 181 182 print "keys="+str(d.keys()) 183 184#_testdriver_dict() 185