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