GnuCash fec5643+
Loading...
Searching...
No Matches
Public Member Functions | Data Fields | Protected Member Functions
python.gnucash_core.GncNumeric Class Reference
Inheritance diagram for python.gnucash_core.GncNumeric:
python.gnucash_core.GnuCashCoreClass

Public Member Functions

 __init__ (self, *args, **kargs)
 
 __setattr__ (self, name, value)
 
 __lt__ (a, b)
 
 __gt__ (a, b)
 
 __le__ (a, b)
 
 __ge__ (a, b)
 
 __eq__ (a, b)
 
 __bool__ (a)
 
 __float__ (self)
 
 __int__ (self)
 
 __pos__ (a)
 
 __neg__ (a)
 
 __abs__ (a)
 
 to_fraction (self)
 
 __str__ (self)
 
- Public Member Functions inherited from python.gnucash_core.GnuCashCoreClass
 do_lookup_create_oo_instance (self, lookup_function, cls, *args)
 

Data Fields

 instance
 

Protected Member Functions

 _operator_fallbacks (monomorphic_operator, fallback_operator)
 
 _add (a, b)
 
 _sub (a, b)
 
 _mul (a, b)
 
 _div (a, b)
 
 _floordiv (a, b)
 
 _lt (a, b)
 
 _gt (a, b)
 
 _le (a, b)
 
 _ge (a, b)
 
 _eq (a, b)
 
 _richcmp (self, other, op)
 

Additional Inherited Members

- Static Protected Attributes inherited from python.gnucash_core.GnuCashCoreClass
 _module = gnucash_core_c
 

Detailed Description

Object used by GnuCash to store all numbers. Always consists of a
numerator and denominator.

The constants GNC_DENOM_AUTO,
GNC_HOW_RND_FLOOR, GNC_HOW_RND_CEIL, GNC_HOW_RND_TRUNC,
GNC_HOW_RND_PROMOTE, GNC_HOW_RND_ROUND_HALF_DOWN,
GNC_HOW_RND_ROUND_HALF_UP, GNC_HOW_RND_ROUND, GNC_HOW_RND_NEVER,
GNC_HOW_DENOM_EXACT, GNC_HOW_DENOM_REDUCE, GNC_HOW_DENOM_LCD,
and GNC_HOW_DENOM_FIXED are available for arithmetic
functions like GncNumeric.add

Look at gnc-numeric.h to see how to use these

Definition at line 537 of file gnucash_core.py.

Constructor & Destructor Documentation

◆ __init__()

python.gnucash_core.GncNumeric.__init__ (   self,
*  args,
**  kargs 
)
Constructor that supports the following formats:
* No arguments defaulting to zero: eg. GncNumeric() == 0/1
* A integer: e.g. GncNumeric(1) == 1/1
* Numerator and denominator intager pair: eg. GncNumeric(1, 2) == 1/2
* A floating point number: e.g. GncNumeric(0.5) == 1/2
* A floating point number with defined conversion: e.g.
  GncNumeric(0.5, GNC_DENOM_AUTO,
            GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER) == 1/2
* A string: e.g. GncNumeric("1/2") == 1/2

Definition at line 552 of file gnucash_core.py.

552 def __init__(self, *args, **kargs):
553 """Constructor that supports the following formats:
554 * No arguments defaulting to zero: eg. GncNumeric() == 0/1
555 * A integer: e.g. GncNumeric(1) == 1/1
556 * Numerator and denominator intager pair: eg. GncNumeric(1, 2) == 1/2
557 * A floating point number: e.g. GncNumeric(0.5) == 1/2
558 * A floating point number with defined conversion: e.g.
559 GncNumeric(0.5, GNC_DENOM_AUTO,
560 GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER) == 1/2
561 * A string: e.g. GncNumeric("1/2") == 1/2
562 """
563 if 'instance' not in kargs:
564 kargs['instance'] = GncNumeric.__args_to_instance(args)
565 GnuCashCoreClass.__init__(self, [], **kargs)
566

Member Function Documentation

◆ __abs__()

python.gnucash_core.GncNumeric.__abs__ (   a)
abs(a)

Definition at line 734 of file gnucash_core.py.

