You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2014-2015 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. def _sum(l):
  5. """ Same as stdlib sum but returns None instead of 0
  6. in case of empty sequence.
  7. >>> sum([1])
  8. 1
  9. >>> _sum([1])
  10. 1
  11. >>> sum([1, 2])
  12. 3
  13. >>> _sum([1, 2])
  14. 3
  15. >>> sum([])
  16. 0
  17. >>> _sum([])
  18. """
  19. if not l:
  20. return None
  21. return sum(l)
  22. def _avg(l):
  23. """ Arithmetic mean of a sequence. Returns None in case of empty sequence.
  24. >>> _avg([1])
  25. 1.0
  26. >>> _avg([1, 2])
  27. 1.5
  28. >>> _avg([])
  29. """
  30. if not l:
  31. return None
  32. return sum(l) / float(len(l))
  33. def _min(*args):
  34. """ Same as stdlib min but returns None instead of exception
  35. in case of empty sequence.
  36. >>> min(1, 2)
  37. 1
  38. >>> _min(1, 2)
  39. 1
  40. >>> min([1, 2])
  41. 1
  42. >>> _min([1, 2])
  43. 1
  44. >>> min(1)
  45. Traceback (most recent call last):
  46. File "<stdin>", line 1, in ?
  47. TypeError: 'int' object is not iterable
  48. >>> _min(1)
  49. Traceback (most recent call last):
  50. File "<stdin>", line 1, in ?
  51. TypeError: 'int' object is not iterable
  52. >>> min([1])
  53. 1
  54. >>> _min([1])
  55. 1
  56. >>> min()
  57. Traceback (most recent call last):
  58. File "<stdin>", line 1, in ?
  59. TypeError: min expected 1 arguments, got 0
  60. >>> _min()
  61. Traceback (most recent call last):
  62. File "<stdin>", line 1, in ?
  63. TypeError: min expected 1 arguments, got 0
  64. >>> min([])
  65. Traceback (most recent call last):
  66. File "<stdin>", line 1, in ?
  67. ValueError: min() arg is an empty sequence
  68. >>> _min([])
  69. """
  70. if len(args) == 1 and not args[0]:
  71. return None
  72. return min(*args)
  73. def _max(*args):
  74. """ Same as stdlib max but returns None instead of exception
  75. in case of empty sequence.
  76. >>> max(1, 2)
  77. 2
  78. >>> _max(1, 2)
  79. 2
  80. >>> max([1, 2])
  81. 2
  82. >>> _max([1, 2])
  83. 2
  84. >>> max(1)
  85. Traceback (most recent call last):
  86. File "<stdin>", line 1, in ?
  87. TypeError: 'int' object is not iterable
  88. >>> _max(1)
  89. Traceback (most recent call last):
  90. File "<stdin>", line 1, in ?
  91. TypeError: 'int' object is not iterable
  92. >>> max([1])
  93. 1
  94. >>> _max([1])
  95. 1
  96. >>> max()
  97. Traceback (most recent call last):
  98. File "<stdin>", line 1, in ?
  99. TypeError: max expected 1 arguments, got 0
  100. >>> _max()
  101. Traceback (most recent call last):
  102. File "<stdin>", line 1, in ?
  103. TypeError: max expected 1 arguments, got 0
  104. >>> max([])
  105. Traceback (most recent call last):
  106. File "<stdin>", line 1, in ?
  107. ValueError: max() arg is an empty sequence
  108. >>> _max([])
  109. """
  110. if len(args) == 1 and not args[0]:
  111. return None
  112. return max(*args)
  113. if __name__ == "__main__":
  114. import doctest
  115. doctest.testmod()