Next / Previous / Contents / Shipman's homepage

9. class Location: Represents a group of stations

An object of this class represents one location.

baseclasses.py
# - - - - -   c l a s s   L o c a t i o n   - - - - -

class Location:
    '''Represents one location (containing one or more stations).

      Exports:
        Location(locCode):
          [ locCode is a location code as a string ->
              return a new Location object with that code and
              containing no stations ]
        .locCode:     [ as passed to constructor, uppercased, read-only ]
        .addStation(station):
          [ station is a Station object ->
              if self has a station whose station code matches
              a station in self ->
                raise IOError
              else ->
                self  :=  self with station added ]
        .lookupStaCode(staCode):
          [ staCode is a string ->
              if staCode matches a station code in self, case
              insensitive ->
                return the Station with that station code
              else -> raise KeyError ]
        .__str__(self):
          [ return a string representation of self ]

      State/Invariants:
        .staCodeMap:
          [ a dictionary whose keys are the station codes in
            self, uppercased, and each corresponding value is
            the Station object having that station code ]
    '''

9.1. Location.__init__(): Constructor

The constructor uppercases and stores the location code, then creates the empty .staCodeMap dictionary.

baseclasses.py
# - - -   L o c a t i o n . _ _ i n i t _ _   - - -

    def __init__(self, locCode):
        '''Constructor for Location
        '''
        self.locCode = locCode.upper()
        self.staCodeMap = {}