Design Patterns with Python for Machine Learning Engineers: Template Method

Photo by Pawel Czerwinski on Unsplash

Learn how to use the Template design pattern to enhance your code

4 min read

12 hours ago

Introduction

Recently I’ve been working on the domain-specific fine-tuning of several LLMs. The first and maybe the most important part of this task is to collect, scrape, and clean textual data to feed the LLM. I noticed that my code was becoming messy with many repetitions, because for every identified source I was writing a script from scratch which had a lot of things in common with other scripts in my codebase. I was not following the “Don’t repeat yourself” (DRY) principle at all. This is why I decided to implement the Template Design Pattern and make my code base more elegant and efficient.

The Template Design Pattern

I won’t repeat here what a design pattern is and how we classify design patterns based on their functionalities, since I’ve written many articles on the subject. If you are interested in reading my previous articles on this topic I will leave some references at the end.

In this article, I will show you an example related to data processing. Let’s say that in our project we have to deal with different kinds of data that we want to analyze. Some of these data are…