datatracker/ietf/utils/ordereddict.py

22 lines
681 B
Python

# Copyright The IETF Trust 2014-2019, All Rights Reserved
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
def insert_after_in_ordered_dict(dictionary, key, value, after):
"""There's no "insert" in ordered dict so simulate it instead by
re-adding entries. Obviously that's not ideal, but for small dicts the
overhead is negligible."""
dictionary[key] = value
reorder = False
l = list(dictionary.items()) # don't mutate the dict while looping
for k, v in l:
if reorder and k != key:
del dictionary[k]
dictionary[k] = v
if k == after:
reorder = True