I have a python hash that contains a mapping of function name to function. I want to modify each hash entry to call the associated function BUT then to also call a final custom function. It's to act somewhat like an exit hook.
def original():
print "original work"
becomes
def replacement():
original()
print "notify somebody..."
My issue is I think I'm getting my scoping etc., wrong as the output of the following code is not as expected. Maybe if I could ask is there a better way to do this? I want to stick with modifying the original cb has as it third party code and less places I change the better.
#!/usr/bin/python
def a():
print "a"
def b():
print "b"
def c():
print "c"
orig_fxn_cb = dict()
" basic name to function callback hash "
orig_fxn_cb['a'] = a
orig_fxn_cb['b'] = b
orig_fxn_cb['c'] = c
" for each call back routine in the hash append a final action to it "
def appendFxn(fxn_cb):
appended_fxn_cb_new = dict()
for i in orig_fxn_cb.keys():
cb = fxn_cb[i]
def fxn_tail():
cb()
print cb.__name__, "tail"
appended_fxn_cb_new[i] = fxn_tail
appended_fxn_cb_new[i]()
return appended_fxn_cb_new
" make up a modified callback hash "
xxx = appendFxn(orig_fxn_cb)
print xxx
for i in xxx:
print xxx[i]()
appended_fxn_cb_new[i]()line for?