当前位置: 首页>>代码示例>>Python>>正文


Python OrderedDict.pop方法代码示例

本文整理汇总了Python中synnefo.lib.ordereddict.OrderedDict.pop方法的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict.pop方法的具体用法?Python OrderedDict.pop怎么用?Python OrderedDict.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在synnefo.lib.ordereddict.OrderedDict的用法示例。


在下文中一共展示了OrderedDict.pop方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _resources_catalog

# 需要导入模块: from synnefo.lib.ordereddict import OrderedDict [as 别名]
# 或者: from synnefo.lib.ordereddict.OrderedDict import pop [as 别名]
def _resources_catalog():
    """
    `resource_catalog` contains a list of tuples. Each tuple contains the group
    key the resource is assigned to and resources list of dicts that contain
    resource information.
    `resource_groups` contains information about the groups
    """
    # presentation data
    resources_meta = presentation.RESOURCES
    resource_groups = resources_meta.get('groups', {})
    resource_catalog = ()
    resource_keys = []

    # resources in database
    resource_details = map(lambda obj: model_to_dict(obj, exclude=[]),
                           Resource.objects.all())
    # initialize resource_catalog to contain all group/resource information
    for r in resource_details:
        if not r.get('group') in resource_groups:
            resource_groups[r.get('group')] = {'icon': 'unknown'}

    resource_keys = [r.get('str_repr') for r in resource_details]
    resource_catalog = [[g, filter(lambda r: r.get('group', '') == g,
                                   resource_details)] for g in resource_groups]

    # order groups, also include unknown groups
    groups_order = resources_meta.get('groups_order')
    for g in resource_groups.keys():
        if not g in groups_order:
            groups_order.append(g)

    # order resources, also include unknown resources
    resources_order = resources_meta.get('resources_order')
    for r in resource_keys:
        if not r in resources_order:
            resources_order.append(r)

    # sort catalog groups
    resource_catalog = sorted(resource_catalog,
                              key=lambda g: groups_order.index(g[0]))

    # sort groups
    def groupindex(g):
        return groups_order.index(g[0])
    resource_groups_list = sorted([(k, v) for k, v in resource_groups.items()],
                                  key=groupindex)
    resource_groups = OrderedDict(resource_groups_list)

    # sort resources
    def resourceindex(r):
        return resources_order.index(r['str_repr'])

    for index, group in enumerate(resource_catalog):
        resource_catalog[index][1] = sorted(resource_catalog[index][1],
                                            key=resourceindex)
        if len(resource_catalog[index][1]) == 0:
            resource_catalog.pop(index)
            for gindex, g in enumerate(resource_groups):
                if g[0] == group[0]:
                    resource_groups.pop(gindex)

    # filter out resources which user cannot request in a project application
    for group, resources in list(resource_catalog):
        for resource in resources:
            if not resource.get('ui_visible'):
                resources.remove(resource)

    # cleanup empty groups
    resource_catalog_new = []
    for group, resources in list(resource_catalog):
        if len(resources) == 0:
            resource_groups.pop(group)
        else:
            resource_catalog_new.append((group, resources))
    return resource_catalog_new, resource_groups
开发者ID:antonis-m,项目名称:synnefo,代码行数:77,代码来源:util.py

示例2: AMQPPukaClient

# 需要导入模块: from synnefo.lib.ordereddict import OrderedDict [as 别名]
# 或者: from synnefo.lib.ordereddict.OrderedDict import pop [as 别名]
class AMQPPukaClient(object):
    """
    AMQP generic client implementing most of the basic AMQP operations.

    """
    def __init__(self, hosts=settings.AMQP_HOSTS, max_retries=30,
                 confirms=True, confirm_buffer=100, logger=None):
        """
        Format hosts as "amqp://username:[email protected]:port"
        max_retries=0 defaults to unlimited retries

        """

        self.hosts = hosts
        shuffle(self.hosts)

        self.max_retries = max_retries
        self.confirms = confirms
        self.confirm_buffer = confirm_buffer

        self.connection = None
        self.channel = None
        self.consumers = {}
        self.unacked = OrderedDict()
        self.unsend = OrderedDict()
        self.consume_promises = []
        self.exchanges = []
        if logger:
            self.log = logger
        else:
            logger = logging.getLogger("amqp")
            logging.basicConfig()
            self.log = logger

    def connect(self, retries=0):
        if self.max_retries and retries >= self.max_retries:
            self.log.error("Aborting after %d retries", retries)
            raise AMQPConnectionError('Aborting after %d connection failures.'
                                      % retries)
            return

        # Pick up a host
        host = self.hosts.pop()
        self.hosts.insert(0, host)

        self.client = Client(host, pubacks=self.confirms)

        host = host.split('@')[-1]
        self.log.debug('Connecting to node %s' % host)

        try:
            promise = self.client.connect()
            self.client.wait(promise)
        except socket_error as e:
            if retries < len(self.hosts):
                self.log.warning('Cannot connect to host %s: %s', host, e)
            else:
                self.log.error('Cannot connect to host %s: %s', host, e)
                sleep(1)
            return self.connect(retries + 1)

        self.log.info('Successfully connected to host: %s', host)

        # Setup TCP keepalive option
        self.client.sd.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
        # Keepalive time
        self.client.sd.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 20)
        # Keepalive interval
        self.client.sd.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 2)
        # Keepalive retry
        self.client.sd.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 10)

        self.log.info('Creating channel')

        # Clear consume_promises each time connecting, since they are related
        # to the connection object
        self.consume_promises = []

        if self.unacked:
            self.log.debug("Resending unacked messages from previous"
                           " connection")
            self._resend_unacked_messages()

        if self.unsend:
            self.log.debug("Resending unsent messages from previous"
                           " connection")
            self._resend_unsend_messages()

        if self.exchanges:
            exchanges = self.exchanges
            self.exchanges = []
            for exchange, type in exchanges:
                self.exchange_declare(exchange, type)

        if self.consumers:
            for queue, callback in self.consumers.items():
                self.basic_consume(queue, callback)

    @reconnect_decorator
    def reconnect(self, timeout=None):
