链接装饰

时间:2017-10-23 17:20:10

标签: python

我正在尝试链接两个装饰器。但有些人最终只能运行一个。可以有人建议我解决这个问题

def decorator_redis(func_name):
    def redis_decorator(func):
        @wraps(func)
        def redis_wrapper(*args):
            redis_utilty = RedisUtilties()
            entity_id = args[0]
            return 'hello' + entity_id
        return redis_wrapper
    return redis_decorator

def decorator_ES(func_name):
    def ES_decorator(func):
        @wraps(func)
        def ES_wrapper(*args):
            return args[0]+ '!'
        return ES_wrapper
    return ES_decorator

@decorator_redis('find')
@decorator_ES('find')
def fetch_entity(name):
    return

print(fetch_entity('John'))

我需要打印hello John!。但是能够打印出hello JohnJohn !,具体取决于我单独使用的装饰器。

1 个答案:

答案 0 :(得分:0)

你应该在装饰器的包装器中调用装饰函数(每个装饰):

var allItems = [
    { id: 1, name: 'item1' },
    { id: 2, name: 'item2' },
    { id: 3, name: 'item3' }
];

var fewItems = [
    { id: 2, name: 'item2' }
];


var props = ['id', 'name'];
var result = allItems.filter(function(o1){
     return !fewItems.some(function(o2){
        return o1.id === o2.id;        
    });
}).map(function(o){
     return props.reduce(function(newo, name){
        newo[name] = o[name];
        return newo;
    }, {});
});

console.log(result);

另一个装饰者也一样。

否则,您可以使用最新(最顶层)包装器替换函数。所以,当你调用它时,它永远不会到达底部装饰器或函数本身。