Browse Source
[ADD] AccountingNone (singleton) to differentiate balances among which the debit and the credit are zero and balances among which debit and credit nullify
pull/169/head
[ADD] AccountingNone (singleton) to differentiate balances among which the debit and the credit are zero and balances among which debit and credit nullify
pull/169/head
ThomasBinsfeld
9 years ago
committed by
Thomas Binsfeld
4 changed files with 145 additions and 5 deletions
-
0mis_builder/__openerp__.py
-
136mis_builder/models/accounting_none.py
-
6mis_builder/models/aep.py
-
8mis_builder/models/mis_builder.py
@ -0,0 +1,136 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
|
||||
|
""" |
||||
|
Provides the AccountingNone singleton |
||||
|
|
||||
|
AccountingNone is a null value that dissolves in basic arithmetic operations, |
||||
|
as illustrated in the examples below |
||||
|
|
||||
|
>>> 1 + 1 |
||||
|
2 |
||||
|
>>> 1 + AccountingNone |
||||
|
1 |
||||
|
>>> AccountingNone + 1 |
||||
|
1 |
||||
|
>>> AccountingNone + None |
||||
|
AccountingNone |
||||
|
>>> +AccountingNone |
||||
|
AccountingNone |
||||
|
>>> -AccountingNone |
||||
|
AccountingNone |
||||
|
>>> -(AccountingNone) |
||||
|
AccountingNone |
||||
|
>>> AccountingNone - 1 |
||||
|
-1 |
||||
|
>>> 1 - AccountingNone |
||||
|
1 |
||||
|
>>> AccountingNone - None |
||||
|
AccountingNone |
||||
|
>>> AccountingNone / 2 |
||||
|
0.0 |
||||
|
>>> 2 / AccountingNone |
||||
|
Traceback (most recent call last): |
||||
|
... |
||||
|
ZeroDivisionError |
||||
|
>>> AccountingNone / AccountingNone |
||||
|
AccountingNone |
||||
|
>>> AccountingNone // 2 |
||||
|
0.0 |
||||
|
>>> 2 // AccountingNone |
||||
|
Traceback (most recent call last): |
||||
|
... |
||||
|
ZeroDivisionError |
||||
|
>>> AccountingNone // AccountingNone |
||||
|
AccountingNone |
||||
|
>>> AccountingNone * 2 |
||||
|
0.0 |
||||
|
>>> 2 * AccountingNone |
||||
|
0.0 |
||||
|
>>> AccountingNone * AccountingNone |
||||
|
AccountingNone |
||||
|
>>> AccountingNone * None |
||||
|
AccountingNone |
||||
|
""" |
||||
|
|
||||
|
|
||||
|
class AccountingNoneType(object): |
||||
|
|
||||
|
def __add__(self, other): |
||||
|
if other is None: |
||||
|
return AccountingNone |
||||
|
return other |
||||
|
|
||||
|
__radd__ = __add__ |
||||
|
|
||||
|
def __sub__(self, other): |
||||
|
if other is None: |
||||
|
return AccountingNone |
||||
|
return -other |
||||
|
|
||||
|
def __rsub__(self, other): |
||||
|
if other is None: |
||||
|
return AccountingNone |
||||
|
return other |
||||
|
|
||||
|
def __iadd__(self, other): |
||||
|
if other is None: |
||||
|
return AccountingNone |
||||
|
return other |
||||
|
|
||||
|
def __isub__(self, other): |
||||
|
if other is None: |
||||
|
return AccountingNone |
||||
|
return -other |
||||
|
|
||||
|
def __pos__(self): |
||||
|
return self |
||||
|
|
||||
|
def __neg__(self): |
||||
|
return self |
||||
|
|
||||
|
def __floordiv__(self, other): |
||||
|
""" |
||||
|
Overload of the // operator |
||||
|
""" |
||||
|
if other is AccountingNone: |
||||
|
return AccountingNone |
||||
|
return 0.0 |
||||
|
|
||||
|
def __rfloordiv__(self, other): |
||||
|
raise ZeroDivisionError |
||||
|
|
||||
|
def __truediv__(self, other): |
||||
|
""" |
||||
|
Overload of the / operator |
||||
|
""" |
||||
|
if other is AccountingNone: |
||||
|
return AccountingNone |
||||
|
return 0.0 |
||||
|
|
||||
|
def __rtruediv__(self, other): |
||||
|
raise ZeroDivisionError |
||||
|
|
||||
|
def __mul__(self, other): |
||||
|
if other is None or other is AccountingNone: |
||||
|
return AccountingNone |
||||
|
return 0.0 |
||||
|
|
||||
|
def __rmul__(self, other): |
||||
|
if other is None or other is AccountingNone: |
||||
|
return AccountingNone |
||||
|
return 0.0 |
||||
|
|
||||
|
def __repr__(self): |
||||
|
return 'AccountingNone' |
||||
|
|
||||
|
def __unicode__(self): |
||||
|
return '' |
||||
|
|
||||
|
|
||||
|
AccountingNone = AccountingNoneType() |
||||
|
|
||||
|
|
||||
|
if __name__ == '__main__': |
||||
|
import doctest |
||||
|
doctest.testmod() |
Write
Preview
Loading…
Cancel
Save
Reference in new issue