Reputation: 23
I have a function called transform_exceptions()
that takes a list of functions, then calls each of the functions (functions are without arguments) and the exceptions that occur with the above convention to an object of ExceptionProxy
and finally the list of transformed errors. It returns functions in the same order
Note: If a function is executed without error, an ExceptionProxy
object should be created and its msg value should be "ok!"
Slow quantification
smple:
class ExceptionProxy(Exception):
# define your class here
def transform_exceptions(func_ls):
# implement your function here
def f():
1/0
def g():
pass
tr_ls = transform_exceptions([f, g])
for tr in tr_ls:
print("msg: " + tr.msg + "\nfunction name: " + tr.function.__name__)
Output:
msg: division by zero
function name: f
msg: ok!
function name: g
my code :
from mimetypes import init
class ExceptionProxy(Exception):
def __init__(self, msg, function):
self.msg = msg
self.function = function
def transform_exceptions(func_ls):
exception_list = []
for func in func_ls:
try:
func
except Exception as e:
r = ExceptionProxy(str(e), func)
exception_list.append(r)
else:
r = ExceptionProxy("ok!", func)
exception_list.append(r)
return exception_list
Upvotes: 2
Views: 88
Reputation: 310
You should do this when calling the function name in the list
func()
Also modified code:
class ExceptionProxy(Exception):
def __init__(self,msg,function):
self.msg = msg
self.function = function
def transform_exceptions(func_ls):
out = []
for x in func_ls:
try:
x()
a = ExceptionProxy("ok!", x)
except Exception as e:
a = ExceptionProxy(str(e), x)
out.append(a)
return out
Upvotes: 2