Python's functools module provides three interesting functions: partial, update_wrapper, and wraps.
Partial function, which can re-bind the optional parameters of the function to generate a callable partial object.
The update_wrapper function copies the _ name _, _ module _, _ Doc _, and _ dict _ of encapsulated functions to the encapsulation function.
The wraps function further encapsulates update_wrapper.
You can use the wraps function to implement a simple method blocking mechanism to implement your own exceptioncallhandler,
Specific implementation:
#-*-Coding: UTF-8-*-# features # Name: module 2 # purpose: # Author: ankier # created: 22-12-2012 # copyright: (c) ankier 2012 # licence: <2012 ~ 2020> # Export Import timefrom functools import wraps_cache ={} def predictioncallhandler (): def _ predictioncallhandler (fun): @ wraps (fun) def wrap (ARGs, kW ): glos = fun. func_globals package = Glos ['_ package _'] model = Glos ['_ name _'] methodname = fun. func_name result = none try: Result = fun (ARGs, kW) failed t exception, E: Print 'package: ', package,', model: ', model,', methodname: ', methodname ,'. ', e return result return wrap return _ exceptioncallhandler
#-*- coding: UTF-8 -*-import timefrom exceptionCallHandler import ExceptionCallHandler@ExceptionCallHandler()def Sum(xx , yy ): raise Exception("Exception error information!") sum = xx + yy print '------sum----- ' time.sleep(10) return sumprint Sum(4, 5)print Sum(4, 5)
Running effect:
Package: None, model: mainframe, methodname: sum. Exception error information!
Package: None, model: mainframe, methodname: sum. Exception error information!