This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients Ramchandra Apte, eric.snow, pitrou, rhettinger, serhiy.storchaka
Date 2013-02-02.17:38:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1359826724.85.0.257742832191.issue17100@psf.upfronthosting.co.za>
In-reply-to
Content
Please don't rush to make patches.  It isn't even clear that this is a good idea.

AFAICT, none of the many extant implementation of ordered dictionaries in any language currently implement a rotate operation.

FWIW, the iter() and move_to_end() methods are likely your best bet for implementing a rotate function using the current API and without doing any ordered dictionary key lookups:

>>> from collections import OrderedDict
>>> from itertools import islice
>>> 
>>> def rotate(d, n):
    # quick demo
    if n > 0:
        for k in list(islice(d, n)):
            d.move_to_end(k)
    elif n < 0:
        for k in list(islice(reversed(d), -n)):
            d.move_to_end(k, 0)
           
>>> d = collections.OrderedDict.fromkeys('abcdefghijk')
>>> list(d)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
>>> rotate(d, 3)
>>> list(d)
['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'a', 'b', 'c']
>>> rotate(d, -3)
>>> list(d)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
History
Date User Action Args
2013-02-02 17:44:53rhettingerunlinkissue17100 messages
2013-02-02 17:38:44rhettingersetrecipients: + rhettinger, pitrou, eric.snow, Ramchandra Apte, serhiy.storchaka
2013-02-02 17:38:44rhettingersetmessageid: <1359826724.85.0.257742832191.issue17100@psf.upfronthosting.co.za>
2013-02-02 17:38:44rhettingerlinkissue17100 messages
2013-02-02 17:38:44rhettingercreate