import collections
import itertools


def partition_dict(items, key):
    """
    Given an ordered dictionary of items and a key in that dict,
    return an ordered dict of items before, the keyed item, and
    an ordered dict of items after.

    >>> od = collections.OrderedDict(zip(range(5), 'abcde'))
    >>> before, item, after = partition_dict(od, 3)
    >>> before
    OrderedDict([(0, 'a'), (1, 'b'), (2, 'c')])
    >>> item
    'd'
    >>> after
    OrderedDict([(4, 'e')])

    Like string.partition, if the key is not found in the items,
    the before will contain all items, item will be None, and
    after will be an empty iterable.

    >>> before, item, after = partition_dict(od, -1)
    >>> before
    OrderedDict([(0, 'a'), ..., (4, 'e')])
    >>> item
    >>> list(after)
    []
    """

    def unmatched(pair):
        test_key, item = pair
        return test_key != key

    items_iter = iter(items.items())
    item = items.get(key)
    left = collections.OrderedDict(itertools.takewhile(unmatched, items_iter))
    right = collections.OrderedDict(items_iter)
    return left, item, right