Skip to content

Commit f141395

Browse files
author
Sameera
committed
for issue 334
1 parent e949bae commit f141395

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/future/builtins/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# The isinstance import is no longer needed. We provide it only for
1212
# backward-compatibility with future v0.8.2. It will be removed in future v1.0.
1313
from future.builtins.misc import (ascii, chr, hex, input, isinstance, next,
14-
oct, open, pow, round, super)
14+
oct, open, pow, round, super, max, min)
1515
from future.utils import PY3
1616

1717
if PY3:
@@ -43,7 +43,7 @@
4343
__all__ = ['filter', 'map', 'zip',
4444
'ascii', 'chr', 'hex', 'input', 'next', 'oct', 'open', 'pow',
4545
'round', 'super',
46-
'bytes', 'dict', 'int', 'list', 'object', 'range', 'str',
46+
'bytes', 'dict', 'int', 'list', 'object', 'range', 'str', 'max', 'min'
4747
]
4848

4949
else:

src/future/builtins/misc.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- ``open`` (equivalent to io.open on Py2)
1414
- ``super`` (backport of Py3's magic zero-argument super() function
1515
- ``round`` (new "Banker's Rounding" behaviour from Py3)
16+
- ``max`` (new max behaviour from Py3)
17+
- ``min`` (new min behaviour from Py3)
1618
1719
``isinstance`` is also currently exported for backwards compatibility
1820
with v0.8.2, although this has been deprecated since v0.9.
@@ -59,6 +61,8 @@
5961
from future.builtins.newnext import newnext as next
6062
from future.builtins.newround import newround as round
6163
from future.builtins.newsuper import newsuper as super
64+
from future.builtins.new_min_max import newmax as max
65+
from future.builtins.new_min_max import newmin as min
6266
from future.types.newint import newint
6367

6468
_SENTINEL = object()
@@ -89,11 +93,12 @@ def pow(x, y, z=_SENTINEL):
8993
else:
9094
return _builtin_pow(x+0j, y, z)
9195

96+
9297
# ``future`` doesn't support Py3.0/3.1. If we ever did, we'd add this:
9398
# callable = __builtin__.callable
9499

95100
__all__ = ['ascii', 'chr', 'hex', 'input', 'isinstance', 'next', 'oct',
96-
'open', 'pow', 'round', 'super']
101+
'open', 'pow', 'round', 'super', 'max', 'min']
97102

98103
else:
99104
import builtins
@@ -109,6 +114,8 @@ def pow(x, y, z=_SENTINEL):
109114
pow = builtins.pow
110115
round = builtins.round
111116
super = builtins.super
117+
max = builtins.max
118+
min = builtins.min
112119

113120
__all__ = []
114121

src/future/builtins/new_min_max.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from __builtin__ import max as _builtin_max, min as _builtin_min
2+
3+
4+
def newmin(*args, **kwargs):
5+
return new_min_max(_builtin_min, *args, **kwargs)
6+
7+
8+
def newmax(*args, **kwargs):
9+
return new_min_max(_builtin_max, *args, **kwargs)
10+
11+
12+
def new_min_max(_builtin_func, *args, **kwargs):
13+
"""
14+
To support the argument "default" introduced in python 3.4 for min and max
15+
:param _builtin_func: builtin min or builtin max
16+
:param args:
17+
:param kwargs:
18+
:return: returns the min or max based on the arguments passed
19+
"""
20+
21+
for key, _ in kwargs.items():
22+
if key not in set(['key', 'default']):
23+
raise TypeError('Illegal argument %s', key)
24+
25+
if len(args) != 1 and kwargs.get('default') is not None:
26+
raise TypeError
27+
28+
if len(args) == 1:
29+
if len(args[0]) == 0:
30+
if kwargs.get('default') is not None:
31+
return kwargs.get('default')
32+
else:
33+
raise ValueError('iterable is an empty sequence')
34+
if kwargs.get('key') is not None:
35+
return _builtin_func(args[0], key=kwargs.get('key'))
36+
else:
37+
return _builtin_func(args[0])
38+
39+
if len(args) > 1:
40+
if kwargs.get('key') is not None:
41+
return _builtin_func(args, key=kwargs.get('key'))
42+
else:
43+
return _builtin_func(args)

0 commit comments

Comments
 (0)