The Pimp my library pattern (PMLP)

Quickly decorate your classes with additional methods and features

Picture source: wikipedia (taken by Marcus Hansson)

When working on projects suddenly the need may arise to add additional methods and/or features to a pre-existing class. Here are two reasons why I do use the pimp my library pattern (PMLP):

  • Testing. You want to test something out without messing around in a library’s code prematurely (especially if the class is part of an external project or library)
  • Context-specificity. if the methods/attributes you are about to implement are context specific and they are unlikely to be necessary outside your piece of work the PMLP can help you to not bloat the contents of the library want to pimp

Pimp My library Pattern: a toy example.

Let’s assume you need a custom print function that we call highlight which we want to use as extension for the scala string.

def highlight(s: String): String = s"<< $s >>"

Now you want this function to be available for any string within you code.

Listing 1:

class GreatWork extends RecentGreatWork {
 def do(s: String): String = {...}
 def doAndHighlight(s: String): String = {
  val greatStuff: String = do(s)

To make the highlights function available, you define a new class. We are going to call it StringHelperFunctions.

Listing 2:

class StringHelperFunctions(s: String) { 
  def highlight: String = s"<< $s >>"

Furthermore you add an implicit conversion function to your package object which looks like this:

Listing 3:

implicit def withStringHelperFunctions(s: String): StringHelperFunctions = 
  new StringHelperFunctions(s)

Now any function you add to the StringHelperFunctions class will be available to any String within the context of your package and so it will in line 5 of the first Listing 1 of this article.

How we apply the PMLP for data science specific tasks.

I just recently used the PMLP for experimental implementation of predictors.

We at Akanoo predict user behavior. We track click-stream data of customers of our clients’ web shops. This data is made available to our models by classes, one is called Visit. This class represents a single visit on a customer’s web site and contains functions that are used all over the place in our components.

Now, if I try to predict a new kind of behavior, like e. g. a visitor is likely (or unlikely) to visit or re-visit a specific product category I can think of predictors that relate to that behavior.

Quite often, the implementation of these predictors profit from new functions that could reside in the Visit class. But:

  • Testing. The Visit class is part of an external project. For every change to the helper functions there I would have to modify, test, build and package the project in order to use it. If I just use the PMLP I can just concentrate on projects code that do contain my experimental predictors.
  • Context-Specificity. My new functions are only used specifically for the predictors I am coming up with adding these functions to Visit would bloat up its contents unnecessarily and would cause hassle in the long run

I hope that gave you a blink of an idea how you also could use the PMLP

I would be happy to hear from you:

  • Did this article helped you and why (not) ?
  • How do you use the Pimp my Library Pattern?
  • What other patterns do you use an think are valuable

Happy Coding and kind greetings from

– Senior Data Scientist @ Akanoo