summaryrefslogtreecommitdiff
path: root/compat.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2016-05-02 08:50:15 +0200
committerAnthon van der Neut <anthon@mnt.org>2016-05-02 08:50:15 +0200
commit4fa9ad23e69dcea31fe1b3dc15dc3cc1510b797c (patch)
treecef93d3c5c2c9e40f58360df87958e3d8b8175fa /compat.py
parentad69246c90b178e99a752769ee4ac70dd4dedb26 (diff)
downloadruamel.yaml-4fa9ad23e69dcea31fe1b3dc15dc3cc1510b797c.tar.gz
allow insert in CommentedMap (Py3) and add comment0.11.11
The insert() would only be available for CommentedMap on Py2 (thanks to ruamel.ordereddict). The standard library lacks this functionality. The method was added in the compatibility layer, and on CommentedMap the optional comment parameter allows directly setting an end-of-line comment on the inserted key.
Diffstat (limited to 'compat.py')
-rw-r--r--compat.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/compat.py b/compat.py
index 8120a4e..abd10ac 100644
--- a/compat.py
+++ b/compat.py
@@ -18,7 +18,20 @@ except:
# to get the right name import ... as ordereddict doesn't do that
class ordereddict(OrderedDict):
- pass
+ if not hasattr(OrderedDict, 'insert'):
+ def insert(self, pos, key, value):
+ if pos >= len(self):
+ self[key] = value
+ return
+ od = ordereddict()
+ od.update(self)
+ for k in od:
+ del self[k]
+ for index, old_key in enumerate(od):
+ if pos == index:
+ self[key] = value
+ self[old_key] = od[old_key]
+
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3