734 def __abs__(a):
735 """abs(a)"""
736 return a.abs()
737

◆ __bool__()

python.gnucash_core.GncNumeric.__bool__ (   a)
a != 0

Definition at line 716 of file gnucash_core.py.

716 def __bool__(a):
717 """a != 0"""
718 return bool(a.num())
719

◆ __eq__()

python.gnucash_core.GncNumeric.__eq__ (   a,
  b 
)
a == b

Definition at line 712 of file gnucash_core.py.

712 def __eq__(a, b):
713 """a == b"""
714 return a._richcmp(b, a._eq)
715

◆ __float__()

python.gnucash_core.GncNumeric.__float__ (   self)

Definition at line 720 of file gnucash_core.py.

720 def __float__(self):
721 return self.to_double()
722

◆ __ge__()

python.gnucash_core.GncNumeric.__ge__ (   a,
  b 
)
a >= b

Definition at line 708 of file gnucash_core.py.

708 def __ge__(a, b):
709 """a >= b"""
710 return a._richcmp(b, a._ge)
711

◆ __gt__()

python.gnucash_core.GncNumeric.__gt__ (   a,
  b 
)
a > b

Definition at line 700 of file gnucash_core.py.

700 def __gt__(a, b):
701 """a > b"""
702 return a._richcmp(b, a._gt)
703

◆ __int__()

python.gnucash_core.GncNumeric.__int__ (   self)

Definition at line 723 of file gnucash_core.py.

723 def __int__(self):
724 return int(self.to_double())
725

◆ __le__()

python.gnucash_core.GncNumeric.__le__ (   a,
  b 
)
a <= b

Definition at line 704 of file gnucash_core.py.

704 def __le__(a, b):
705 """a <= b"""
706 return a._richcmp(b, a._le)
707

◆ __lt__()

python.gnucash_core.GncNumeric.__lt__ (   a,
  b 
)
a < b

Definition at line 696 of file gnucash_core.py.

696 def __lt__(a, b):
697 """a < b"""
698 return a._richcmp(b, a._lt)
699

◆ __neg__()

python.gnucash_core.GncNumeric.__neg__ (   a)
-a

Definition at line 730 of file gnucash_core.py.

730 def __neg__(a):
731 """-a"""
732 return a.neg()
733

◆ __pos__()

python.gnucash_core.GncNumeric.__pos__ (   a)
+a

Definition at line 726 of file gnucash_core.py.

726 def __pos__(a):
727 """+a"""
728 return GncNumeric(a.num(), a.denom())
729
The primary numeric class for representing amounts and values.

◆ __setattr__()

python.gnucash_core.GncNumeric.__setattr__ (   self,
  name,
  value 
)

Definition at line 567 of file gnucash_core.py.

567 def __setattr__(self, name, value):
568 # num and denom are read-only accessor methods, not writable fields.
569 if name in ('num', 'denom'):
570 raise AttributeError(
571 "Cannot assign to '%s': it is a read-only accessor method on "
572 "GncNumeric, not a writable field, so the assignment would not "
573 "change the value. Construct a new value instead, e.g. "
574 "GncNumeric(numerator, denominator)." % name)
575 super().__setattr__(name, value)
576

◆ __str__()

python.gnucash_core.GncNumeric.__str__ (   self)
Returns a human readable numeric value string as UTF8.

Definition at line 742 of file gnucash_core.py.

742 def __str__(self):
743 """Returns a human readable numeric value string as UTF8."""
744 return gnc_numeric_to_string(self.instance)
745

◆ _add()

python.gnucash_core.GncNumeric._add (   a,
  b 
)
protected

Definition at line 637 of file gnucash_core.py.

