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


Python tools.reverse_enumerate函数代码示例

本文整理汇总了Python中tools.reverse_enumerate函数的典型用法代码示例。如果您正苦于以下问题:Python reverse_enumerate函数的具体用法?Python reverse_enumerate怎么用?Python reverse_enumerate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: borrow

    def borrow(self, dsn):
        self._debug('Borrow connection to %s' % (dsn,))

        # free leaked connections
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                self._debug('Free leaked connection to %s' % (cnx.dsn,))

        for i, (cnx, used) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                self._connections.pop(i)
                self._connections.append((cnx, True))
                self._debug('Existing connection found at index %d' % i)

                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    self._debug('Removing old connection at index %d: %s' % (i, cnx.dsn))
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')

        result = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
        self._connections.append((result, True))
        self._debug('Create new connection')
        return result
开发者ID:ascetic85,项目名称:ziwei,代码行数:34,代码来源:sql_db.py

示例2: borrow

    def borrow(self, dsn):
        self._debug('Borrow connection to %r', dsn)

        # free leaked connections
        for i, (cnx, _, t) in tools.reverse_enumerate(self._connections):
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False, t))
                self.__logger.warn('%r: Free leaked connection to %r', self, cnx.dsn)

        for i, (cnx, used, t) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                self._connections.pop(i)
                if time.time() - t > 3600:
                    cnx.close()
                    try:
                        cnx = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
                        t = time.time()
                    except psycopg2.Error, e:
                        self.__logger.exception('Connection to the database failed')
                        raise
                self._connections.append((cnx, True, t))
                self._debug('Existing connection found at index %d', i)

                return cnx
开发者ID:hectord,项目名称:unifield,代码行数:26,代码来源:sql_db.py

示例3: get_weights

    def get_weights(self, item, question):
        """Returns weights of previous answers to the given item.

        :param item: *Item* (i.e. practiced place by a user).
        :type item: :class:`Item`
        :param question: Asked question.
        :type question: :class:`pandas.Series` or :class:`Question`
        """
        correct_weights = [
            ans.is_correct * self.decay ** k for k, ans
            in tools.reverse_enumerate(item.practices)
        ]
        incorrect_weights = [
            (1 - ans.is_correct) * self.decay ** k for k, ans
            in tools.reverse_enumerate(item.practices)
        ]
        return sum(correct_weights), sum(incorrect_weights)
开发者ID:paveldedik,项目名称:thesis,代码行数:17,代码来源:models.py

示例4: borrow

    def borrow(self, connection_info):
        """
        :param dict connection_info: dict of psql connection keywords
        :rtype: PsycoConnection
        """
        # free dead and leaked connections
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if cnx.closed:
                self._connections.pop(i)
                self._debug('Removing closed connection at index %d: %r', i, cnx.dsn)
                continue
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                _logger.info('%r: Free leaked connection to %r', self, cnx.dsn)

        for i, (cnx, used) in enumerate(self._connections):
            if not used and cnx._original_dsn == connection_info:
                try:
                    cnx.reset()
                except psycopg2.OperationalError:
                    self._debug('Cannot reset connection at index %d: %r', i, cnx.dsn)
                    # psycopg2 2.4.4 and earlier do not allow closing a closed connection
                    if not cnx.closed:
                        cnx.close()
                    continue
                self._connections.pop(i)
                self._connections.append((cnx, True))
                self._debug('Borrow existing connection to %r at index %d', cnx.dsn, i)

                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    if not cnx.closed:
                        cnx.close()
                    self._debug('Removing old connection at index %d: %r', i, cnx.dsn)
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')

        try:
            result = psycopg2.connect(
                connection_factory=PsycoConnection,
                **connection_info)
        except psycopg2.Error:
            _logger.info('Connection to the database failed')
            raise
        result._original_dsn = connection_info
        self._connections.append((result, True))
        self._debug('Create new connection')
        return result
开发者ID:474416133,项目名称:odoo,代码行数:57,代码来源:sql_db.py

示例5: close_all

 def close_all(self, dsn=None):
     count = 0
     last = None
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn is None or cnx._original_dsn == dsn:
             cnx.close()
             last = self._connections.pop(i)[0]
             count += 1
     _logger.info('%r: Closed %d connections %s', self, count,
                 (dsn and last and 'to %r' % last.dsn) or '')
