Next / Previous / Contents / Shipman's homepage

19.10. BaseCompiler.classify(): Which kind of line is it?

Each of the three nontrivial line types is processed by its own specific method. This facilitates the different formats processed by different banding sheet layouts. It also makes possible the construction of custom data compilers for nonstandard formats. For line classification, see the specification.

baseclasses.py
# - - -   B a s e C o m p i l e r . c l a s s i f y   - - -

    def classify(self):
        '''Dispatch lines by type.

          [ if the line in self.scan is empty or contains only
            whitespace ->
              raise StopIteration
            else if the line in self.scan is a valid new page header ->
              self.pageHeader  :=  a PageHeader object representing
                  that line
              raise StopIteration
            else if the line in self.scan is a valid band prefix line
            in the context of self ->
              self.prefix  :=  a Prefix object representing that line
              raise StopIteration
            else if the line in self.scan is a valid encounter line in
            the context of self ->
              yield a BaseEncounter object representing that line
            else ->
              Log()  +:=  error message(s)
              raise StopIteration ]
            In any case ->
              self.scan  :=  self.scan advanced no further than end
                             of line ]
        '''

First we'll test for an empty or blank line. The Python string .strip() method removes all leading and trailing whitespace; if the line is empty or blank, it returns an empty string.

baseclasses.py
        #-- 1 --
        # [ if the line in self.scan is empty or contains only
        #   whitespace ->
        #     return
        #   else -> I ]
        if  len(self.scan.line.strip()) == 0:
            raise StopIteration

Next we check for the special symbols that start page header and prefix lines. If it isn't one of them, it must be an encounter line. Note: the .tabMatch() of the Scan class advances past the matched string. Also note that we have to use a for loop to call self.scanEncounter, because it is a generator. See Section 19.11, “BaseCompiler.scanPageHeader(): Process a page header line”, Section 19.12, “BaseCompiler.scanPrefix(): Process a band prefiix line”, and Section 19.13, “BaseCompiler.scanEncounter(): Process an encounter line”.

baseclasses.py
        #-- 2 --
        # [ if the line in self.scan is a valid page header line ->
        #     self.pageHeader  :=  a PageHeader object representing
        #                          that line
        #   else if the line in self.scan is a valid band prefix line ->
        #     self.prefix  :=  a Prefix object representing that line
        #   else if the line in self.scan is a valid encounter record ->
        #     yield a BaseEncounter object representing that record
        #   else ->
        #     Log()  +:=  error message(s)
        #   In any case ->
        #     self.scan  :=  self.scan advanced no further than end
        #                    of line ]
        if  self.scan.tabMatch(PAGE_SYMBOL):
            self.scanPageHeader()
            raise StopIteration
        elif self.scan.tabMatch(PREFIX_SYMBOL):
            self.scanPrefix()
            raise StopIteration
        else:
            for x in self.scanEncounter():
                yield x
            raise StopIteration