Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a82aa55b
Commit
a82aa55b
authored
Apr 24, 2011
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor clean-ups to docstrings, comments, and var names.
parent
6803dc28
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
16 deletions
+21
-16
Lib/collections/__init__.py
Lib/collections/__init__.py
+21
-16
No files found.
Lib/collections/__init__.py
View file @
a82aa55b
...
...
@@ -28,20 +28,20 @@ class OrderedDict(dict):
# An inherited dict maps keys to values.
# The inherited dict provides __getitem__, __len__, __contains__, and get.
# The remaining methods are order-aware.
# Big-O running times for all methods are the same as
for
regular dictionaries.
# Big-O running times for all methods are the same as regular dictionaries.
# The internal self.__map dict
ionary
maps keys to links in a doubly linked list.
# The internal self.__map dict maps keys to links in a doubly linked list.
# The circular doubly linked list starts and ends with a sentinel element.
# The sentinel element never gets deleted (this simplifies the algorithm).
# The sentinel is
stored
in self.__hardroot with a weakref proxy in self.__root.
# The sentinel is in self.__hardroot with a weakref proxy in self.__root.
# The prev/next links are weakref proxies (to prevent circular references).
# Individual links are kept alive by the hard reference in self.__map.
# Those hard references disappear when a key is deleted from an OrderedDict.
def
__init__
(
self
,
*
args
,
**
kwds
):
'''Initialize an ordered dictionary.
Signature is the same as for
regular dictionaries, but keyword arguments are not recommended
because
their insertion order is arbitrary.
'''Initialize an ordered dictionary.
The signature is the same as
regular dictionaries, but keyword arguments are not recommended
because
their insertion order is arbitrary.
'''
if
len
(
args
)
>
1
:
...
...
@@ -58,8 +58,8 @@ class OrderedDict(dict):
def
__setitem__
(
self
,
key
,
value
,
dict_setitem
=
dict
.
__setitem__
,
proxy
=
_proxy
,
Link
=
_Link
):
'od.__setitem__(i, y) <==> od[i]=y'
# Setting a new item creates a new link
which goes at the end of the linked
#
list,
and the inherited dictionary is updated with the new key/value pair.
# Setting a new item creates a new link
at the end of the linked list,
# and the inherited dictionary is updated with the new key/value pair.
if
key
not
in
self
:
self
.
__map
[
key
]
=
link
=
Link
()
root
=
self
.
__root
...
...
@@ -71,8 +71,8 @@ class OrderedDict(dict):
def
__delitem__
(
self
,
key
,
dict_delitem
=
dict
.
__delitem__
):
'od.__delitem__(y) <==> del od[y]'
# Deleting an existing item uses self.__map to find the link which
i
s
#
then
removed by updating the links in the predecessor and successor nodes.
# Deleting an existing item uses self.__map to find the link which
get
s
# removed by updating the links in the predecessor and successor nodes.
dict_delitem
(
self
,
key
)
link
=
self
.
__map
.
pop
(
key
)
link_prev
=
link
.
prev
...
...
@@ -170,6 +170,11 @@ class OrderedDict(dict):
__marker
=
object
()
def
pop
(
self
,
key
,
default
=
__marker
):
'''od.pop(k[,d]) -> v, remove specified key and return the corresponding
value. If key is not found, d is returned if given, otherwise KeyError
is raised.
'''
if
key
in
self
:
result
=
self
[
key
]
del
self
[
key
]
...
...
@@ -179,7 +184,7 @@ class OrderedDict(dict):
return
default
def
setdefault
(
self
,
key
,
default
=
None
):
'
OD.setdefault(k[,d]) -> OD.get(k,d), also set OD[k]=d if k not in OD
'
'
od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od
'
if
key
in
self
:
return
self
[
key
]
self
[
key
]
=
default
...
...
@@ -208,14 +213,14 @@ class OrderedDict(dict):
@
classmethod
def
fromkeys
(
cls
,
iterable
,
value
=
None
):
'''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
and values equal to v (which defaults to None)
.
'''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
.
If not specified, the value defaults to None
.
'''
d
=
cls
()
self
=
cls
()
for
key
in
iterable
:
d
[
key
]
=
value
return
d
self
[
key
]
=
value
return
self
def
__eq__
(
self
,
other
):
'''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
...
...
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