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
d4cb56d4
Commit
d4cb56d4
authored
Jan 30, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert some custom sort comparison functions to equivalent key functions.
parent
fd66e51c
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
50 additions
and
20 deletions
+50
-20
Lib/bsddb/dbtables.py
Lib/bsddb/dbtables.py
+10
-1
Lib/bsddb/test/test_compare.py
Lib/bsddb/test/test_compare.py
+11
-1
Lib/ctypes/util.py
Lib/ctypes/util.py
+1
-1
Lib/idlelib/MultiCall.py
Lib/idlelib/MultiCall.py
+1
-1
Lib/idlelib/TreeWidget.py
Lib/idlelib/TreeWidget.py
+1
-1
Lib/pyclbr.py
Lib/pyclbr.py
+1
-2
Lib/pydoc.py
Lib/pydoc.py
+1
-4
Lib/tarfile.py
Lib/tarfile.py
+1
-1
Lib/unittest.py
Lib/unittest.py
+10
-1
Tools/pynche/ColorDB.py
Tools/pynche/ColorDB.py
+1
-4
Tools/scripts/finddiv.py
Tools/scripts/finddiv.py
+1
-1
Tools/unicode/makeunicodedata.py
Tools/unicode/makeunicodedata.py
+11
-2
No files found.
Lib/bsddb/dbtables.py
View file @
d4cb56d4
...
@@ -88,6 +88,15 @@ class LikeCond(Cond):
...
@@ -88,6 +88,15 @@ class LikeCond(Cond):
def
__call__
(
self
,
s
):
def
__call__
(
self
,
s
):
return
self
.
re
.
match
(
s
.
decode
(
self
.
encoding
))
return
self
.
re
.
match
(
s
.
decode
(
self
.
encoding
))
def
CmpToKey
(
mycmp
):
'Convert a cmp= function into a key= function'
class
K
(
object
):
def
__init__
(
self
,
obj
,
*
args
):
self
.
obj
=
obj
def
__lt__
(
self
,
other
):
return
mycmp
(
self
.
obj
,
other
.
obj
)
==
-
1
return
K
#
#
# keys used to store database metadata
# keys used to store database metadata
#
#
...
@@ -587,7 +596,7 @@ class bsdTableDB :
...
@@ -587,7 +596,7 @@ class bsdTableDB :
return
0
return
0
conditionlist
=
list
(
conditions
.
items
())
conditionlist
=
list
(
conditions
.
items
())
conditionlist
.
sort
(
cmp_conditions
)
conditionlist
.
sort
(
key
=
CmpToKey
(
cmp_conditions
)
)
# Apply conditions to column data to find what we want
# Apply conditions to column data to find what we want
cur
=
self
.
db
.
cursor
()
cur
=
self
.
db
.
cursor
()
...
...
Lib/bsddb/test/test_compare.py
View file @
d4cb56d4
...
@@ -32,10 +32,20 @@ _expected_lexical_test_data = [s.encode('ascii') for s in
...
@@ -32,10 +32,20 @@ _expected_lexical_test_data = [s.encode('ascii') for s in
_expected_lowercase_test_data
=
[
s
.
encode
(
'ascii'
)
for
s
in
_expected_lowercase_test_data
=
[
s
.
encode
(
'ascii'
)
for
s
in
(
''
,
'a'
,
'aaa'
,
'b'
,
'c'
,
'CC'
,
'cccce'
,
'ccccf'
,
'CCCP'
)]
(
''
,
'a'
,
'aaa'
,
'b'
,
'c'
,
'CC'
,
'cccce'
,
'ccccf'
,
'CCCP'
)]
def
CmpToKey
(
mycmp
):
'Convert a cmp= function into a key= function'
class
K
(
object
):
def
__init__
(
self
,
obj
,
*
args
):
self
.
obj
=
obj
def
__lt__
(
self
,
other
):
return
mycmp
(
self
.
obj
,
other
.
obj
)
==
-
1
return
K
class
ComparatorTests
(
unittest
.
TestCase
):
class
ComparatorTests
(
unittest
.
TestCase
):
def
comparator_test_helper
(
self
,
comparator
,
expected_data
):
def
comparator_test_helper
(
self
,
comparator
,
expected_data
):
data
=
expected_data
[:]
data
=
expected_data
[:]
data
.
sort
(
comparator
)
data
.
sort
(
key
=
CmpToKey
(
comparator
)
)
self
.
failUnless
(
data
==
expected_data
,
self
.
failUnless
(
data
==
expected_data
,
"comparator `%s' is not right: %s vs. %s"
"comparator `%s' is not right: %s vs. %s"
%
(
comparator
,
expected_data
,
data
))
%
(
comparator
,
expected_data
,
data
))
...
...
Lib/ctypes/util.py
View file @
d4cb56d4
...
@@ -123,7 +123,7 @@ elif os.name == "posix":
...
@@ -123,7 +123,7 @@ elif os.name == "posix":
res = re.findall(expr, data)
res = re.findall(expr, data)
if not res:
if not res:
return _get_soname(_findLib_gcc(name))
return _get_soname(_findLib_gcc(name))
res.sort(
cmp= lambda x,y: cmp(_num_version(x), _num_version(y))
)
res.sort(
key=_num_version
)
return res[-1]
return res[-1]
else:
else:
...
...
Lib/idlelib/MultiCall.py
View file @
d4cb56d4
...
@@ -125,7 +125,7 @@ def expand_substates(states):
...
@@ -125,7 +125,7 @@ def expand_substates(states):
statelist
=
[]
statelist
=
[]
for
state
in
states
:
for
state
in
states
:
substates
=
list
(
set
(
state
&
x
for
x
in
states
))
substates
=
list
(
set
(
state
&
x
for
x
in
states
))
substates
.
sort
(
lambda
a
,
b
:
nbits
(
b
)
-
nbits
(
a
)
)
substates
.
sort
(
key
=
nbits
,
reverse
=
True
)
statelist
.
append
(
substates
)
statelist
.
append
(
substates
)
return
statelist
return
statelist
...
...
Lib/idlelib/TreeWidget.py
View file @
d4cb56d4
...
@@ -398,7 +398,7 @@ class FileTreeItem(TreeItem):
...
@@ -398,7 +398,7 @@ class FileTreeItem(TreeItem):
names
=
os
.
listdir
(
self
.
path
)
names
=
os
.
listdir
(
self
.
path
)
except
os
.
error
:
except
os
.
error
:
return
[]
return
[]
names
.
sort
(
lambda
a
,
b
:
cmp
(
os
.
path
.
normcase
(
a
),
os
.
path
.
normcase
(
b
))
)
names
.
sort
(
key
=
os
.
path
.
normcase
)
sublist
=
[]
sublist
=
[]
for
name
in
names
:
for
name
in
names
:
item
=
FileTreeItem
(
os
.
path
.
join
(
self
.
path
,
name
))
item
=
FileTreeItem
(
os
.
path
.
join
(
self
.
path
,
name
))
...
...
Lib/pyclbr.py
View file @
d4cb56d4
...
@@ -324,8 +324,7 @@ def _main():
...
@@ -324,8 +324,7 @@ def _main():
path
=
[]
path
=
[]
dict
=
readmodule_ex
(
mod
,
path
)
dict
=
readmodule_ex
(
mod
,
path
)
objs
=
dict
.
values
()
objs
=
dict
.
values
()
objs
.
sort
(
lambda
a
,
b
:
cmp
(
getattr
(
a
,
'lineno'
,
0
),
objs
.
sort
(
key
=
lambda
a
:
getattr
(
a
,
'lineno'
,
0
))
getattr
(
b
,
'lineno'
,
0
)))
for
obj
in
objs
:
for
obj
in
objs
:
if
isinstance
(
obj
,
Class
):
if
isinstance
(
obj
,
Class
):
print
(
"class"
,
obj
.
name
,
obj
.
super
,
obj
.
lineno
)
print
(
"class"
,
obj
.
name
,
obj
.
super
,
obj
.
lineno
)
...
...
Lib/pydoc.py
View file @
d4cb56d4
...
@@ -797,10 +797,7 @@ class HTMLDoc(Doc):
...
@@ -797,10 +797,7 @@ class HTMLDoc(Doc):
tag
+=
':<br>
\
n
'
tag
+=
':<br>
\
n
'
# Sort attrs by name.
# Sort attrs by name.
try
:
attrs
.
sort
(
key
=
lambda
t
:
t
[
0
])
attrs
.
sort
(
key
=
lambda
t
:
t
[
0
])
except
TypeError
:
attrs
.
sort
(
lambda
t1
,
t2
:
cmp
(
t1
[
0
],
t2
[
0
]))
# 2.3 compat
# Pump out the attrs, segregated by kind.
# Pump out the attrs, segregated by kind.
attrs
=
spill
(
'Methods %s'
%
tag
,
attrs
,
attrs
=
spill
(
'Methods %s'
%
tag
,
attrs
,
...
...
Lib/tarfile.py
View file @
d4cb56d4
...
@@ -2016,7 +2016,7 @@ class TarFile(object):
...
@@ -2016,7 +2016,7 @@ class TarFile(object):
self.extract(tarinfo, path)
self.extract(tarinfo, path)
# Reverse sort directories.
# Reverse sort directories.
directories.sort(
lambda a, b: cmp(a.name, b.name)
)
directories.sort(
key=lambda a: a.name
)
directories.reverse()
directories.reverse()
# Set correct owner, mtime and filemode on directories.
# Set correct owner, mtime and filemode on directories.
...
...
Lib/unittest.py
View file @
d4cb56d4
...
@@ -504,6 +504,15 @@ class FunctionTestCase(TestCase):
...
@@ -504,6 +504,15 @@ class FunctionTestCase(TestCase):
# Locating and loading tests
# Locating and loading tests
##############################################################################
##############################################################################
def
CmpToKey
(
mycmp
):
'Convert a cmp= function into a key= function'
class
K
(
object
):
def
__init__
(
self
,
obj
,
*
args
):
self
.
obj
=
obj
def
__lt__
(
self
,
other
):
return
mycmp
(
self
.
obj
,
other
.
obj
)
==
-
1
return
K
class
TestLoader
:
class
TestLoader
:
"""This class is responsible for loading tests according to various
"""This class is responsible for loading tests according to various
criteria and returning them wrapped in a TestSuite
criteria and returning them wrapped in a TestSuite
...
@@ -598,7 +607,7 @@ class TestLoader:
...
@@ -598,7 +607,7 @@ class TestLoader:
and
hasattr
(
getattr
(
testCaseClass
,
attrname
),
'__call__'
)
and
hasattr
(
getattr
(
testCaseClass
,
attrname
),
'__call__'
)
testFnNames
=
list
(
filter
(
isTestMethod
,
dir
(
testCaseClass
)))
testFnNames
=
list
(
filter
(
isTestMethod
,
dir
(
testCaseClass
)))
if
self
.
sortTestMethodsUsing
:
if
self
.
sortTestMethodsUsing
:
testFnNames
.
sort
(
self
.
sortTestMethodsUsing
)
testFnNames
.
sort
(
key
=
CmpToKey
(
self
.
sortTestMethodsUsing
)
)
return
testFnNames
return
testFnNames
...
...
Tools/pynche/ColorDB.py
View file @
d4cb56d4
...
@@ -122,10 +122,7 @@ class ColorDB:
...
@@ -122,10 +122,7 @@ class ColorDB:
self
.
__allnames
=
[]
self
.
__allnames
=
[]
for
name
,
aliases
in
self
.
__byrgb
.
values
():
for
name
,
aliases
in
self
.
__byrgb
.
values
():
self
.
__allnames
.
append
(
name
)
self
.
__allnames
.
append
(
name
)
# sort irregardless of case
self
.
__allnames
.
sort
(
key
=
unicode
.
lower
)
def
nocase_cmp
(
n1
,
n2
):
return
cmp
(
n1
.
lower
(),
n2
.
lower
())
self
.
__allnames
.
sort
(
nocase_cmp
)
return
self
.
__allnames
return
self
.
__allnames
def
aliases_of
(
self
,
red
,
green
,
blue
):
def
aliases_of
(
self
,
red
,
green
,
blue
):
...
...
Tools/scripts/finddiv.py
View file @
d4cb56d4
...
@@ -78,7 +78,7 @@ def processdir(dir, listnames):
...
@@ -78,7 +78,7 @@ def processdir(dir, listnames):
fn
=
os
.
path
.
join
(
dir
,
name
)
fn
=
os
.
path
.
join
(
dir
,
name
)
if
os
.
path
.
normcase
(
fn
).
endswith
(
".py"
)
or
os
.
path
.
isdir
(
fn
):
if
os
.
path
.
normcase
(
fn
).
endswith
(
".py"
)
or
os
.
path
.
isdir
(
fn
):
files
.
append
(
fn
)
files
.
append
(
fn
)
files
.
sort
(
lambda
a
,
b
:
cmp
(
os
.
path
.
normcase
(
a
),
os
.
path
.
normcase
(
b
))
)
files
.
sort
(
key
=
os
.
path
.
normcase
)
exit
=
None
exit
=
None
for
fn
in
files
:
for
fn
in
files
:
x
=
process
(
fn
,
listnames
)
x
=
process
(
fn
,
listnames
)
...
...
Tools/unicode/makeunicodedata.py
View file @
d4cb56d4
...
@@ -441,6 +441,15 @@ def makeunicodetype(unicode, trace):
...
@@ -441,6 +441,15 @@ def makeunicodetype(unicode, trace):
# --------------------------------------------------------------------
# --------------------------------------------------------------------
# unicode name database
# unicode name database
def
CmpToKey
(
mycmp
):
'Convert a cmp= function into a key= function'
class
K
(
object
):
def
__init__
(
self
,
obj
,
*
args
):
self
.
obj
=
obj
def
__lt__
(
self
,
other
):
return
mycmp
(
self
.
obj
,
other
.
obj
)
==
-
1
return
K
def
makeunicodename
(
unicode
,
trace
):
def
makeunicodename
(
unicode
,
trace
):
FILE
=
"Modules/unicodename_db.h"
FILE
=
"Modules/unicodename_db.h"
...
@@ -490,7 +499,7 @@ def makeunicodename(unicode, trace):
...
@@ -490,7 +499,7 @@ def makeunicodename(unicode, trace):
if
r
:
if
r
:
return
r
return
r
return
cmp
(
aword
,
bword
)
return
cmp
(
aword
,
bword
)
wordlist
.
sort
(
cmpwords
)
wordlist
.
sort
(
key
=
CmpToKey
(
cmpwords
)
)
# figure out how many phrasebook escapes we need
# figure out how many phrasebook escapes we need
escapes
=
0
escapes
=
0
...
@@ -514,7 +523,7 @@ def makeunicodename(unicode, trace):
...
@@ -514,7 +523,7 @@ def makeunicodename(unicode, trace):
# length (to maximize overlap)
# length (to maximize overlap)
wordlist
,
wordtail
=
wordlist
[:
short
],
wordlist
[
short
:]
wordlist
,
wordtail
=
wordlist
[:
short
],
wordlist
[
short
:]
wordtail
.
sort
(
lambda
a
,
b
:
len
(
b
[
0
])
-
len
(
a
[
0
])
)
wordtail
.
sort
(
key
=
lambda
a
:
a
[
0
],
reverse
=
True
)
wordlist
.
extend
(
wordtail
)
wordlist
.
extend
(
wordtail
)
# generate lexicon from words
# generate lexicon from words
...
...
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