Using Timeboard to create an appendable file with 4 different shifts working a revolving 12/12 schedule

Hi all,

I am just finishing Python Crash Course, 2nd edition and have a project I’m starting on.
I am wanting to create a work schedule that I can append data to so that I can create a call out list for when people take vacation, call in sick, etc…

The following table is the revolving 28 day schedule with 1’s showing days that are worked and zero’s showing days off.

A = [0,0,1,1,1,1,0],[0,0,0,0,0,0,1],[1,1,1,0,0,0,1],[1,1,0,1,1,1,0]
B = [1,1,1,0,0,0,1],[1,1,0,1,1,1,0],[0,0,1,1,1,1,0],[0,0,0,0,0,0,1]
C = [1,1,0,1,1,1,0],[0,0,1,1,1,1,0],[0,0,0,0,0,0,1],[1,1,1,0,0,0,1]
D = [0,0,0,0,0,0,1],[1,1,1,0,0,0,1],[1,1,0,1,1,1,0],[0,0,1,1,1,1,0]

I’ve been trying to use Timeboard and have successfully created a schedule for shift A-D but can’t seem to figure out how to transpose them to appendable files so that I can build on them with other def’s.

Any help would be greatly appreciated!!
Cheers!

Here’s what I have so far:

import timeboard as tb
import pandas as pd

class making_calendar:

A = [0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,1,0,1,1,1,0]
B = [1,1,1,0,0,0,1,1,1,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1]
C = [1,1,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1]
D = [0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,1,0,1,1,1,0,0,0,1,1,1,1,0]

def shift_A():

    clnd_A = tb.Timeboard(base_unit_freq = 'D',start = '01 Jan 2022 0430', end = '01 Jan 2023 0430', 
        layout = [0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,1,0,1,1,1,0])
    print(clnd_A)

def shift_B():

    clnd_B = tb.Timeboard(base_unit_freq = 'D',start = '01 Jan 2022 0430', end = '01 Jan 2023 0430', 
        layout =[1,1,1,0,0,0,1,1,1,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1])
    print(clnd_B)

def shift_C():

    clnd_C = tb.Timeboard(base_unit_freq = 'D',start = '01 Jan 2022 0430', end = '01 Jan 2023 0430', 
        layout = [1,1,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1])

    print(clnd_C)

def shift_D():

    clnd_D = tb.Timeboard(base_unit_freq = 'D',start = '01 Jan 2022 0430', end = '01 Jan 2023 0430', 
        layout = [0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,1,0,1,1,1,0,0,0,1,1,1,1,0])

    print(clnd_D)
    
shift_A()
shift_B()
shift_C()
shift_D()

You have a lot of data and code duplication. For example: First you define the schedule like this:

Then again like this:

…and again for the third time:

Do not repeat your data. Have them just in one place. If your first format is the preferred source format:

from itertools import chain

worker_A_schedule_weeks = (
    [0,0,1,1,1,1,0], [0,0,0,0,0,0,1], [1,1,1,0,0,0,1], [1,1,0,1,1,1,0])
worker_A_schedule_days = list(chain.from_iterable(worker_A_schedule_weeks))
...
    clnd_A = tb.Timeboard(base_unit_freq='D', start='01 Jan 2022 0430',
        end='01 Jan 2023 0430', layout=worker_A_schedule_days)

What is your exact meaning of “revolving” here? Does the schedule shift by one each day? For example today’s item is removed and the 28th day from tommorow is added?

If it is so, there is an appropriate data structure in the standard library - deque:

from itertools import chain
from collections import deque

SCHEDULE_LENGTH = 28
worker_A_schedule_weeks = (
        [0,0,1,1,1,1,0], [0,0,0,0,0,0,1], [1,1,1,0,0,0,1], [1,1,0,1,1,1,0])
worker_A_schedule_days = deque(
        chain.from_iterable(worker_A_schedule_weeks), SCHEDULE_LENGTH)
worker_A_schedule_today = worker_A_schedule_days[0]
worker_A_schedule_days.append(1)  # the new value for day 28 from tommorow
# The values are shifted left automatically and the leftmost value is discarded.

You can access the values like values in a list. For example for day 5 from tomorrow: worker_A_schedule_days[4]

Instead of collections.deque you can use a normal list but list will not keep the fixed number of items (days) for you automatically:

...
worker_A_schedule_days = list(chain.from_iterable(worker_A_schedule_weeks))
worker_A_schedule_today = worker_A_schedule_days.pop(0)  # the list has 27 elements now
worker_A_schedule_days.append(1)  # the new value for day 28 from tommorow

Also you probably do not want to define a new variable for every worker. How would you add new workers? Use a list or dict of workers instead. Then you will be able to process all the workers using for loops which will help you to further minimize the code repetition.