| 1 |
from typing import List |
| 2 |
|
| 3 |
try: |
| 4 |
import attr |
| 5 |
except ImportError: |
| 6 |
print("This example requires attrs library") |
| 7 |
print("pip install attrs") |
| 8 |
raise SystemExit() |
| 9 |
|
| 10 |
|
| 11 |
@attr.define |
| 12 |
class Point3D: |
| 13 |
x: float |
| 14 |
y: float |
| 15 |
z: float = 0 |
| 16 |
|
| 17 |
|
| 18 |
@attr.define |
| 19 |
class Triangle: |
| 20 |
point1: Point3D |
| 21 |
point2: Point3D |
| 22 |
point3: Point3D |
| 23 |
|
| 24 |
|
| 25 |
@attr.define |
| 26 |
class Model: |
| 27 |
name: str |
| 28 |
triangles: List[Triangle] = attr.Factory(list) |
| 29 |
|
| 30 |
|
| 31 |
if __name__ == "__main__": |
| 32 |
model = Model( |
| 33 |
name="Alien#1", |
| 34 |
triangles=[ |
| 35 |
Triangle( |
| 36 |
Point3D(x=20, y=50), |
| 37 |
Point3D(x=50, y=15, z=-45.34), |
| 38 |
Point3D(3.1426, 83.2323, -16), |
| 39 |
) |
| 40 |
], |
| 41 |
) |
| 42 |
|
| 43 |
from rich.console import Console |
| 44 |
from rich.pretty import Pretty |
| 45 |
from rich.table import Column, Table |
| 46 |
from rich.text import Text |
| 47 |
|
| 48 |
console = Console() |
| 49 |
|
| 50 |
table = Table("attrs *with* Rich", Column(Text.from_markup("attrs *without* Rich"))) |
| 51 |
|
| 52 |
table.add_row(Pretty(model), repr(model)) |
| 53 |
console.print(table) |