1
1
# -*- coding: utf-8 -*-
2
-
3
-
2
+ # © 2016 Thomas Binsfeld
3
+ # © 2016 ACSONE SA/NV (<http://acsone.eu>)
4
+ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
4
5
"""
5
- Provides the AccountingNone singleton
6
+ Provides the AccountingNone singleton.
6
7
7
8
AccountingNone is a null value that dissolves in basic arithmetic operations,
8
- as illustrated in the examples below
9
+ as illustrated in the examples below. In comparisons, AccountingNone behaves
10
+ the same as zero.
9
11
10
12
>>> 1 + 1
11
13
2
51
53
AccountingNone
52
54
>>> AccountingNone * None
53
55
AccountingNone
56
+ >>> None * AccountingNone
57
+ AccountingNone
58
+ >>> str(AccountingNone)
59
+ ''
60
+ >>> bool(AccountingNone)
61
+ False
62
+ >>> AccountingNone > 0
63
+ False
64
+ >>> AccountingNone < 0
65
+ False
66
+ >>> AccountingNone < 1
67
+ True
68
+ >>> AccountingNone > 1
69
+ False
70
+ >>> 0 < AccountingNone
71
+ False
72
+ >>> 0 > AccountingNone
73
+ False
74
+ >>> 1 < AccountingNone
75
+ False
76
+ >>> 1 > AccountingNone
77
+ True
78
+ >>> AccountingNone == 0
79
+ True
80
+ >>> AccountingNone == 0.0
81
+ True
82
+ >>> AccountingNone == None
83
+ True
54
84
"""
55
85
86
+ __all__ = ['AccountingNone' ]
87
+
56
88
57
89
class AccountingNoneType (object ):
58
90
@@ -89,10 +121,15 @@ def __pos__(self):
89
121
def __neg__ (self ):
90
122
return self
91
123
124
+ def __div__ (self , other ):
125
+ if other is AccountingNone :
126
+ return AccountingNone
127
+ return 0.0
128
+
129
+ def __rdiv__ (self , other ):
130
+ raise ZeroDivisionError
131
+
92
132
def __floordiv__ (self , other ):
93
- """
94
- Overload of the // operator
95
- """
96
133
if other is AccountingNone :
97
134
return AccountingNone
98
135
return 0.0
@@ -101,9 +138,6 @@ def __rfloordiv__(self, other):
101
138
raise ZeroDivisionError
102
139
103
140
def __truediv__ (self , other ):
104
- """
105
- Overload of the / operator
106
- """
107
141
if other is AccountingNone :
108
142
return AccountingNone
109
143
return 0.0
@@ -116,17 +150,29 @@ def __mul__(self, other):
116
150
return AccountingNone
117
151
return 0.0
118
152
119
- def __rmul__ (self , other ):
120
- if other is None or other is AccountingNone :
121
- return AccountingNone
122
- return 0.0
153
+ __rmul__ = __mul__
123
154
124
155
def __repr__ (self ):
125
156
return 'AccountingNone'
126
157
127
- def __unicode__ (self ):
158
+ def __str__ (self ):
128
159
return ''
129
160
161
+ def __nonzero__ (self ):
162
+ return False
163
+
164
+ def __bool__ (self ):
165
+ return False
166
+
167
+ def __eq__ (self , other ):
168
+ return other == 0 or other is None or other is AccountingNone
169
+
170
+ def __lt__ (self , other ):
171
+ return 0 < other
172
+
173
+ def __gt__ (self , other ):
174
+ return 0 > other
175
+
130
176
131
177
AccountingNone = AccountingNoneType ()
132
178
0 commit comments