Next / Previous / Contents / Shipman's homepage

22.6. YearRow.predecessor(): Find the previous month

This method finds the month that precedes a given month, if there is one.

noteweb
# - - -   Y e a r R o w . p r e d e c e s s o r

    def predecessor ( self, mm ):
        '''Find the month preceding month mm, if any
        '''

First we find the set of month keys in self, then sort it in descending order, so that the preceding month's key (if any) will follow mm's position in that list. We get to assume that mm is a member of the list, by the precondition on this method.

noteweb
        #-- 1 --
        # [ mmList  :=  keys of self.__monthMap in descending order ]
        mmList  =  self.__monthMap.keys()
        mmList.sort()
        mmList.reverse()

        #-- 2 --
        # [ mm is a member of mmList ->
        #     pos  :=  position of mm in mmList ]
        pos  =  mmList.index ( mm )

        #-- 3 --
        # [ if pos+1 >= len(mmList) ->
        #     raise KeyError
        #   else ->
        #     return mmList[pos+1] ]
        if  pos+1 >= len(mmList):
            raise KeyError
        else:
            return mmList[pos+1]