Last active
October 6, 2017 16:59
-
-
Save saintsGrad15/ab2832f48d785ef65cbbf9fd6a0ad436 to your computer and use it in GitHub Desktop.
A Heartbeater offspring that reports periodically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Reporter(Heartbeater): | |
""" | |
Behaves like a Heartbeater but specifically designed to print statements periodically. | |
""" | |
def __init__(self, report_string, period=1, reporting_function=None): | |
""" | |
Every 'period' seconds 'report_string' will be passed to 'reporting_function.' | |
If 'reporting_function' is None it will print 'report_string' instead. | |
:param report_string: The string to report. | |
:param period: The number of seconds to wait between reports. | |
:param reporting_function: The reporting function to use instead of 'print.' | |
This could be a logging function like 'logging.debug' for instance. | |
This must be capable of taking no more than one string argument. | |
""" | |
Heartbeater.__init__(self, work=self.report, period=period) | |
self.report_string = report_string | |
self.period = period | |
self.reporting_function = reporting_function | |
def report(self): | |
""" | |
The function that is called to "report." | |
:return: None | |
""" | |
if self.reporting_function is None: | |
print self.report_string | |
else: | |
self.reporting_function(self.report_string) | |
def __call__(self, function_): | |
self.function = function_ | |
def _call_it(*args, **kwargs): | |
with Reporter(report_string=self.report_string, period=self.period, reporting_function=self.reporting_function): | |
self.function(*args, **kwargs) | |
return _call_it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment