Gracefully kill Messenger code with FuncTools

functools.partial

A partial function is one that allows parts of a function to be applied to higher-order functions. Suppose you have a function with many arguments, but only need to change one or two arguments at a time to use the function, you can use partial functions. With partial functions, you can assign any parameter value to a function to fix its parameter number, reducing the need to create another parameter. The result is elegant and easy-to-read code.

The logger function above takes two arguments -log_level and message.

Problem statement – Obviously just keeping all messages logged at the same ‘DEBUG’ level, but unfortunately we are forced to pass log_level with a value of ‘DEBUG’ every time we call the function using the Logger method.

Solution – Using partial functions, we fixed the value of log_level to “DEBUG” and created a more convenient function for it, debug_logger, which now takes message as the only argument and returns the same result.

functools.lru_cache

The feature we’re talking about is definitely one of my favorites. Imagine that you could solve almost 40 to 50 percent of your dynamic programming problems with the ** “lru_cache” ** decorator. LRU stands for “least recently used”, it keeps the result of the last function execution in memory, and when the function must be executed again, it checks the cache first and returns the result if it is found, otherwise the function continues.

When we need to perform computationally intensive or constrained operations with functions, “lru_cache” can save us a lot of time.

Let’s use an example to understand the above concepts

Just apply the “lru_cache” decorator to the function to execute quickly with the above algorithm, so try this method the next time you solve a problem and see for yourself what it really does.

functools.singledispatch

“Singledispatch” allows you to implement function overloading. It converts your function to a generic function, which may behave differently depending on the type of the first argument.

Use the “singleDispatch” decorator on the default implementation, then simply add “@.register ()” to the functions that need to be overloaded.

Let’s look at the code that implements the above concepts in action →

If not for single-pass, then just to implement the code above, otherwise multiple conditions are required. And we’ll have to change the doubleTheValue function every time we need to handle a new type in the future, violating the Open Close principle.

functools.reduce

Reduce is also a higher-order function, which is mainly used to accumulate data → when iterating on the transmitted sequence

reduce(function, sequence, start)
Copy the code

Narrowing the function of the work →

  1. In the first iteration, the provided function is applied to the first element and the starting value of the sequence, and the result is returned to the next iteration.

  2. In the second iteration, the same function is applied to the previously computed result and the next element of the sequence, and the result is updated with the newly computed value.

3. Repeat Step 2 for all subsequent iterations until the sequence is exhausted, and then return the final result.

Reduce always returns a single value.

Let’s look at reducing action →

conclusion

In this article, we learned about the FuncTools module and how it can make our code more readable, elegant, and faster. I hope you feel more comfortable using the FuncTools module after reading this article and becoming familiar with its functions.

Last but not least, you can keep an eye on the editor for daily updates on Python learning experiences and interesting snippets of code.

There is also a small creative learning exchange group below, interested partners can add.