Implement Geometric Primitives
00:00 Implement Geometric Primitives. Create a new Python package in your project with these three placeholder modules in it. You’ll cover each of these modules in more detail in the next few sections, starting with the geometric primitives in this section.
00:20 Primitives are the basic shapes—such as points, lines, rectangles, or polygons—that you’ll build your maze with. Each primitive has only one responsibility, to draw itself by providing the corresponding XML representation according to SVG semantics.
Because SVG element attributes, such as
stroke-width, can contain hyphens, which aren’t valid Python names, you’ll automatically replace underscores with hyphens so that you can pass them as function arguments. If an element has no value, then you’ll use the XML self-closing tag.
Because protocols are about the interface rather than implementation, it’s common to find either the
pass statement or the ellipsis literal (
...) in their method bodies. Both work as a placeholder to silence the Python interpreter, which requires that every block of code starting with a colon must not be empty.
The class extends a named tuple, but not your
Primitive protocol. It’s sufficient that it implements the
.draw() method, which returns an SVG point as a Python string, to define a concrete primitive. Later, you’ll need to translate your points in x and y directions, so you also implement a relevant method.
This time, the
.draw() method returns an SVG line, which is a stand-alone element rather than the value of an attribute. Now that you have the foundations of the SVG renderer in place, in the next section, you’ll see how to add more complex primitives to your code.
Become a Member to join the conversation.