637 def _add(a, b):
638 return a.add(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
639

◆ _div()

python.gnucash_core.GncNumeric._div (   a,
  b 
)
protected

Definition at line 646 of file gnucash_core.py.

646 def _div(a, b):
647 return a.div(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
648

◆ _eq()

python.gnucash_core.GncNumeric._eq (   a,
  b 
)
protected

Definition at line 676 of file gnucash_core.py.

676 def _eq(a, b):
677 return a.compare(b) == 0
678

◆ _floordiv()

python.gnucash_core.GncNumeric._floordiv (   a,
  b 
)
protected

Definition at line 649 of file gnucash_core.py.

649 def _floordiv(a, b):
650 return a.div(b, 1, GNC_HOW_RND_TRUNC)
651

◆ _ge()

python.gnucash_core.GncNumeric._ge (   a,
  b 
)
protected

Definition at line 673 of file gnucash_core.py.

673 def _ge(a, b):
674 return a.compare(b) in (0,1)
675

◆ _gt()

python.gnucash_core.GncNumeric._gt (   a,
  b 
)
protected

Definition at line 667 of file gnucash_core.py.

667 def _gt(a, b):
668 return a.compare(b) == 1
669

◆ _le()

python.gnucash_core.GncNumeric._le (   a,
  b 
)
protected

Definition at line 670 of file gnucash_core.py.

670 def _le(a, b):
671 return a.compare(b) in (0,-1)
672

◆ _lt()

python.gnucash_core.GncNumeric._lt (   a,
  b 
)
protected

Definition at line 664 of file gnucash_core.py.

664 def _lt(a, b):
665 return a.compare(b) == -1
666

◆ _mul()

python.gnucash_core.GncNumeric._mul (   a,
  b 
)
protected

Definition at line 643 of file gnucash_core.py.

643 def _mul(a, b):
644 return a.mul(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
645

◆ _operator_fallbacks()

python.gnucash_core.GncNumeric._operator_fallbacks (   monomorphic_operator,
  fallback_operator 
)
protected
fallbacks are not needed except for method name,
keep for possible later use

Definition at line 614 of file gnucash_core.py.

614 def _operator_fallbacks(monomorphic_operator, fallback_operator):
615 """fallbacks are not needed except for method name,
616 keep for possible later use"""
617 def forward(a, b):
618 if isinstance(b, GncNumeric):
619 return monomorphic_operator(a, b)
620 if isinstance(b, (int, float)):
621 return monomorphic_operator(a, GncNumeric(b))
622 else:
623 return NotImplemented
624 forward.__name__ = '__' + fallback_operator.__name__ + '__'
625 forward.__doc__ = monomorphic_operator.__doc__
626
627 def reverse(b, a):
628 if isinstance(a, (GncNumeric, int, float)):
629 return forward(b, a)
630 else:
631 return NotImplemented
632 reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
633 reverse.__doc__ = monomorphic_operator.__doc__
634
635 return forward, reverse
636

◆ _richcmp()

python.gnucash_core.GncNumeric._richcmp (   self,
  other,
  op 
)
protected
Helper for comparison operators, for internal use only.
Implement comparison between a GncNumeric instance `self`,
and either another GncNumeric instance, an int or a float
`other`.  If `other` is not an instance of that kind, return
NotImplemented. `op` should be one of the six standard
comparison operators. The comparisons are based on
GncNumeric.compare().

Definition at line 679 of file gnucash_core.py.

679 def _richcmp(self, other, op):
680 """Helper for comparison operators, for internal use only.
681 Implement comparison between a GncNumeric instance `self`,
682 and either another GncNumeric instance, an int or a float
683 `other`. If `other` is not an instance of that kind, return
684 NotImplemented. `op` should be one of the six standard
685 comparison operators. The comparisons are based on
686 GncNumeric.compare().
687 """
688 import math
689 if isinstance(other, GncNumeric):
690 return op(other)
691 elif isinstance(other, (int, float)):
692 return op(GncNumeric(other))
693 else:
694 return NotImplemented
695

◆ _sub()

python.gnucash_core.GncNumeric._sub (   a,
  b 
)
protected

Definition at line 640 of file gnucash_core.py.

640 def _sub(a, b):
641 return a.sub(b, GNC_DENOM_AUTO, GNC_HOW_RND_ROUND)
642

◆ to_fraction()

python.gnucash_core.GncNumeric.to_fraction (   self)

Definition at line 738 of file gnucash_core.py.

738 def to_fraction(self):
739 from fractions import Fraction
740 return Fraction(self.num(), self.denom())
741

Field Documentation

◆ instance

python.gnucash_core.GncNumeric.instance

Definition at line 744 of file gnucash_core.py.


The documentation for this class was generated from the following file: