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.

136 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Provides the AccountingNone singleton
  4. AccountingNone is a null value that dissolves in basic arithmetic operations,
  5. as illustrated in the examples below
  6. >>> 1 + 1
  7. 2
  8. >>> 1 + AccountingNone
  9. 1
  10. >>> AccountingNone + 1
  11. 1
  12. >>> AccountingNone + None
  13. AccountingNone
  14. >>> +AccountingNone
  15. AccountingNone
  16. >>> -AccountingNone
  17. AccountingNone
  18. >>> -(AccountingNone)
  19. AccountingNone
  20. >>> AccountingNone - 1
  21. -1
  22. >>> 1 - AccountingNone
  23. 1
  24. >>> AccountingNone - None
  25. AccountingNone
  26. >>> AccountingNone / 2
  27. 0.0
  28. >>> 2 / AccountingNone
  29. Traceback (most recent call last):
  30. ...
  31. ZeroDivisionError
  32. >>> AccountingNone / AccountingNone
  33. AccountingNone
  34. >>> AccountingNone // 2
  35. 0.0
  36. >>> 2 // AccountingNone
  37. Traceback (most recent call last):
  38. ...
  39. ZeroDivisionError
  40. >>> AccountingNone // AccountingNone
  41. AccountingNone
  42. >>> AccountingNone * 2
  43. 0.0
  44. >>> 2 * AccountingNone
  45. 0.0
  46. >>> AccountingNone * AccountingNone
  47. AccountingNone
  48. >>> AccountingNone * None
  49. AccountingNone
  50. """
  51. class AccountingNoneType(object):
  52. def __add__(self, other):
  53. if other is None:
  54. return AccountingNone
  55. return other
  56. __radd__ = __add__
  57. def __sub__(self, other):
  58. if other is None:
  59. return AccountingNone
  60. return -other
  61. def __rsub__(self, other):
  62. if other is None:
  63. return AccountingNone
  64. return other
  65. def __iadd__(self, other):
  66. if other is None:
  67. return AccountingNone
  68. return other
  69. def __isub__(self, other):
  70. if other is None:
  71. return AccountingNone
  72. return -other
  73. def __pos__(self):
  74. return self
  75. def __neg__(self):
  76. return self
  77. def __floordiv__(self, other):
  78. """
  79. Overload of the // operator
  80. """
  81. if other is AccountingNone:
  82. return AccountingNone
  83. return 0.0
  84. def __rfloordiv__(self, other):
  85. raise ZeroDivisionError
  86. def __truediv__(self, other):
  87. """
  88. Overload of the / operator
  89. """
  90. if other is AccountingNone:
  91. return AccountingNone
  92. return 0.0
  93. def __rtruediv__(self, other):
  94. raise ZeroDivisionError
  95. def __mul__(self, other):
  96. if other is None or other is AccountingNone:
  97. return AccountingNone
  98. return 0.0
  99. def __rmul__(self, other):
  100. if other is None or other is AccountingNone:
  101. return AccountingNone
  102. return 0.0
  103. def __repr__(self):
  104. return 'AccountingNone'
  105. def __unicode__(self):
  106. return ''
  107. AccountingNone = AccountingNoneType()
  108. if __name__ == '__main__':
  109. import doctest
  110. doctest.testmod()