a9d055f5b83acd80a4ba2f987edd1d490ccf284b
4 class OrderedRawConfigParser( ConfigParser
.RawConfigParser
):
6 Overload standard Class ConfigParser.RawConfigParser
9 """Write an .ini-format representation of the configuration state."""
11 fp
.write("[%s]\n" % DEFAULTSECT
)
12 for key
in sorted( self
._defaults
):
13 fp
.write( "%s = %s\n" %
(key
, str( self
._defaults
[ key
]
14 ).replace('\n', '\n\t')) )
16 for section
in self
._sections
:
17 fp
.write("[%s]\n" % section
)
18 for key
in sorted( self
._sections
[section
] ):
20 fp
.write("%s = %s\n" %
21 (key
, str( self
._sections
[section
][ key
]
22 ).replace('\n', '\n\t')))
32 """An injective (one-to-one) python dict. Ensures that each key maps
33 to a unique value, and each value maps back to that same key.
35 Code mostly taken from here:
36 http://code.activestate.com/recipes/576968-flipdict-python-dict-that-also-maintains-a-one-to-/
39 def __init__(self
, *args
, **kw
):
40 self
._flip
= dict.__new__(self
.__class__
)
41 setattr(self
._flip
, "_flip", self
)
42 for key
, val
in dict(*args
, **kw
).iteritems():
47 """The inverse mapping."""
51 return "%s(%r)" %
(self
.__class__
.__name__
, dict(self
))
56 return self
.__class__(self
)
59 def fromkeys(cls
, keys
, value
=None):
60 return cls(dict.fromkeys(keys
, value
))
62 def __setitem__(self
, key
, val
):
63 k
= self
._flip
.get(val
, _NOTFOUND
)
64 if not (k
is _NOTFOUND
or k
==key
):
65 raise KeyError('(key,val) would erase mapping for value %r' % val
)
67 v
= self
.get(key
, _NOTFOUND
)
68 if v
is not _NOTFOUND
:
69 dict.__delitem__(self
._flip
, v
)
71 dict.__setitem__(self
, key
, val
)
72 dict.__setitem__(self
._flip
, val
, key
)
74 def setdefault(self
, key
, default
= None):
75 # Copied from python's UserDict.DictMixin code.
82 def update(self
, other
= None, **kwargs
):
83 # Copied from python's UserDict.DictMixin code.
84 # Make progressively weaker assumptions about "other"
87 elif hasattr(other
, 'iteritems'): # iteritems saves memory and lookups
88 for k
, v
in other
.iteritems():
90 elif hasattr(other
, 'keys'):
91 for k
in other
.keys():
99 def __delitem__(self
, key
):
100 val
= dict.pop(self
, key
)
101 dict.__delitem__(self
._flip
, val
)
103 def pop(self
, key
, *args
):
104 val
= dict.pop(self
, key
, *args
)
105 dict.__delitem__(self
._flip
, val
)
109 key
, val
= dict.popitem(self
)
110 dict.__delitem__(self
._flip
, val
)
115 dict.clear(self
._flip
)