Next / Previous / Contents / Shipman's homepage

27. class FoDim: A linear dimension

An instance of this class represents a dimension expressed in one of the units acceptable to XSL-FO.

fosox.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, coerced
            to Decimal type ]
        .units:   [ as passed to constructor, read-only ]
    '''

27.1. FoDim.__init__()

fosox.py
# - - -   F o D i m . _ _ i n i t _ _

    def __init__(self, n, units):
        '''Constructor
        '''
        self.n = Decimal(n)
        self.units = units