如果你想暗示运行后,所有的crawl_*
方法的方法,最简单的解决方案可能是设置一个元类,以编程方式为你包装这些方法。开始这个问题,一个简单的包装功能:
import functools
def wrapit(func):
@functools.wraps(func)
def _(self, *args, **kwargs):
func(self, *args, **kwargs)
self.save_to_db()
return _
这是一个包装func
一个基本的装饰,呼吁 self.save_to_db()
调用func
后。现在,我们成立了元类 将编程应用此具体方法:
class Wrapper (type):
def __new__(mcls, name, bases, nmspc):
for attrname, attrval in nmspc.items():
if callable(attrval) and attrname.startswith('crawl_'):
nmspc[attrname] = wrapit(attrval)
return super(Wrapper, mcls).__new__(mcls, name, bases, nmspc)
这将遍历在包装类中的方法,寻找与crawl_
开始 方法名,并与我们的 包装他们装饰者功能。
最后,包裹类本身,它声明Wrapper
作为 元类:
class Wrapped (object):
__metaclass__ = Wrapper
def crawl_1(self):
print 'this is crawl 1'
def crawl_2(self):
print 'this is crawl 2'
def this_is_not_wrapped(self):
print 'this is not wrapped'
def save_to_db(self):
print 'saving to database'
鉴于上述情况,我们得到以下行为:
>>> W = Wrapped()
>>> W.crawl_1()
this is crawl 1
saving to database
>>> W.crawl_2()
this is crawl 2
saving to database
>>> W.this_is_not_wrapped()
this is not wrapped
>>>
你可以看到我们的save_to_database
方法在crawl_1
和crawl_2
(但不是在this_is_not_wrapped
之后)的 之后被调用。
在Python 2.上述作品在Python 3,replase这样:
class Wrapped (object):
__metaclass__ = Wrapper
随着:
class Wrapped (object, metaclass=Wrapper):
你想要什么发生如果'crawl_1 '抛出异常? – RoadieRich