python单处理器甘特图
时间: 2023-11-29 22:47:35 浏览: 81
以下是一个使用Python编写的单处理器甘特图的示例代码:
```python
import datetime
class Task:
def __init__(self, name, start, end):
self.name = name
self.start = start
self.end = end
def gantt_chart(tasks):
start_date = min(task.start for task in tasks)
end_date = max(task.end for task in tasks)
date_range = end_date - start_date
chart = [[' '] * (len(tasks) * 2 + 1) for _ in range(date_range.days + 1)]
for i, task in enumerate(tasks):
row = [' '] * (len(tasks) * 2 + 1)
row[i * 2 + 1] = task.name
for j in range((task.end - task.start).days):
chart[(task.start - start_date).days + j][i * 2:i * 2 + 3] = ['─', '█', '─']
chart[(task.start - start_date).days][(i * 2):(i * 2 + 3)] = ['┌', '─', '┐']
chart[(task.end - start_date).days][(i * 2):(i * 2 + 3)] = ['└', '─', '┘']
chart[(task.start - start_date).days - 1][i * 2 + 1] = '┬'
chart[(task.end - start_date).days + 1][i * 2 + 1] = '┴'
chart[(task.start - start_date).days - 1][i * 2] = '│'
chart[(task.end - start_date).days + 1][i * 2] = '│'
chart[(task.start - start_date).days - 1][i * 2 + 2] = '│'
chart[(task.end - start_date).days + 1][i * 2 + 2] = '│'
chart[(task.start - start_date).days][i * 2] = '│'
chart[(task.end - start_date).days][i * 2] = '│'
chart[(task.start - start_date).days][i * 2 + 2] = '│'
chart[(task.end - start_date).days][i * 2 + 2] = '│'
chart[(task.start - start_date).days][i * 2 + 1] = '█'
chart[(task.end - start_date).days][i * 2 + 1] = '█'
chart[(task.start - start_date).days + 1][i * 2 + 1] = ' '
chart[(task.end - start_date).days - 1][i * 2 + 1] = ' '
chart[(task.start - start_date).days + 1][i * 2] = ' '
chart[(task.end - start_date).days - 1][i * 2] = ' '
chart[(task.start - start_date).days + 1][i * 2 + 2] = ' '
chart[(task.end - start_date).days - 1][i * 2 + 2] = ' '
chart[(task.start - start_date).days - 1][len(tasks) * 2] = '│'
chart[(task.end - start_date).days + 1][len(tasks) * 2] = '│'
chart[(task.start - start_date).days][len(tasks) * 2] = '│'
chart[(task.end - start_date).days][len(tasks) * 2] = '│'
chart[(task.start - start_date).days + 1][len(tasks) * 2 + 1] = ' '
chart[(task.end - start_date).days - 1][len(tasks) * 2 + 1] = ' '
chart[(task.start - start_date).days][len(tasks) * 2 + 1] = ' '
chart[(task.end - start_date).days][len(tasks) * 2 + 1] = ' '
chart[(task.start - start_date).days - 1][len(tasks) * 2 + 1] = '┼'
chart[(task.end - start_date).days + 1][len(tasks) * 2 + 1] = '┼'
chart[(task.start - start_date).days][len(tasks) * 2 + 1] = '│'
chart[(task.end - start_date).days][len(tasks) * 2 + 1] = '│'
chart[(task.start - start_date).days + 1][len(tasks) * 2] = '─'
chart[(task.end - start_date).days - 1][len(tasks) * 2] = '─'
chart[(task.start - start_date).days + 1][len(tasks) * 2 + 2] = '─'
chart[(task.end - start_date).days - 1][len(tasks) * 2 + 2] = '─'
chart[(task.start - start_date).days - 1][0] = '┌'
chart[(task.end - start_date).days + 1][0] = '└'
chart[(task.start - start_date).days][0] = '│'
chart[(task.end - start_date).days][0] = '│'
chart[(task.start - start_date).days + 1][1] = '─'
chart[(task.end - start_date).days - 1][1] = '─'
chart[(task.start - start_date).days + 1][0] = ' '
chart[(task.end - start_date).days - 1][0] = ' '
chart[(task.start - start_date).days][1] = '█'
chart[(task.end - start_date).days][1] = '█'
chart[0][0] = '┌'
chart[-1][0] = '└'
chart[0][-1] = '┐'
chart[-1][-1] = '┘'
for i, task in enumerate(tasks):
chart[-1][i * 2 + 1] = task.name
for row in chart:
print(''.join(row))
tasks = [
Task('Task 1', datetime.date(2022, 1, 1), datetime.date(2022, 1, 5)),
Task('Task 2', datetime.date(2022, 1, 3), datetime.date(2022, 1, 7)),
Task('Task 3', datetime.date(2022, 1, 6), datetime.date(2022, 1, 10)),
Task('Task 4', datetime.date(2022, 1, 8), datetime.date(2022, 1, 12)),
]
gantt_chart(tasks)
```
该代码使用Python的datetime模块来处理日期,使用Task类来表示每个任务的名称、开始日期和结束日期。gantt_chart函数接受一个任务列表作为参数,并生成一个甘特图。甘特图是一个二维列表,其中每个元素表示一个日期和任务的组合。在甘特图中,任务用█字符表示,任务的开始和结束日期用┌、┐、└和┘字符表示,任务的持续时间用─字符表示,任务之间的间隔用空格字符表示。
阅读全文