Skip to content

Commit 7bd8227

Browse files
Model the mesh and background as dataclasses
Replace the hand-rolled Mesh class and the SimpleNamespace background with dataclasses, and introduce PEP 695 type aliases for the geometry primitives. No behavior change. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 24a9236 commit 7bd8227

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

python315-sampling-profiler/src/pretzel/assets.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
"""Load Pretzel's model and background assets from disk."""
22

3+
from dataclasses import dataclass
34
from importlib.resources import files
4-
from types import SimpleNamespace
55

66
import numpy as np
7+
import numpy.typing as npt
78

89
from pretzel.engine import Mesh
910

1011
ASSETS_DIR = files("pretzel") / "assets"
1112

1213

14+
@dataclass
15+
class Background:
16+
name: str
17+
path: str
18+
pixels: npt.NDArray[np.float32]
19+
20+
1321
def asset_path(name):
1422
return str(ASSETS_DIR / name)
1523

@@ -31,7 +39,7 @@ def load_mesh(name):
3139
def load_background(name, fast=False):
3240
path = asset_path(name)
3341
decode = decode_ppm if fast else decode_ppm_pixel_by_pixel
34-
return SimpleNamespace(name=name, path=path, pixels=decode(path))
42+
return Background(name=name, path=path, pixels=decode(path))
3543

3644

3745
def decode_ppm_pixel_by_pixel(path):

python315-sampling-profiler/src/pretzel/engine.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
"""Pure-Python 3D math: rotation, projection, culling, and sorting."""
22

33
import math
4+
from dataclasses import dataclass
45

6+
type Vertex = tuple[float, float, float]
7+
type Face = tuple[int, int, int]
8+
type Point = tuple[float, float]
9+
type Matrix = tuple[
10+
tuple[float, float, float],
11+
tuple[float, float, float],
12+
tuple[float, float, float],
13+
]
514

15+
16+
@dataclass
617
class Mesh:
7-
def __init__(self, vertices, faces):
8-
self.vertices = vertices
9-
self.faces = faces
18+
vertices: list[Vertex]
19+
faces: list[Face]
1020

1121

1222
def rotation_matrix(pitch, yaw, roll):

0 commit comments

Comments
 (0)