Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min/max with default keyword (added in Python 3.4) #338

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions six.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,56 @@ def python_2_unicode_compatible(klass):
return klass


if PY2:
def min(*iterable, **kwargs):
"""
min(...)
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value

With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
"""
default = kwargs.pop('default', None)
key = kwargs.pop('key', None)

if kwargs:
raise TypeError('min() got an unexpected keyword argument')

if default is not None and len(iterable) == 1 and len(iterable[0]) == 0:
return default

return moves.builtins.min(*iterable, key=key)


def max(*iterable, **kwargs):
"""
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value

With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
"""
default = kwargs.pop('default', None)
key = kwargs.pop('key', None)

if kwargs:
raise TypeError('max() got an unexpected keyword argument')

if default is not None and len(iterable) == 1 and len(iterable[0]) == 0:
return default

return moves.builtins.max(*iterable, key=key)
else:
min = moves.builtins.min
max = moves.builtins.max


# Complete the moves implementation.
# This code is at the end of this module to speed up module loading.
# Turn this module into a package.
Expand Down
31 changes: 27 additions & 4 deletions test_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def test_lazy():
except ImportError:
have_gdbm = False

have_ndbm = True
try:
import dbm.ndbm
except ImportError:
have_ndbm = False

@pytest.mark.parametrize("item_name",
[item.name for item in six._moved_attributes])
def test_move_items(item_name):
Expand All @@ -124,11 +130,12 @@ def test_move_items(item_name):
except ImportError:
if item_name == "winreg" and not sys.platform.startswith("win"):
pytest.skip("Windows only module")
if item_name.startswith("tkinter"):
if not have_tkinter:
pytest.skip("requires tkinter")
if item_name.startswith("tkinter") and not have_tkinter:
pytest.skip("requires tkinter")
if item_name.startswith("dbm_gnu") and not have_gdbm:
pytest.skip("requires gdbm")
if item_name == 'dbm_ndbm' and not have_ndbm:
pytest.skip("requires ndbm")
raise
assert item_name in dir(six.moves)

Expand Down Expand Up @@ -220,7 +227,7 @@ def test_map():

def test_getoutput():
from six.moves import getoutput
output = getoutput('echo "foo"')
output = getoutput('echo foo')
assert output == 'foo'


Expand Down Expand Up @@ -1039,3 +1046,19 @@ def test_ensure_text(self):
assert converted_unicode == self.UNICODE_EMOJI and isinstance(converted_unicode, str)
# PY3: bytes -> str
assert converted_binary == self.UNICODE_EMOJI and isinstance(converted_unicode, str)


@pytest.mark.parametrize('func_name', ['min', 'max'])
def test_min_max(self, func_name):
func = six.__builtins__[func_name]
six_func = getattr(six, func_name)

assert func([1,2]) == six_func([1,2])
assert func(1, 2) == six_func(1, 2)

def key(l):
return l[1]

assert func([9, 1], [3, 4], key=key) == six_func([9, 1], [3, 4], key=key)

assert six_func([], default=12) == 12