Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
f45abc97
Kaydet (Commit)
f45abc97
authored
Eyl 06, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add method to OrderedDict for repositioning keys to the ends.
üst
7b2a7710
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
51 additions
and
8 deletions
+51
-8
collections.rst
Doc/library/collections.rst
+17
-0
collections.py
Lib/collections.py
+17
-6
functools.py
Lib/functools.py
+1
-1
test_collections.py
Lib/test/test_collections.py
+13
-1
NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/collections.rst
Dosyayı görüntüle @
f45abc97
...
...
@@ -793,6 +793,23 @@ the items are returned in the order their keys were first added.
(key, value) pair. The pairs are returned in LIFO order if *last* is true
or FIFO order if false.
.. method:: move_to_end(key, last=True)
Move an existing *key* to either end of an ordered dictionary. The item
is moved to the right end if *last* is true (the default) or to the
beginning if *last* is false. Raises :exc:`KeyError` if the *key* does
not exist::
>>> d = OrderedDict.fromkeys('abcde')
>>> d.move_to_end('b')
>>> ''.join(d.keys)
'acdeb'
>>> d.move_to_end('b', 0)
>>> ''.join(d.keys)
'bacde'
.. versionadded:: 3.2
In addition to the usual mapping methods, ordered dictionaries also support
reverse iteration using :func:`reversed`.
...
...
Lib/collections.py
Dosyayı görüntüle @
f45abc97
...
...
@@ -173,18 +173,29 @@ class OrderedDict(dict, MutableMapping):
def
__del__
(
self
):
self
.
clear
()
# eliminate cyclical references
def
_renew
(
self
,
key
,
PREV
=
0
,
NEXT
=
1
):
'Fast version of self[key]=self.pop(key). Private method for internal use.'
def
move_to_end
(
self
,
key
,
last
=
True
,
PREV
=
0
,
NEXT
=
1
):
'''Move an existing element to the end (or beginning if last==False).
Raises KeyError if the element does not exist.
When last=True, acts like a fast version of self[key]=self.pop(key).
'''
link
=
self
.
__map
[
key
]
link_prev
=
link
[
PREV
]
link_next
=
link
[
NEXT
]
link_prev
[
NEXT
]
=
link_next
link_next
[
PREV
]
=
link_prev
root
=
self
.
__root
last
=
root
[
PREV
]
link
[
PREV
]
=
last
link
[
NEXT
]
=
root
last
[
NEXT
]
=
root
[
PREV
]
=
link
if
last
:
last
=
root
[
PREV
]
link
[
PREV
]
=
last
link
[
NEXT
]
=
root
last
[
NEXT
]
=
root
[
PREV
]
=
link
else
:
first
=
root
[
NEXT
]
link
[
PREV
]
=
root
link
[
NEXT
]
=
first
root
[
NEXT
]
=
first
[
PREV
]
=
link
################################################################################
...
...
Lib/functools.py
Dosyayı görüntüle @
f45abc97
...
...
@@ -127,7 +127,7 @@ def lru_cache(maxsize=100):
len
=
len
,
KeyError
=
KeyError
):
cache
=
OrderedDict
()
# ordered least recent to most recent
cache_popitem
=
cache
.
popitem
cache_renew
=
cache
.
_renew
cache_renew
=
cache
.
move_to_end
kwd_mark
=
object
()
# separate positional and keyword args
lock
=
Lock
()
...
...
Lib/test/test_collections.py
Dosyayı görüntüle @
f45abc97
...
...
@@ -973,7 +973,19 @@ class TestOrderedDict(unittest.TestCase):
od
[
'a'
]
=
1
self
.
assertEqual
(
list
(
od
.
items
()),
[(
'b'
,
2
),
(
'a'
,
1
)])
def
test_move_to_end
(
self
):
od
=
OrderedDict
.
fromkeys
(
'abcde'
)
self
.
assertEqual
(
list
(
od
),
list
(
'abcde'
))
od
.
move_to_end
(
'c'
)
self
.
assertEqual
(
list
(
od
),
list
(
'abdec'
))
od
.
move_to_end
(
'c'
,
0
)
self
.
assertEqual
(
list
(
od
),
list
(
'cabde'
))
od
.
move_to_end
(
'c'
,
0
)
self
.
assertEqual
(
list
(
od
),
list
(
'cabde'
))
od
.
move_to_end
(
'e'
)
self
.
assertEqual
(
list
(
od
),
list
(
'cabde'
))
with
self
.
assertRaises
(
KeyError
):
od
.
move_to_end
(
'x'
)
class
GeneralMappingTests
(
mapping_tests
.
BasicTestMappingProtocol
):
type2test
=
OrderedDict
...
...
Misc/NEWS
Dosyayı görüntüle @
f45abc97
...
...
@@ -13,6 +13,9 @@ Core and Builtins
Library
-------
- collections.OrderedDict now supports a new method for repositioning
keys to either end.
- Issue #9754: Similarly to assertRaises and assertRaisesRegexp, unittest
test cases now also have assertWarns and assertWarnsRegexp methods to
check that a given warning type was triggered by the code under test.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment