-
Notifications
You must be signed in to change notification settings - Fork 294
to allow "default" parameter in min and max issue 334 #459
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
Changes from 2 commits
f141395
98e8ae1
5c3f994
fbea279
ee0d6dc
4102c9a
61c9aec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,8 @@ | |||||
| - ``open`` (equivalent to io.open on Py2) | ||||||
| - ``super`` (backport of Py3's magic zero-argument super() function | ||||||
| - ``round`` (new "Banker's Rounding" behaviour from Py3) | ||||||
| - ``max`` (new max behaviour from Py3) | ||||||
| - ``min`` (new min behaviour from Py3) | ||||||
|
||||||
| - ``min`` (new min behaviour from Py3) | |
| - ``min`` (new default option from Py3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| from __builtin__ import max as _builtin_max, min as _builtin_min | ||
|
|
||
|
|
||
| def newmin(*args, **kwargs): | ||
| return new_min_max(_builtin_min, *args, **kwargs) | ||
|
|
||
|
|
||
| def newmax(*args, **kwargs): | ||
| return new_min_max(_builtin_max, *args, **kwargs) | ||
|
|
||
|
|
||
| def new_min_max(_builtin_func, *args, **kwargs): | ||
| """ | ||
| To support the argument "default" introduced in python 3.4 for min and max | ||
| :param _builtin_func: builtin min or builtin max | ||
| :param args: | ||
| :param kwargs: | ||
| :return: returns the min or max based on the arguments passed | ||
| """ | ||
|
|
||
| for key, _ in kwargs.items(): | ||
| if key not in {'key', 'default'}: | ||
| raise TypeError('Illegal argument %s', key) | ||
|
|
||
| if len(args) == 0: | ||
| raise TypeError | ||
|
|
||
| if len(args) != 1 and kwargs.get('default') is not None: | ||
| raise TypeError | ||
|
|
||
| if len(args) == 1: | ||
| try: | ||
| next(iter(args[0])) | ||
| except StopIteration: | ||
| if kwargs.get('default') is not None: | ||
| return kwargs.get('default') | ||
| else: | ||
| raise ValueError('iterable is an empty sequence') | ||
| if kwargs.get('key') is not None: | ||
| return _builtin_func(args[0], key=kwargs.get('key')) | ||
| else: | ||
| return _builtin_func(args[0]) | ||
|
|
||
| if len(args) > 1: | ||
| if kwargs.get('key') is not None: | ||
| return _builtin_func(args, key=kwargs.get('key')) | ||
| else: | ||
| return _builtin_func(args) |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |||
| from __future__ import absolute_import, division, print_function, unicode_literals | ||||
| from future.builtins import (bytes, dict, int, range, round, str, super, | ||||
| ascii, chr, hex, input, next, oct, open, pow, | ||||
| filter, map, zip) | ||||
| filter, map, zip, min, max) | ||||
|
|
||||
| from future.utils import PY3, exec_, native_str, implements_iterator | ||||
| from future.tests.base import (unittest, skip26, expectedFailurePY2, | ||||
|
|
@@ -138,6 +138,7 @@ def test_round(self): | |||
| self.assertEqual(round(0.125000001, 2), 0.13) | ||||
| self.assertEqual(round(123.5, 0), 124.0) | ||||
| self.assertEqual(round(123.5), 124) | ||||
| self.assertEqual(round(123.5), 124) | ||||
|
||||
| self.assertEqual(round(123.5), 124) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.