Skip to content

Commit

Permalink
Personalise calendar data
Browse files Browse the repository at this point in the history
  • Loading branch information
Naunet authored Jun 14, 2020
1 parent 0048199 commit d1bdb25
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions my_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def __init__(self, month, year):
self.month = month
self.year = year
self.table_vals = self.makeCalendar()
self.personal_vals = None
self.col_labels = list()
for i in range(7):
self.col_labels.append(calendar.day_abbr[i])
self.draw()

def makeCalendar(self):
vals = list()
Expand Down Expand Up @@ -50,13 +50,75 @@ def draw(self):
for icol, col in enumerate(self.col_labels):
plt.text(icol + 0.5, nrow + 0.5,
col, ha='center', va='center')
# plot table content
# plot dates
for irow, row in enumerate(self.table_vals):
for icol, cell in enumerate(row):
plt.text(icol + 0.5, nrow - irow - 0.5,
cell, ha='center', va='center')
plt.text(icol + 0.1, nrow - irow - 0.1,
cell, va='top')
# plot data
if self.personal_vals:
for irow, row in enumerate(self.personal_vals):
for icol, cell in enumerate(row):
plt.text(icol + 0.5, nrow - irow - 0.6,
cell, ha='center', va='center',
color='blue')
plt.show()

"""
@param content an iterable structure
@param day between 1 and number of days in the month, default 1
Sets data to line up with given day
One element of iterable per day
Truncates at number of days in the month
"""
def set_personal_data(self, content, day=1):
vals = list(content)
_, limit = calendar.monthrange(self.year, self.month)
vals = vals[:limit]
prefix = list()
i = 0
j = 0
while self.table_vals[j][i] != day:
prefix.append('')
i += 1
if i == 7:
i = 0
j += 1
vals = prefix + vals
prefix = list()
vals = prefix + vals
self.personal_vals = list()
start = 0
for end in range(7, len(vals)+7, 7):
self.personal_vals.append(vals[start:end])
start = end

"""
@param day between 1 and the number of days in the month
@return tuple of position from top left
"""
def day_to_pos(self, day):
pos_x = (day-1) % 7
pos_y = (day-1)//7
while(self.table_vals[pos_y][pos_x] != day):
if pos_x == 7:
pos_y += 1
pos_x = 0
else:
pos_x += 1
return(pos_x, pos_y)

"""
@param x between 0 and 6
@param y between 0 and number of weeks in the month -1
@return tuple of position from bottom left
"""
def pos_to_coord(self, x, y):
coord_x = x
coord_y = len(self.table_vals)-y-1
return(coord_x, coord_y)

# Example
cal = My_Calendar(2, 1968)
def color_date(self, day, color):
pos = self.day_to_pos(day)
x, y = self.pos_to_coord(*pos)
plt.fill([x, x, x+1, x+1], [y, y+1, y+1, y], color)

0 comments on commit d1bdb25

Please sign in to comment.