Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

00:38 To help generate SVG elements, you are going to devise a generic function that will spit out an XML tag with a given name, optional value, and zero or more attributes.

01:05 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.

01:35 On-screen are a few examples illustrating the use of this function. The element name is the first and only required parameter of the function. The second parameter is the optional value.

01:57 Notice that both are positional arguments, while element attributes are variable-length keyword arguments. You can add a name and attributes …

02:14 a name, value, and attributes … and you can also nest elements by using the output of one call to the function as the input for another.

02:39 To take advantage of static duck typing, which your type checker tool can leverage, you’ll define a protocol or an interface common to all geometric primitives.

02:54 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.

03:13 The type checker will consider any class implementing the .draw() method with this signature as a subtype of Primitive, even if they’re unrelated through inheritance.

03:22 The most basic primitive is a Euclidian point comprising the x and y coordinates.

03:40 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.

04:01 Because named tuples are immutable, translating a point creates a brand-new object. The next most basic primitive is a line segment delimited by starting and ending points.

04:25 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.