I am building a tool that retrieves the meta descriptions from a web page. Because sites mark up descriptions differently, there are a few different c

Python pattern: Ordered list of lambda functions

submited by
Style Pass
2024-06-06 12:30:03

I am building a tool that retrieves the meta descriptions from a web page. Because sites mark up descriptions differently, there are a few different checks you need to run to ascertain a description.

These checks should be ordered: what a site sets as its description is probably what you want to prioritise over the og:description, since the description attribute is used commonly in web search (and is thus optimised for such by website owners), whereas og: tags are used for social media. There are plenty of other places where such ordered checks come in, especially in web crawling.

For these ordered checks, I like to use an ordered list of lambda functions. Lambda functions defer execution until they are called, which means you can have a list of functions and run them in order, without running all functions. You can accomplish the same behaviour of performing multiple checks with a series of if statements, but such code can get verbose quickly, particularly if you need to perform a dozen or more checks.

The checks are only run when the description = option(page) line of code is run. This is because the checks are lambda functions. The functions are not executed until they are called. Thus, we can run checks in order, and only run the checks we need.

Leave a Comment