1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# util.py
# Copyright (C) 2005 Michael Bayer mike_mp@zzzcomputing.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
__ALL__ = ['OrderedProperties', 'OrderedDict']
import thread
class OrderedProperties(object):
def __init__(self):
self.__dict__['_list'] = []
def keys(self):
return self._list
def __iter__(self):
return iter([self[x] for x in self._list])
def __setitem__(self, key, object):
setattr(self, key, object)
def __getitem__(self, key):
return getattr(self, key)
def __setattr__(self, key, object):
if not hasattr(self, key):
self._list.append(key)
self.__dict__[key] = object
class OrderedDict(dict):
"""A Dictionary that keeps its own internal ordering"""
def __init__(self, values = None):
self.list = []
if values is not None:
for val in values:
self.update(val)
def keys(self):
return self.list
def update(self, dict):
for key in dict.keys():
self.__setitem__(key, dict[key])
def setdefault(self, key, value):
if not self.has_key(key):
self.__setitem__(key, value)
return value
else:
return self.__getitem__(key)
def values(self):
return map(lambda key: self[key], self.list)
def __iter__(self):
return iter(self.list)
def itervalues(self):
return iter([self[key] for key in self.list])
def iterkeys(self):return self.__iter__()
def iteritems(self):
return iter([(key, self[key]) for key in self.keys()])
def __setitem__(self, key, object):
if not self.has_key(key):
self.list.append(key)
dict.__setitem__(self, key, object)
def __getitem__(self, key):
return dict.__getitem__(self, key)
class ThreadLocal(object):
def __init__(self):
object.__setattr__(self, 'tdict', {})
def __getattribute__(self, key):
try:
return object.__getattribute__(self, 'tdict')["%d_%s" % (thread.get_ident(), key)]
except KeyError:
raise AttributeError(key)
def __setattr__(self, key, value):
object.__getattribute__(self, 'tdict')["%d_%s" % (thread.get_ident(), key)] = value
|