Skip to content

Commit

Permalink
Remove use of OrderedDict in partition_dict; fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jul 12, 2023
1 parent d08d74a commit d19e0d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
22 changes: 11 additions & 11 deletions jaraco/itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,29 +1128,29 @@ def _swap_on_miss(partition_result):

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.
Given a dictionary of items and a key in that dict,
return another dict of items before, the keyed item, and
the dict of items after.
>>> od = collections.OrderedDict(zip(range(5), 'abcde'))
>>> od = dict(zip(range(5), 'abcde'))
>>> before, item, after = partition_dict(od, 3)
>>> before
OrderedDict([(0, 'a'), (1, 'b'), (2, 'c')])
{0: 'a', 1: 'b', 2: 'c'}
>>> item
'd'
>>> after
OrderedDict([(4, 'e')])
{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')])
{0: 'a', ..., 4: 'e'}
>>> item
>>> list(after)
[]
>>> after
{}
"""

def unmatched(pair):
Expand All @@ -1159,8 +1159,8 @@ def unmatched(pair):

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


Expand Down
1 change: 1 addition & 0 deletions newsfragments/17.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove use of ``OrderedDict`` in ``partition_dict``.

0 comments on commit d19e0d0

Please sign in to comment.