이더넷 물리 구성 문서의 그림 생성 코드다. if __name__ == "__main__": 아래가 자체 테스트다.
# 이더넷 물리 구성 문서의 그림을 만든다: 64_ethernet-physical_fig1.svg, fig2.svg
# fig1 고전 이더넷(10Base5): 500 m 세그먼트를 중계기로 이은 모습과, 가장 먼 두 호스트 사이의 경로
# fig2 허브와 꼬임선(10BaseT): 밖에서는 별 모양, 허브 안에는 버스가 들어 있다
# 실행: ~/.venvs/vault-plots/bin/python -I 64_ethernet-physical_plot.py (matplotlib, numpy 필요)
import os
import matplotlib
matplotlib.use("svg")
import matplotlib.pyplot as plt
from matplotlib import font_manager
from matplotlib.patches import FancyBboxPatch, Rectangle
HERE = os.path.dirname(os.path.abspath(__file__))
PREFIX = "64_ethernet-physical"
INK = "#888888"
C = ["#3b82c4", "#d9622b", "#3a9a5b", "#9b59b6", "#c49a1b"]
BLUE, ORANGE, GREEN, PURPLE, GOLD = C
_fonts = {f.name for f in font_manager.fontManager.ttflist}
FONT = next((n for n in ["Apple SD Gothic Neo", "NanumGothic", "Noto Sans CJK KR"] if n in _fonts), "DejaVu Sans")
plt.rcParams.update({
"font.family": FONT, "font.size": 10.5, "axes.unicode_minus": False, "svg.fonttype": "path",
"svg.hashsalt": PREFIX, "text.color": INK,
"axes.facecolor": "none", "figure.facecolor": "none", "savefig.transparent": True,
})
SEG_M = 500
MAX_REPEATERS = 4
def save(fig, k):
fig.savefig(os.path.join(HERE, f"{PREFIX}_fig{k}.svg"), bbox_inches="tight", metadata={"Date": None})
if os.environ.get("PREVIEW_DIR"):
fig.savefig(os.path.join(os.environ["PREVIEW_DIR"], f"{PREFIX}_fig{k}.png"), bbox_inches="tight", dpi=110,
facecolor="white")
plt.close(fig)
def host(ax, x, y, color=INK, label=None):
ax.add_patch(Rectangle((x - 0.17, y - 0.17), 0.34, 0.34, facecolor="none", edgecolor=color, lw=1.2))
if label:
ax.text(x, y, label, ha="center", va="center", fontsize=10, color=color)
def repeater(ax, x, y):
ax.add_patch(Rectangle((x - 0.25, y - 0.2), 0.5, 0.4, facecolor=ORANGE, edgecolor=ORANGE, alpha=0.35, lw=1.2))
ax.text(x, y, "R", ha="center", va="center", fontsize=10, color=ORANGE)
def fig1():
"""세그먼트 다섯 개를 계단처럼 놓고 중계기 네 개로 잇는다."""
fig, ax = plt.subplots(figsize=(7.6, 4.2))
n_seg = MAX_REPEATERS + 1
total = 0
for i in range(n_seg):
y = -i * 1.0
x0, x1 = i * 1.5, i * 1.5 + 3.0
ax.plot([x0, x1], [y, y], color=BLUE, lw=2.4)
ax.text(x1 + 0.1, y, f"세그먼트 {i + 1}: 최대 {SEG_M} m", va="center", fontsize=10, color=BLUE)
for hx in (x0 + 1.7, x0 + 2.3):
ax.plot([hx, hx], [y, y + 0.32], color=INK, lw=1)
host(ax, hx, y + 0.49)
total += SEG_M
if i < n_seg - 1:
rx = x0 + 2.6
ax.plot([rx, rx], [y, y - 1.0], color=ORANGE, lw=1.2)
repeater(ax, rx, y - 0.5)
# 가장 먼 두 호스트
host(ax, 0.3, 0.49, GREEN, "A")
ax.plot([0.3, 0.3], [0, 0.32], color=GREEN, lw=1.2)
last_x = (n_seg - 1) * 1.5 + 2.9
host(ax, last_x, -(n_seg - 1) + 0.49, GREEN, "B")
ax.plot([last_x, last_x], [-(n_seg - 1), -(n_seg - 1) + 0.32], color=GREEN, lw=1.2)
ax.text(0.0, -4.85, f"A에서 B까지: 세그먼트 {n_seg}개 · 중계기 {MAX_REPEATERS}개 · 선 길이 최대 {total:,} m",
fontsize=10.5, color=GREEN)
ax.set_xlim(-0.3, 11.2)
ax.set_ylim(-5.1, 1.0)
ax.axis("off")
save(fig, 1)
return n_seg, total
def fig2():
fig, ax = plt.subplots(figsize=(7.6, 4.0))
top = (3.6, 2.6)
labs = [(1.4, 1.2), (5.8, 1.2)]
def hub(x, y, name):
ax.add_patch(FancyBboxPatch((x - 0.6, y - 0.22), 1.2, 0.44, boxstyle="round,pad=0.02",
facecolor="none", edgecolor=PURPLE, lw=1.4))
ax.plot([x - 0.45, x + 0.45], [y, y], color=BLUE, lw=2.2) # 허브 안의 버스
ax.text(x + 0.7, y + 0.05, name, va="center", fontsize=10, color=PURPLE)
hub(*top, "학과 허브")
for i, (x, y) in enumerate(labs):
ax.plot([top[0], x], [top[1] - 0.22, y + 0.22], color=INK, lw=1.2)
hub(x, y, f"실험실 {i + 1} 허브")
for j, hx in enumerate((x - 0.9, x - 0.3, x + 0.3, x + 0.9)):
ax.plot([x, hx], [y - 0.22, 0.17], color=INK, lw=1)
host(ax, hx, 0.0)
ax.text(0.2, -0.65, "선은 별 모양(허브마다 꼬임선 한 가닥씩, 최대 100 m)으로 뻗는다.", fontsize=10)
ax.text(0.2, -1.05, "파란 줄: 허브 안에서는 모든 선이 버스 하나에 이어져 있다.", fontsize=10, color=BLUE)
ax.set_xlim(-0.2, 7.8)
ax.set_ylim(-1.3, 3.1)
ax.axis("off")
save(fig, 2)
if __name__ == "__main__":
n_seg, total = fig1()
assert n_seg == 5 and total == 2500 # 중계기 4개 -> 세그먼트 5개 -> 2,500 m
fig2()
print("ALL CHECKS PASSED")