开发者ID:474416133,项目名称:odoo,代码行数:10,代码来源:sql_db.py

示例6: borrow

    def borrow(self, dsn):
        self._debug('Borrow connection to %r', dsn)

        # free dead and leaked connections
        time_now = time.time()
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
            if (cnx.closed or (not cnx.executing and time_now - cnx.init_time > CONNECTION_LIFETIME) or (cnx.executing and CURSOR_TIMEOUT and time_now - cnx.last_execution_time > CURSOR_TIMEOUT)):
                self._connections.pop(i)
                self._debug('Removing closed connection at index %d: %r', i, cnx.dsn)
                if not cnx.closed:
                    cnx.close() # Close unclosed connection.
                continue
            if getattr(cnx, 'leaked', False):
                delattr(cnx, 'leaked')
                self._connections.pop(i)
                self._connections.append((cnx, False))
                _logger.warning('%r: Free leaked connection to %r', self, cnx.dsn)

        for i, (cnx, used) in enumerate(self._connections):
            if not used and dsn_are_equals(cnx.dsn, dsn):
                self._connections.pop(i)
                try:
                    cnx.reset()
                except psycopg2.OperationalError:
                    self._debug('Cannot reset connection at index %d: %r, removing it', i, cnx.dsn)
                    # psycopg2 2.4.4 and earlier do not allow closing a closed connection
                    if not cnx.closed:
                        cnx.close()
                    continue
                self._connections.append((cnx, True))
                self._debug('Existing connection found at index %d', i)
                return cnx

        if len(self._connections) >= self._maxconn:
            # try to remove the oldest connection not used
            for i, (cnx, used) in enumerate(self._connections):
                if not used:
                    self._connections.pop(i)
                    cnx.close()
                    self._debug('Removing old connection at index %d: %r', i, cnx.dsn)
                    break
            else:
                # note: this code is called only if the for loop has completed (no break)
                raise PoolError('The Connection Pool Is Full')
        try:
            result = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
            result.init_time = time.time()
            result.executing = False
        except psycopg2.Error:
            _logger.exception('Connection to the database failed')
            raise
        self._connections.append((result, True))
        self._debug('Create new connection')
        return result
开发者ID:gkliska,项目名称:enapps-openerp-server,代码行数:54,代码来源:sql_db.py

示例7: to_sql

    def to_sql(self):
        stack = []
        params = []
        for i, e in reverse_enumerate(self.__exp):
            if self._is_leaf(e, internal=True):
                table = self.__tables.get(i, self.__main_table)
                q, p = self.__leaf_to_sql(e, table)
                params.insert(0, p)
                stack.append(q)
            else:
                if e == '!':
                    stack.append('(NOT (%s))' % (stack.pop(),))
                else:
                    ops = {'&': ' AND ', '|': ' OR '}
                    q1 = stack.pop()
                    q2 = stack.pop()
                    stack.append('(%s %s %s)' % (q1, ops[e], q2,))

        query = ' AND '.join(reversed(stack))
        joins = ' AND '.join(map(lambda j: j[0], self.__joins))
        if joins:
            query = '(%s) AND (%s)' % (joins, query)
        return (query, flatten(params))
开发者ID:ascetic85,项目名称:ziwei,代码行数:23,代码来源:expression.py

示例8: close_all

 def close_all(self, dsn):
     self._debug('Close all connections to %s' % (dsn,))
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn_are_equals(cnx.dsn, dsn):
             cnx.close()
             self._connections.pop(i)
开发者ID:ascetic85,项目名称:ziwei,代码行数:6,代码来源:sql_db.py

示例9: close_all

 def close_all(self, dsn=None):
     _logger.info('%r: Close all connections to %r', self, dsn)
     for i, (cnx, used) in tools.reverse_enumerate(self._connections):
         if dsn is None or cnx._original_dsn == dsn:
             cnx.close()
             self._connections.pop(i)
开发者ID:0k,项目名称:odoo,代码行数:6,代码来源:sql_db.py


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