Next / Previous / Contents / Shipman's homepage

9. class FoDim: Dimensional arithmetic

In a situation where you need to do arithmetic with dimensions—adding them, subtracting them—use an instance of this class to represent the dimension. See Section 4.3, “class FoDim: General dimension”.

fohelpers.py
# - - - - -   c l a s s   F o D i m

class FoDim(object):
    '''Represents a length dimension on XSL-FO.

      Exports:
        FoDim(n, units):
          [ (n is a number in a form acceptable to the Decimal()
            constructor) and
            (units is in ['in', 'mm', 'cm', 'pc', 'pt']) ->
              return a FoDim instance representing that length
        .__str__(self):
          [ return a string representing self as an XSL-FO length ]
        .__add__(self, other):
          [ other is a FoDim instance ->
              return a new FoDim representing self+other ]
        .__sub__(self, other):
          [ other is a FoDim instance ->
              return a new FoDim representing self-other ]
        .__mul__(self, other):
          [ other is a value acceptable to the Decimal() constructor ->
              return a new FoDim representing self*other ]
        .__div__(self, other):
          [ other is a value acceptable to the Decimal() constructor ->
              if other is zero ->
                raise ZeroDivisionError
              else ->
                return a new FoDim representing self/other
        .__neg__(self):
          [ return a new FoDim representing 0-self ]
        .convert(newUnits):
          [ newUnits is one of the units acceptable to FoDim() ->
              return a new FoDim representing self expressed
              in newUnits ]

      State/Invariants:
        .n:       [ as passed to constructor, read-only ]
        .units:   [ as passed to constructor, read-only ]
    '''

For a program that tests the various unit conversions of this class, see Section 30, “confactest: Test unit conversions”.