Match
Last updated
Last updated
Let's take another look at our example from the previous lesson:
Python has a match
statement that tends to be a lot cleaner than a series of if/else/elif
statements when we're working with a fixed set of possible values (like a sum type, or more specifically an enum):
If you have two values to match, you can use a tuple
:
The value we want to compare is set after the match
keyword, which is then compared against different cases/patterns. If a match is found, the code in the block is executed.
Complete the convert_format
function. Using the enum DocFormat
, it should support 3 types of conversions:
From MD
to HTML
:
Assume the content is a single h1
tag in markdown syntax - it's a single string representing a line. Replace the leading #
with an <h1>
and add a </h1>
to the end.
# This is a heading
-> <h1>This is a heading</h1>
From TXT
to PDF
:
Simply add a [PDF]
tag to the beginning and end of the content. Notice the spaces between [PDF]
tags and the content:
This is some text
-> [PDF] This is some text [PDF]
From HTML
to MD
:
Replace any <h1>
tags with #
and remove any </h1>
tags.
<h1>This is a heading</h1>
-> # This is a heading
Any other conversion:
If the input format is invalid, raise an Exception
with the string invalid type
The solution works because it correctly applies pattern matching (match-case
) to handle different document format conversions while ensuring proper transformations for MD -> HTML
, TXT -> PDF
, and HTML -> MD
. Hereβs a breakdown of why itβs correct:
DocFormat
Enum Ensures Valid InputsEnum
restricts valid formats to PDF, TXT, MD, and HTML, ensuring that convert_format
only works with these predefined document types.
This makes checking from_format
and to_format
more structured and type-safe.
This is a tuple pattern match, which checks the (from_format, to_format)
pair.
MD
) to HTML (HTML
)Transformation Logic:
Markdown # This is a heading
should become <h1>This is a heading</h1>
.
lstrip("# ")
removes the leading #
from the Markdown heading.
Wraps the remaining content in <h1>
and </h1>
.
β Example:
TXT
) to PDF (PDF
)Transformation Logic:
The input text needs to be enclosed with [PDF]
and [PDF]
(including spaces).
β Example:
HTML
) to Markdown (MD
)Transformation Logic:
Removes <h1>
and </h1>
tags.
Adds #
at the beginning to convert back to Markdown.
β Example:
If none of the valid cases match, an exception is raised.
This prevents incorrect conversions (e.g., MD -> PDF
or TXT -> HTML
).
β Example:
β 1. Pattern Matching is Clean and Readable
The match-case
structure avoids multiple if-elif
checks and makes the conversion logic explicit.
β 2. Enum Ensures Strict Format Matching
No risk of passing random strings like "Markdown"
or "pdf"
, since DocFormat
enforces valid values.
β 3. Proper String Transformations
Uses lstrip()
and rstrip()
correctly for Markdown/HTML transformations.
Adds [PDF]
tags with proper spacing.
β 4. Error Handling is Explicit
If an invalid format conversion is attempted, an exception is raised instead of returning incorrect output.