I am trying to extend the calendar
module to color days according to a passed pd.Series, whose index is the dates and whose value is a color code (0 to 10).
I'm not deep enough into OOP to do this. The TextCalendar
class has a method formatyear
, which in turn calls a method formatweek
.
My current solution is to define a class ColoredTextCalendar(TextCalendar)
whose __init__()
sets a class property date_to_color
(a pandas Series of color codes, with the date as index). I then basically copy-and-pasted the original formatyear
and formatweek
methods, and changed only two or three lines in them: formatyear
now calls formatweek
with additional parameters year
and month
, so that this method knows what actual date it is formatting (previously it just printed a 7-day calendar week like 12 13 14 15 16 17 18
). And formatweek
now looks up the corresponding color code in its class property date_to_color
and colors each day's string accordingly.
What I'm doing kind of works, but it feels wrong, like there should be a simpler, more straightforward solution. Copy-and-pasting a long method and changing only a few lines feels wrong - that's not how you usually do OOP, right?