0

I've been using different ad-hoc variations on this, especially in numpy / pandas / data science-y applications. For example let's say I've done some intensive processing that outputs a numpy array and there's probably no need to redo that part of the code. While debugging I want to rerun the script but avoid that processing. So I dump that array to a file and then wrap that part of the code in some form of "if the dumped file exists and it's ok, load it, else run the processing again, and save that output". The idea is to make the script behave a bit like a jupyter notebook.

I know it's pretty simple to write something like that most of the time, I was just wondering if there's an established pattern for how people do it so that I look like I know what I'm doing. Try-block? Decorators? Context managers? Commenting and uncommenting the same 5 lines of code like a psychopath?

Thanks for any help.

1
  • If you are searching a simple solution, you might be interested in the Python Joblib package. They call it memoize pattern.
    – pschill
    Commented Mar 31, 2021 at 17:09

2 Answers 2

4

Design Patterns are well-known solutions to certain specific problems.

The most well-known of these patterns are the Gang of Four patterns documented in the book "Design Patterns: Elements of Reusable Object-Oriented Software," which essentially describes certain inadequacies in object-oriented programming languages like C++ and Java, and the means that the authors invented to "work around" them.

Software Patterns are not:

  1. A comprehensive problem/solution catalog,
  2. An established body of coding standards, or
  3. A track to follow for writing a software application.

Consequently, the most common answer to the question "is there a design pattern for this" is "no."

Most people use design patterns the wrong way. They have a problem, and they start looking for a design pattern to solve it. This is backwards. The correct way to use design patterns is to study them: get to know them so that, when you arrive at a problem that one of the design patterns solves, you can then identify the pattern and make use of it.

3
  • Thank you, your assumption about me thinking of patterns the wrong way was absolutely correct, this taught me more than I hoped for.
    – theo2434
    Commented Apr 1, 2021 at 10:06
  • @theo2434: I think it was also the Gang of Four book that describes design patterns as something discovered after many different people separated by time and space encounter the same problem and coincidentally come to similar solutions. The design pattern is a name used to facilitate communication — a mental shortcut, if you will. Commented May 10, 2021 at 2:07
  • @GregBurghardt: Indeed. Once a team knows the patterns, they have a new vocabulary. Commented May 10, 2021 at 2:29
3

I don't know of any special name for it. But, "just do it." This sort of thing is done all the time.

I once had a script that had to process about 155,000 files scattered over about twenty directories, and it took a significant amount of time to "find them" each time. So, I simply dumped the list of filenames to a file and reloaded it each time ... using a command-line option to cause it to repeat the scan when needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.