Next / Previous / Contents / Shipman's homepage

8.3. BaseStationSet.__init__(): Constructor

All the constructor logic is the same for all derived classes except for one method that extracts the various fields from the input file.

baseclasses.py
# - - -   B a s e S t a t i o n S e t . _ _ i n i t _ _   - - -

    def __init__(self, fileName):
        '''Constructor for BaseStationSet.
        '''

First we create the two internal dictionaries.

baseclasses.py
        #-- 1 --
        self.locCodeMap = {}
        self.staNoMap   = {}

Next we attempt to open the file. The Scan() constructor will raise an IOError exception if we can't open it for reading. For more information on the Scan object, see Section 4, “baseclasses.py: Prologue”.

baseclasses.py
        #-- 2 --
        # [ if fileName can be opened for reading ->
        #     inFile  :=  that file, so opened
        #   else -> raise IOError ]
        scan = Scan(fileName)

Next we read through the lines in the file. If any of them are bad, we raise IOError. If all goes well, the data from the file is added to the internal dictionaries. See Section 8.4, “BaseStationSet.readLine(): Read one station line”.

baseclasses.py
        #-- 3 --
        # [ if inFile contains only valid station records ->
        #     self.locCodeMap  +:=  entries mapping the uppercased
        #         location codes from those records |-> Location
        #         objects representing those records
        #     self.staNoMap  +:=  entries mapping the station numbers
        #         from those records |-> Station objects representing
        #         those records
        #   else -> raise IOError ]
        while  not scan.atEndFile:
            #-- 3 body --
            # [ scan is a Scan object ->
            #     if the line in scan is not a valid station line ->
            #       raise IOError
            #     else if the line in scan contains a location code
            #     not in self.locCodeMap ->
            #       self.locCodeMap  +:=  a new entry whose key is
            #           that location code, uppercase, and whose
            #           value is a new Location containing the
            #           station from that line
            #     else if the line in scan contains a location code
            #     that is in self.locCodeMap ->
            #       corresponding value  +:=  the station from
            #           that line
            #     In any case ->
            #       scan  :=  scan advanced to the next line, if any ]
            self.readLine(scan)
            scan.nextLine()

        #-- 4 --
        scan.close()