xref: /aoo42x/main/l10ntools/scripts/tool/pseudo.py (revision ae54856b)
1a0428e9eSAndrew Rist#**************************************************************
2a0428e9eSAndrew Rist#
3a0428e9eSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4a0428e9eSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5a0428e9eSAndrew Rist#  distributed with this work for additional information
6a0428e9eSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7a0428e9eSAndrew Rist#  to you under the Apache License, Version 2.0 (the
8a0428e9eSAndrew Rist#  "License"); you may not use this file except in compliance
9a0428e9eSAndrew Rist#  with the License.  You may obtain a copy of the License at
10a0428e9eSAndrew Rist#
11a0428e9eSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12a0428e9eSAndrew Rist#
13a0428e9eSAndrew Rist#  Unless required by applicable law or agreed to in writing,
14a0428e9eSAndrew Rist#  software distributed under the License is distributed on an
15a0428e9eSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a0428e9eSAndrew Rist#  KIND, either express or implied.  See the License for the
17a0428e9eSAndrew Rist#  specific language governing permissions and limitations
18a0428e9eSAndrew Rist#  under the License.
19a0428e9eSAndrew Rist#
20a0428e9eSAndrew Rist#**************************************************************
21cdf0e10cSrcweir
22*ae54856bSPedro Giffuni# to support macosx baseline machines from Cretaceous period
23cdf0e10cSrcweir
24cdf0e10cSrcweir# incomplete set() class implementation of Python 2.4
25cdf0e10cSrcweirclass PseudoSet:
26cdf0e10cSrcweir    _list = []
27*ae54856bSPedro Giffuni
28cdf0e10cSrcweir    def __str__(self):
29cdf0e10cSrcweir        return str(self._list)
30cdf0e10cSrcweir
31cdf0e10cSrcweir    def __init__(self, newlist=[]):
32cdf0e10cSrcweir        self._list = self._remove_dupes(newlist)
33cdf0e10cSrcweir
34cdf0e10cSrcweir    def __or__(self, other):
35cdf0e10cSrcweir        tmplist = []
36cdf0e10cSrcweir        if self._list != None and other != None:
37cdf0e10cSrcweir            tmplist.extend(self._list)
38cdf0e10cSrcweir            tmplist.extend(other)
39cdf0e10cSrcweir            return PseudoSet(self._remove_dupes(tmplist))
40cdf0e10cSrcweir        else:
41cdf0e10cSrcweir            print "__or__(None)"
42cdf0e10cSrcweir
43cdf0e10cSrcweir    def __sub__(self,other):
44cdf0e10cSrcweir        tmplist = []
45cdf0e10cSrcweir        if self._list != None and other != None:
46cdf0e10cSrcweir            tmplist.extend(self._list)
47cdf0e10cSrcweir            [tmplist.remove(key) for key in other if key in tmplist]
48cdf0e10cSrcweir        else:
49cdf0e10cSrcweir            print "__sub__(none)"
50cdf0e10cSrcweir        return PseudoSet(tmplist)
51cdf0e10cSrcweir
52cdf0e10cSrcweir    def __and__(self, other):
53cdf0e10cSrcweir        tmplist = []
54cdf0e10cSrcweir        if other != None and self._list != None:
55cdf0e10cSrcweir            [tmplist.append(key) for key in self._list if key in other]
56cdf0e10cSrcweir            return PseudoSet(tmplist)
57cdf0e10cSrcweir        else:
58cdf0e10cSrcweir            print "__and__(None)"
59*ae54856bSPedro Giffuni
60cdf0e10cSrcweir    def __iter__(self):
61cdf0e10cSrcweir        return self._list.__iter__()
62*ae54856bSPedro Giffuni
63cdf0e10cSrcweir    def __items__(self):
64cdf0e10cSrcweir        return self._list.items()
65cdf0e10cSrcweir
66cdf0e10cSrcweir    def __keys__(self):
67cdf0e10cSrcweir        return keys(self._list)
68cdf0e10cSrcweir
69cdf0e10cSrcweir    def _remove_dupes(self, list):
70*ae54856bSPedro Giffuni        tmpdict = {}
71cdf0e10cSrcweir        for key in list:
72cdf0e10cSrcweir            tmpdict[key] = 1
73cdf0e10cSrcweir        return tmpdict.keys()
74cdf0e10cSrcweir
75*ae54856bSPedro Giffuni# incomplete OrderedDict() class implementation
76cdf0e10cSrcweirclass PseudoOrderedDict(dict):
77cdf0e10cSrcweir    _keylist        = []
78cdf0e10cSrcweir    _valuelist      = []
79*ae54856bSPedro Giffuni
80cdf0e10cSrcweir    def __init__(self, defaults={}):
81cdf0e10cSrcweir        dict.__init__(self)
82cdf0e10cSrcweir        for n,v in defaults.items():
83cdf0e10cSrcweir            self[n] = v
84cdf0e10cSrcweir
85cdf0e10cSrcweir    def __setitem__(self, key, value):
86cdf0e10cSrcweir        self._keylist.append(key)
87cdf0e10cSrcweir        self._valuelist.append(value)
88cdf0e10cSrcweir        return dict.__setitem__(self, key, value)
89*ae54856bSPedro Giffuni
90cdf0e10cSrcweir    def __delattr__(self, key):
91cdf0e10cSrcweir        self._keylist.__delattr__(key)
92cdf0e10cSrcweir        self._valuelist.__delattr__(dict[key])
93cdf0e10cSrcweir        return dict.__delattr__(self, key)
94cdf0e10cSrcweir
95cdf0e10cSrcweir    def __delitem__(self, key):
96cdf0e10cSrcweir        self._keylist.__delitem__(key)
97cdf0e10cSrcweir        self._valuelist.__delitem__(dict[key])
98cdf0e10cSrcweir        return dict.__delitem__(self, key)
99*ae54856bSPedro Giffuni
100cdf0e10cSrcweir    def __iter__(self):
101cdf0e10cSrcweir        raise NotImplementedError("__iter__")
102*ae54856bSPedro Giffuni
103cdf0e10cSrcweir    def __iterkeys__(self):
104cdf0e10cSrcweir        return self._keylist
105*ae54856bSPedro Giffuni
106cdf0e10cSrcweir    def iteritems(self):
107cdf0e10cSrcweir        #return self._valuelist
108cdf0e10cSrcweir        return zip(self._keylist, self._valuelist)
109*ae54856bSPedro Giffuni
110cdf0e10cSrcweir    def items(self):
111cdf0e10cSrcweir        return zip(self._keylist,self._valuelist)
112cdf0e10cSrcweir
113cdf0e10cSrcweir    def __keys__(self):
114cdf0e10cSrcweir        return self._keylist
115cdf0e10cSrcweir
116cdf0e10cSrcweir    def keys(self):
117cdf0e10cSrcweir        return self._keylist
118cdf0e10cSrcweir
119cdf0e10cSrcweir    def __keysattr__(self):
120cdf0e10cSrcweir        return self._keylist
121*ae54856bSPedro Giffuni
122cdf0e10cSrcweir    def pop(self, key):
123cdf0e10cSrcweir        self._keylist.pop(key)
124cdf0e10cSrcweir        self._valuelist.pop(key)
125cdf0e10cSrcweir        return dict.__pop__(self, key)
126*ae54856bSPedro Giffuni
127cdf0e10cSrcweir    def popitem(self):
128cdf0e10cSrcweir        raise NotImplementedError("popitem")
129*ae54856bSPedro Giffuni
130cdf0e10cSrcweirdef _testdriver_set():
131cdf0e10cSrcweir    list, list1 = [] ,[]
132cdf0e10cSrcweir    list.append("a")
133cdf0e10cSrcweir    list.append("b")
134cdf0e10cSrcweir    list.append("c")
135cdf0e10cSrcweir
136cdf0e10cSrcweir    list1.append("a")
137cdf0e10cSrcweir    list1.append("b")
138cdf0e10cSrcweir    list1.append("d")
139cdf0e10cSrcweir    list1.append("e")
140cdf0e10cSrcweir    list1.append("e")
141cdf0e10cSrcweir
142cdf0e10cSrcweir    if "a" in list:
143cdf0e10cSrcweir        print "YEAH!"
144cdf0e10cSrcweir
145cdf0e10cSrcweir    a = PseudoSet(list)
146cdf0e10cSrcweir    b = PseudoSet(list1)
147cdf0e10cSrcweir
148cdf0e10cSrcweir    print "a="+str(a)
149cdf0e10cSrcweir    print "b="+str(b)
150cdf0e10cSrcweir    print "a|b=" + str(a|b)
151cdf0e10cSrcweir    print "a="+str(a)
152cdf0e10cSrcweir    print "b="+str(b)
153cdf0e10cSrcweir    print "a&b=" + str(a&b)
154cdf0e10cSrcweir    print "a="+str(a)
155cdf0e10cSrcweir    print "b="+str(b)
156cdf0e10cSrcweir    print "a-b" + str(a-b)
157cdf0e10cSrcweir
158cdf0e10cSrcweir    for key in a:
159cdf0e10cSrcweir        print key
160cdf0e10cSrcweir
161cdf0e10cSrcweirdef _testdriver_dict():
162cdf0e10cSrcweir    d = PseudoOrderedDict()
163cdf0e10cSrcweir    d["a"] = 1
164cdf0e10cSrcweir    d["b"] = 2
165cdf0e10cSrcweir    d["c"] = 3
166cdf0e10cSrcweir    d["d"] = 4
167cdf0e10cSrcweir    d["e"] = 5
168cdf0e10cSrcweir    d["f"] = 6
169cdf0e10cSrcweir
170cdf0e10cSrcweir    print "a="+str(d["a"])
171cdf0e10cSrcweir    print "e="+str(d["e"])
172cdf0e10cSrcweir    for key,value in d.iteritems():
173cdf0e10cSrcweir        print "d["+key+"]="+str(d[key])
174cdf0e10cSrcweir        print "key="+str(key)+" value="+str(value)
175cdf0e10cSrcweir
176cdf0e10cSrcweir    print "keys="+str(d.keys())
177cdf0e10cSrcweir
178cdf0e10cSrcweir#_testdriver_dict()
179