码迷,mamicode.com
首页 > 编程语言 > 详细

Python functools.partial

时间:2014-08-05 15:26:59      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:blog   使用   for   art   ar   代码   div   log   

    def declare_consumer(self, consumer_cls, topic, callback):
        """Create a Consumer using the class that was passed in and
        add it to our list of consumers
        """

        def _connect_error(exc):
            log_info = {‘topic‘: topic, ‘err_str‘: str(exc)}
            LOG.error(_("Failed to declare consumer for topic ‘%(topic)s‘: "
                      "%(err_str)s") % log_info)

        def _declare_consumer():
            consumer = consumer_cls(self.conf, self.channel, topic, callback,
                                    six.next(self.consumer_num))
            self.consumers.append(consumer)
            return consumer

        return self.ensure(_connect_error, _declare_consumer)

    def declare_direct_consumer(self, topic, callback):
        """Create a ‘direct‘ queue.
        In nova‘s use, this is generally a msg_id queue used for
        responses for call/multicall
        """
        self.declare_consumer(DirectConsumer, topic, callback)

    def declare_topic_consumer(self, topic, callback=None, queue_name=None,
                               exchange_name=None):
        """Create a ‘topic‘ consumer."""
        self.declare_consumer(functools.partial(TopicConsumer,
                                                name=queue_name,
                                                exchange_name=exchange_name,
                                                ),
                              topic, callback)

    def declare_fanout_consumer(self, topic, callback):
        """Create a ‘fanout‘ consumer."""
        self.declare_consumer(FanoutConsumer, topic, callback)

functools.partial可以提供对象的默认参数而生成一个新的访问形式,方便统一代码调用形式。以上代码中TopicConsumer类和FanoutConsumer类及DirectConsumer类的构造函数存在差异,如果不使用functools则需要在declare_consumer中进行判断

Python functools.partial,布布扣,bubuko.com

Python functools.partial

标签:blog   使用   for   art   ar   代码   div   log   

原文地址:http://www.cnblogs.com/lailailai/p/3892241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!