Skip to content

Instantly share code, notes, and snippets.

@cshuaimin
Created September 4, 2018 06:21
Show Gist options
  • Save cshuaimin/e951dd405f7df96b3f75df9d6d67c17c to your computer and use it in GitHub Desktop.
Save cshuaimin/e951dd405f7df96b3f75df9d6d67c17c to your computer and use it in GitHub Desktop.
Generate school timetable in terminal.
import random
from collections import defaultdict
from itertools import cycle
from termcolor import colored
from terminaltables import SingleTable
colors = ['green', 'yellow', 'magenta', 'cyan', 'white']
random.shuffle(colors)
colors = cycle(colors)
data = [
['节次', '星期一', '星期二', '星期三', '星期四', '星期五'],
]
# 标题颜色
for i, color in zip(range(len(data[0])), colors):
data[0][i] = colored(data[0][i], color)
default_colors = defaultdict(lambda: next(colors))
def colored_input(prompt):
# s = input(prompt)
s = input()
return colored(s, default_colors[s])
for i in range(3):
row = ['']
for day in range(1, 6):
subject = colored_input('Subject > ')
weeks = colored_input('Weeks > ')
position = colored_input('Position >')
row.append(f'{subject}\n{weeks}\n{position}')
data.append(row)
# 第一列
color = iter(colors)
num = (str(i) for i in range(1, (len(data) - 1) * 2 + 1))
for i in range(1, len(data)):
data[i][0] = f'\n{colored(next(num), next(color))}-{colored(next(num), next(color))} 节'
table = SingleTable(data)
table.inner_row_border = True
for i in range(len(data[0])):
table.justify_columns[i] = 'center'
print(table.table)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment