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.

149 lines
3.7 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # mis_builder module for Odoo, Management Information System Builder
  5. # Copyright (C) 2014-2015 ACSONE SA/NV (<http://acsone.eu>)
  6. #
  7. # This file is a part of mis_builder
  8. #
  9. # mis_builder is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License v3 or later
  11. # as published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # mis_builder is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License v3 or later for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # v3 or later along with this program.
  21. # If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. def _sum(l):
  25. """ Same as stdlib sum but returns None instead of 0
  26. in case of empty sequence.
  27. >>> sum([1])
  28. 1
  29. >>> _sum([1])
  30. 1
  31. >>> sum([1, 2])
  32. 3
  33. >>> _sum([1, 2])
  34. 3
  35. >>> sum([])
  36. 0
  37. >>> _sum([])
  38. """
  39. if not l:
  40. return None
  41. return sum(l)
  42. def _avg(l):
  43. """ Arithmetic mean of a sequence. Returns None in case of empty sequence.
  44. >>> _avg([1])
  45. 1.0
  46. >>> _avg([1, 2])
  47. 1.5
  48. >>> _avg([])
  49. """
  50. if not l:
  51. return None
  52. return sum(l) / float(len(l))
  53. def _min(*args):
  54. """ Same as stdlib min but returns None instead of exception
  55. in case of empty sequence.
  56. >>> min(1, 2)
  57. 1
  58. >>> _min(1, 2)
  59. 1
  60. >>> min([1, 2])
  61. 1
  62. >>> _min([1, 2])
  63. 1
  64. >>> min(1)
  65. Traceback (most recent call last):
  66. File "<stdin>", line 1, in ?
  67. TypeError: 'int' object is not iterable
  68. >>> _min(1)
  69. Traceback (most recent call last):
  70. File "<stdin>", line 1, in ?
  71. TypeError: 'int' object is not iterable
  72. >>> min([1])
  73. 1
  74. >>> _min([1])
  75. 1
  76. >>> min()
  77. Traceback (most recent call last):
  78. File "<stdin>", line 1, in ?
  79. TypeError: min expected 1 arguments, got 0
  80. >>> _min()
  81. Traceback (most recent call last):
  82. File "<stdin>", line 1, in ?
  83. TypeError: min expected 1 arguments, got 0
  84. >>> min([])
  85. Traceback (most recent call last):
  86. File "<stdin>", line 1, in ?
  87. ValueError: min() arg is an empty sequence
  88. >>> _min([])
  89. """
  90. if len(args) == 1 and not args[0]:
  91. return None
  92. return min(*args)
  93. def _max(*args):
  94. """ Same as stdlib max but returns None instead of exception
  95. in case of empty sequence.
  96. >>> max(1, 2)
  97. 2
  98. >>> _max(1, 2)
  99. 2
  100. >>> max([1, 2])
  101. 2
  102. >>> _max([1, 2])
  103. 2
  104. >>> max(1)
  105. Traceback (most recent call last):
  106. File "<stdin>", line 1, in ?
  107. TypeError: 'int' object is not iterable
  108. >>> _max(1)
  109. Traceback (most recent call last):
  110. File "<stdin>", line 1, in ?
  111. TypeError: 'int' object is not iterable
  112. >>> max([1])
  113. 1
  114. >>> _max([1])
  115. 1
  116. >>> max()
  117. Traceback (most recent call last):
  118. File "<stdin>", line 1, in ?
  119. TypeError: max expected 1 arguments, got 0
  120. >>> _max()
  121. Traceback (most recent call last):
  122. File "<stdin>", line 1, in ?
  123. TypeError: max expected 1 arguments, got 0
  124. >>> max([])
  125. Traceback (most recent call last):
  126. File "<stdin>", line 1, in ?
  127. ValueError: max() arg is an empty sequence
  128. >>> _max([])
  129. """
  130. if len(args) == 1 and not args[0]:
  131. return None
  132. return max(*args)
  133. if __name__ == "__main__":
  134. import doctest
  135. doctest.testmod()