#.........这里部分代码省略.........
开发者ID:AthinaB,项目名称:synnefo,代码行数:103,代码来源:amqp_puka.py

示例3: AMQPHaighaClient

# 需要导入模块: from synnefo.lib.ordereddict import OrderedDict [as 别名]
# 或者: from synnefo.lib.ordereddict.OrderedDict import pop [as 别名]
class AMQPHaighaClient():
    def __init__(self, hosts=settings.AMQP_HOSTS, max_retries=30,
                 confirms=True, confirm_buffer=200):
        self.hosts = hosts
        shuffle(self.hosts)

        self.max_retries = max_retries
        self.confirms = confirms
        self.confirm_buffer = confirm_buffer

        self.connection = None
        self.channel = None
        self.consumers = {}
        self.unacked = OrderedDict()

    def connect(self):
        """Initialize a connection with the AMQP server. If it fails, retry at
        most <max_retries> times.
        """

        # Patch gevent
        monkey.patch_all()

        retries = 0
        self.connection = None

        # Try to connect at most <max_retries> times
        while self.max_retries == 0 or retries < self.max_retries:
            retries += 1
            # Pick up a host
            host = self.hosts.pop()
            self.hosts.insert(0, host)
            try:
                self.connection = RabbitConnection(logger=logger, debug=True,
                                                   user='rabbit',
                                                   password='[email protected]',
                                                   vhost='/', host=host,
                                                   heartbeat=None,
                                                   sock_opts=sock_opts,
                                                   transport='gevent')
                break
            except socket.error as e:
                self.connection = None
                logger.error('Cannot connect to host %s: %s', host, e)
                if retries > 2 * len(self.hosts):
                    sleep(1)

        if not self.connection:
            raise AMQPConnectionError('Aborting after %d connection failures!'
                                      % self.max_retries)

        logger.info('Successfully connected to host: %s', host)

        logger.info('Creating channel')
        self.channel = self.connection.channel()

        if self.confirms:
            self._confirm_select()

        if self.unacked:
            self._resend_unacked_messages()

        if self.consumers:
            for queue, callback in self.consumers.items():
                self.basic_consume(queue, callback)

    def exchange_declare(self, exchange, type='direct'):
        """Declare an exchange
        @type exchange_name: string
        @param exchange_name: name of the exchange
        @type exchange_type: string
        @param exhange_type: one of 'direct', 'topic', 'fanout'

        """

        logger.info('Declaring %s exchange: %s', type, exchange)
        self.channel.exchange.declare(exchange, type,
                                      auto_delete=False, durable=True)

    def queue_declare(self, queue, exclusive=False, mirrored=True,
                      mirrored_nodes='all', ttl=None):
        """Declare a queue

        @type queue: string
        @param queue: name of the queue
        @param mirrored: whether the queue will be mirrored to other brokers
        @param mirrored_nodes: the policy for the mirrored queue.
            Available policies:
                - 'all': The queue is mirrored to all nodes and the
                  master node is the one to which the client is
                  connected
                - a list of nodes. The queue will be mirrored only to
                  the specified nodes, and the master will be the
                  first node in the list. Node names must be provided
                  and not host IP. example: [[email protected],[email protected]]
        @type ttl: int
        @param tll: Queue TTL in seconds

        """

#.........这里部分代码省略.........
开发者ID:grnet,项目名称:synnefo,代码行数:103,代码来源:amqp_haigha.py


注:本文中的synnefo.lib.ordereddict.OrderedDict.pop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。