Next / Previous / Contents / Shipman's homepage

21. class YearCollection: The top-level data structure

The single instance of this class holds the entire collection of years and months.

noteweb
# - - - - -   c l a s s   Y e a r C o l l e c t i o n

class YearCollection:
    '''Represents the entire set of years and months.

      Exports:
        YearCollection():
          [ return a new, empty YearCollection instance ]
        .addYear ( yyyy ):
          [ yyyy is a year number as a four-digit string ->
              self  :=  self with a new, empty YearRow 
                        added for year=yyyy
              return that YearRow ]
        .__getitem__(self, yyyy):
          [ yyyy is a year number as a four-digit string ->
              if yyyy is contained in self ->
                return the corresponding YearRow instance
              else -> raise KeyError ]
        .genYearsRev():
          [ generate self's contained YearRow instances in
            reverse chronological order ]
        .neighbors ( yyyy_mm ):
          [ yyyy_mm is a month string as 'yyyy-mm' ->
              let:
                prev == the month in self prior to yyyy_mm in
                    chronological sequence as a 'yyyy-mm' string,
                    or None if first
                next == the month in self after yyyy_mm in
                    chronological sequence as a 'yyyy-mm' string,
                    or None if last
              in:
                return (prev,next) ]
        .findMonth ( yyyy_mm ):
          [ yyyy_mm is a month key string as 'yyyy-mm' ->
              if self has a month with that key ->
                return the corresponding MonthCell
              else -> raise KeyError ]

Inside the instance, we will need someplace to store the contained YearRow instances. Because the set of years might have gaps in it, we can use a dictionary whose keys are year numbers.

noteweb
      State/Invariants:
        .__yearMap:
          [ a dictionary whose keys are the 'yyyy' strings
            of years in self, and each corresponding value
            is a YearRow instance for that year ]
    '''