1
0

Handy new class PrintError

Saves adding "def print_error" to endless classes.
This commit is contained in:
Neil Booth
2015-09-06 21:40:00 +09:00
parent 735a9e9a29
commit 93b99ebded
5 changed files with 32 additions and 39 deletions

View File

@@ -25,22 +25,27 @@ class MyEncoder(json.JSONEncoder):
return obj.as_dict()
return super(MyEncoder, self).default(obj)
class ThreadJob:
class PrintError:
'''A handy base class'''
def diagnostic_name(self):
return self.__class__.__name__
def print_error(self, *msg):
print_error("[%s]" % self.diagnostic_name(), *msg)
def print_msg(self, *msg):
print_msg("[%s]" % self.diagnostic_name(), *msg)
class ThreadJob(PrintError):
"""A job that is run periodically from a thread's main loop. run() is
called from that thread's context.
"""
def print_error(self, *msg):
print_error("[%s]" % self.__class__.__name__, *msg)
def print_msg(self, *msg):
print_msg("[%s]" % self.__class__.__name__, *msg)
def run(self):
"""Called periodically from the thread"""
pass
class DaemonThread(threading.Thread):
class DaemonThread(threading.Thread, PrintError):
""" daemon thread that terminates cleanly """
def __init__(self):
@@ -84,12 +89,6 @@ class DaemonThread(threading.Thread):
with self.running_lock:
self.running = False
def print_error(self, *msg):
print_error("[%s]" % self.__class__.__name__, *msg)
def print_msg(self, *msg):
print_msg("[%s]" % self.__class__.__name__, *msg)
is_verbose = False