For the algorithm, see Section 21.5, “YearCollection.neighbors(): Find
previous and next month”.
# - - - Y e a r C o l l e c t i o n . _ _ f i n d P r e v
def __findPrev ( self, yyyy_mm ):
'''Find the month preceding yyyy_mm.
[ yyyy_mm is a month in self with key string 'yyyy-mm' ->
if self has any months before yyyy_mm ->
return the month key of the immediate predecessor
else -> return None ]
'''
First we find the YearRow containing yyyy_mm, and ask that instance if the month has
a predecessor in the same year.
#-- 1 --
# [ yyyy := year part of yyyy_mm
# mm := month part of yyyy_mm ]
yyyy, mm = yyyy_mm.split('-')
#-- 2 --
# [ yyyy is a year in self ->
# yearRow := YearRow instance for year yyyy ]
yearRow = self[yyyy]
#-- 3 --
# [ if yearRow has a predecessor to month mm ->
# return that month's key as a string 'mm'
# else -> I ]
try:
prev = yearRow.predecessor ( mm )
return '%s-%s' % (yyyy, prev)
except KeyError:
pass
If there is a predecessor, it must be in a previous year.
First we form a list of the year numbers in descending
order. Then we find the position of yyyy
in that list, and step through the remaining elements (if
any) until we find a year that has at least one month in
it, and return the last month.
#-- 4 --
# [ yyyyList := all year key strings in self, sorted
# in descending order ]
yyyyList = self.__yearMap.keys()
yyyyList.sort()
yyyyList.reverse()
#-- 5 --
# [ yyyy is an element of yyyyList ->
# pos := yyyy's position in yyyyList ]
pos = yyyyList.index ( yyyy )
For the method that looks for the last month of a year,
see Section 22.5, “YearRow.lastMonth(): Return the
last month”.
#-- 6 --
# [ if any year with a key in yyyyList[pos+1:] has at
# least one month in it ->
# return the yyyy-mm key of the last month in the
# first such year
# else -> I ]
for prevYear in yyyyList[pos+1:]:
prevRow = self[prevYear]
if len(prevRow) > 0:
lastMM = prevRow.lastMonth()
return '%s-%s' % (prevYear, lastMM)
#-- 7 --
return None