Next / Previous / Contents / Shipman's homepage

61. class MassField: Body mass

For the format of the input field for mass, see the specification.

The mass field is just a bit trickier than the wing field, which is one reason we didn't write an IntField base class that could handle both of them. The mass field is in decigrams, with an implied decimal before the last digit, so " 143" means 14.3 grams. The .flatten() method must insert the decimal point explicitly, assuming there is a value at all; the field is optional.

baseclasses.py
# - - - - -   c l a s s   W e i g h t F i e l d   - - - - -

class MassField(FieldItem):
    '''Represents a mass in decigrams.

      Exports, beyond those inherited:
        .mass:    [ mass in decigrams ]
    '''

61.1. MassField.scanField()

If the field is blank, we take no action, so that the corresponding field of the encounter record will have its default value of None.

As your author is a veteran of data entry going back to the IBM 029 keypunch in 1966, he abhors trailing blanks in an integer field. Python's int() function is far too permissive, converting an abomination such as "34 " without a peep. Fortunately, the Scan.flatInt() method does not allow such bad practice.

baseclasses.py
# - - -   W e i g h t F i e l d . s c a n F i e l d   - - -

    @staticmethod
    def scanField(encounter, scan, fieldName):
        '''Scan a mass field.
        '''

        #-- 1 --
        # [ if scan starts with a blank mass field ->
        #       scan  :=  scan advanced past that field
        #       return
        #   else -> I ]
        if  scan.tabMatch(BLANK_MASS_IN):
            return

        #-- 2 --
        # [ if scan starts with a valid integer field of size
        #   MASS_IN_L ->
        #       scan    :=  scan advanced MASS_IN_L
        #       mass    :=  that field as an integer
        #   else ->
        #     Log()  +:=  error message
        #     raise SyntaxError ]
        mass = scan.flatInt(MASS_IN_L)
        if  mass is None:
            scan.syntax("Mass field '%s' is not a valid integer." %
                          MASS_IN_L)

        #-- 3 --
        # [ encounter.(fieldName)  :=  a new MassField object with
        #                              value (mass) ]
        setattr(encounter, fieldName,
                MassField(encounter, mass))