misc-scripts/postoffice/post-office.py
2023-07-02 11:50:07 +02:00

97 lines
1.9 KiB
Python

import csv
import dataclasses
import datetime
import pathlib
import sys
from matplotlib import patches
from matplotlib import pyplot as plt
LOW = 13
HIGH = 15
plt.rcParams["font.family"] = "monospace"
plt.rcParams["font.monospace"] = ["IntelOne Mono"]
@dataclasses.dataclass
class Postbox:
owner: str
x: int
y: int
z: int
postboxes = []
texts = []
with open("data.csv", "rt", newline="") as fr:
for line in csv.reader(fr):
x, y, z, owner = line
postboxes.append(Postbox(owner, int(x), int(y), int(z)))
fig: plt.Figure
ax: plt.Axes
fig, ax = plt.subplots(1, 1)
minx, maxx = float("inf"), -float("inf")
minz, maxz = float("inf"), -float("inf")
legend = {}
for box in postboxes:
if "#" in box.owner:
continue
color = "green"
label = "Available"
if box.owner:
color = "red"
label = "Occupied"
offset = 0
if box.y == HIGH:
offset = 0.5
minx, maxx = min(minx, box.x), max(maxx, box.x)
minz, maxz = min(minz, box.z), max(maxz, box.z)
legend[bool(box.owner)] = ax.add_patch(
patches.Rectangle(
(box.x, box.z + offset), 1, 0.5, fill=True, color=color, label=label
)
)
if box.owner:
texts.append(
ax.annotate(
box.owner,
(box.x + 0.5, box.z + offset + 0.25),
color="black",
ha="center",
va="center",
fontsize=6,
)
)
ax.legend()
plt.grid(True)
minx, minz = minx - 0.5, minz - 0.5
maxx, maxz = maxx + 1.5, maxz + 1.5
ax.set_xlim(minx, maxx)
ax.set_ylim(minz, maxz)
ax.set_aspect("auto")
ax.set_title("Haven Post Office\n" + str(datetime.date.today()))
plt.legend = ax.legend(handles=legend.values(), loc="center")
fig.set_size_inches((31.25, 11.5))
if len(sys.argv) >= 2:
fig.savefig(pathlib.Path().absolute() / sys.argv[1], dpi=300)
else:
plt.show()