Is there a pythonic way to decouple optional functionality from a function's main purpose?
If you need functionality outside the function to use data from inside the function, then there needs to be some messaging system inside the function to support this. There is no way around this. Local variables in functions are totally isolated from the outside.
The logging module is quite good at setting up a message system. It is not only restricted to printing out the log messages - using custom handlers, you can do anything.
Adding a message system is similar to your callback example, except that the places where the 'callbacks' (logging handlers) are handled can be specified anywhere inside the example_function
(by sending the messages to the logger). Any variables that are needed by the logging handlers can be specified when you send the message (you can still use locals()
, but it is best to explicitly
declare the variables you need).
A new example_function
might look like:
import logging
# Helper function
def send_message(logger, level=logging.DEBUG, **kwargs):
logger.log(level, "", extra=kwargs)
# Your example function with logging information
def example_function(numbers, n_iters):
logger = logging.getLogger("example_function")
# If you have a logging system set up, then we don't want the messages sent here to propagate to the root logger
logger.propagate = False
sum_all = 0
for number in numbers:
send_message(logger, action="processing", number=number)
for i_iter in range(n_iters):
number = number/2
send_message(logger, action="division", i_iter=i_iter, number=number)
sum_all += number
send_message(logger, action="sum", sum=sum_all)
return sum_all
This specifies three locations where the messages could be handled.
On its own, this example_function
will not do anything other than the functionality of the example_function
itself.
It will not print out anything, or do any other functionality.
To add extra functionality to the example_function
, then you will need to add handlers to the logger.
For example, if you want to do some printing out of the sent variables (similar to your debugging
example), then you define
the custom handler, and add it to the example_function
logger:
class ExampleFunctionPrinter(logging.Handler):
def emit(self, record):
if record.action == "processing":
print("Processing number {}".format(record.number))
elif record.action == "division":
print(record.number)
elif record.action == "sum":
print("sum_all: {}".format(record.sum))
example_function_logger = logging.getLogger("example_function")
example_function_logger.setLevel(logging.DEBUG)
example_function_logger.addHandler(ExampleFunctionPrinter())
If you want to plot the results on a graph, then just define another handler:
class ExampleFunctionDivisionGrapher(logging.Handler):
def __init__(self, grapher):
self.grapher = grapher
def emit(self, record):
if record.action == "division":
self.grapher.plot_point(x=record.i_iter, y=record.number)
example_function_logger = logging.getLogger("example_function")
example_function_logger.setLevel(logging.DEBUG)
example_function_logger.addHandler(
ExampleFunctionDivisionGrapher(MyFancyGrapherClass())
)
You can define and add in whatever handlers you want. They will be totally separate from the functionality of the example_function
,
and can only use the variables that the example_function
gives them.
Although logging can be used as a messaging system, it might be better to move to a fully fledged messaging system, such as PyPubSub, so that it doesn't interfere with any actual logging that you might be doing:
from pubsub import pub
# Your example function
def example_function(numbers, n_iters):
sum_all = 0
for number in numbers:
pub.sendMessage("example_function.processing", number=number)
for i_iter in range(n_iters):
number = number/2
pub.sendMessage("example_function.division", i_iter=i_iter, number=number)
sum_all += number
pub.sendMessage("example_function.sum", sum=sum_all)
return sum_all
# If you need extra functionality added in, then subscribe to the messages.
# Otherwise nothing will happen, other than the normal example_function functionality.
def handle_example_function_processing(number):
print("Processing number {}".format(number))
def handle_example_function_division(i_iter, number):
print(number)
def handle_example_function_sum(sum):
print("sum_all: {}".format(sum))
pub.subscribe(
"example_function.processing",
handle_example_function_processing
)
pub.subscribe(
"example_function.division",
handle_example_function_division
)
pub.subscribe(
"example_function.sum",
handle_example_function_sum
)