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