Source for: lesson1.py [raw]

 1class Box():
 2
 3    def __init__(self, x=0, y=0, w=1, h=1):
 4        """Accept arguments to define our box, and store them."""
 5        self.x = x
 6        self.y = y
 7        self.w = w
 8        self.h = h
 9
10    def __repr__(self):
11        return "Box(%s, %s, %s, %s)" % (self.x, self.y, self.w, self.h)
12
13
14many_boxes = [Box() for i in range(5000)]
15
16import svgwrite
17
18
19def draw_boxes(boxes, fname, size):
20    dwg = svgwrite.Drawing(fname, profile="full", size=size)
21    # Draw all the boxes
22    for box in boxes:
23        dwg.add(
24            dwg.rect(
25                insert=(f"{box.x}cm", f"{box.y}cm"),
26                size=(f"{box.w}cm", f"{box.h}cm"),
27                fill="red",
28            )
29        )
30    dwg.save()
31
32
33draw_boxes(many_boxes, "lesson1.svg", ("5cm", "2cm"))
34