comparison planemo/lib/python3.7/site-packages/future/builtins/new_min_max.py @ 0:d30785e31577 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:18:57 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d30785e31577
1 import itertools
2
3 from future import utils
4 if utils.PY2:
5 from __builtin__ import max as _builtin_max, min as _builtin_min
6 else:
7 from builtins import max as _builtin_max, min as _builtin_min
8
9 _SENTINEL = object()
10
11
12 def newmin(*args, **kwargs):
13 return new_min_max(_builtin_min, *args, **kwargs)
14
15
16 def newmax(*args, **kwargs):
17 return new_min_max(_builtin_max, *args, **kwargs)
18
19
20 def new_min_max(_builtin_func, *args, **kwargs):
21 """
22 To support the argument "default" introduced in python 3.4 for min and max
23 :param _builtin_func: builtin min or builtin max
24 :param args:
25 :param kwargs:
26 :return: returns the min or max based on the arguments passed
27 """
28
29 for key, _ in kwargs.items():
30 if key not in set(['key', 'default']):
31 raise TypeError('Illegal argument %s', key)
32
33 if len(args) == 0:
34 raise TypeError
35
36 if len(args) != 1 and kwargs.get('default', _SENTINEL) is not _SENTINEL:
37 raise TypeError
38
39 if len(args) == 1:
40 iterator = iter(args[0])
41 try:
42 first = next(iterator)
43 except StopIteration:
44 if kwargs.get('default', _SENTINEL) is not _SENTINEL:
45 return kwargs.get('default')
46 else:
47 raise ValueError('{}() arg is an empty sequence'.format(_builtin_func.__name__))
48 else:
49 iterator = itertools.chain([first], iterator)
50 if kwargs.get('key') is not None:
51 return _builtin_func(iterator, key=kwargs.get('key'))
52 else:
53 return _builtin_func(iterator)
54
55 if len(args) > 1:
56 if kwargs.get('key') is not None:
57 return _builtin_func(args, key=kwargs.get('key'))
58 else:
59 return _builtin_func(args)