Created
May 24, 2014 01:52
-
-
Save schmohlio/5ad6e4a611254da5f7e3 to your computer and use it in GitHub Desktop.
An error handler for functions that are embedded within parallelized functions
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
""" | |
EmbeddedFunctionHandler.py | |
A class that decorates functions that are embedded/called within | |
other map|reduce functions. Provides error logging | |
to log. Type of error and function *args are logged. Useful for Python's | |
dynamic typing | |
""" | |
import sys | |
import logging | |
import os | |
class EmbeddedFunctionHandler(object): | |
_log_initialized = False | |
def __init__(self, description, max_errors): | |
assert self.__class__._log_initialized == True, "log file not initialized" | |
self.description = description | |
self.max_errors = max_errors | |
@classmethod | |
def init_log(cls, filename, level): | |
if len(logging.getLogger().handlers)>0: | |
logging.warning("handler already exists, ignoring log initialization") | |
cls._log_initialized = True | |
return | |
else: | |
logging.basicConfig(filename=filename, level=level) | |
cls._log_initialized = True | |
def __call__(self, func): | |
def inner(*args): | |
try: | |
return func(*args) | |
except Exception as e: | |
d = dict( description=self.description, | |
function=func.__name__, | |
error=str(type(e)), | |
args = str(*args)) | |
logging.error(d.__repr__()) | |
self.max_errors -= 1 | |
print self.max_errors | |
if self.max_errors==0: | |
msg = "exiting due to max errors on %s"%func.__name__ | |
logging.debug(msg) | |
sys.exit() | |
return None | |
return inner | |
# EXAMPLE | |
if __name__=="__main__": | |
EmbeddedFunctionHandler.init_log('file.log', logging.DEBUG) | |
MAX_ERRORS = 2 | |
@EmbeddedFunctionHandler("catch errors adding 1", MAX_ERRORS) | |
def add_one(x): | |
return x+1 | |
add_one(2) # prints 3 | |
add_one("2") # logs error, returns None | |
add_one("3") # logs error, returns Non | |
add_one("4") # exits | |
#### OR ##### | |
x = [1,2,3,"4","5","6",7] | |
map(add_one,x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment