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


Python Error.increment方法代码示例

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


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

示例1: Exchange

# 需要导入模块: from error import Error [as 别名]
# 或者: from error.Error import increment [as 别名]

#.........这里部分代码省略.........
    def stop(self):
        if self.timeout_id:
            GLib.source_remove(self.timeout_id)

        self.started = False
        self.indicator.alarm.deactivate()
        self.error.reset()

        return self

    ##
    # Restarts the exchange. This is necessary for restoring normal frequency as
    # False must be returned for the restart operation to be done only once
    #
    def restart(self):
        self.start()
        return False

    ##
    # This function is called frequently to get price updates from the API
    #
    def _check_price(self):
        self.pair = self.asset_pair.get('pair')
        timestamp = time.time()
        command = DownloadCommand(self._get_ticker_url(), self.indicator.update_gui)
        command.timestamp = timestamp
        command.error = self._handle_error
        command.validation = self.asset_pair
        self.downloader.execute(command, self._handle_result)

        logging.info('Request with TS: ' + str(timestamp))
        if not self.error.is_ok():
            self.timeout_id = None

        return self.error.is_ok()  # continues the timer if there are no errors

    def _handle_error(self, error):
        self.error.log(str(error))
        self.error.increment()

    # def _handle_result(self, data, validation, timestamp):
    def _handle_result(self, command):
        data = command.response
        # Check to see if the returning response is still valid
        # (user may have changed exchanges before the request finished)
        if not self.started:
            logging.info("Discarding packet for inactive exchange")
            return

        if command.validation is not self.asset_pair:  # we've already moved on.
            logging.info("Discarding packet for wrong asset pair or exchange")
            return

        # also check if a newer response hasn't already been returned
        if command.timestamp < self.indicator.latest_response:  # this is an older request
            logging.info("Discarding outdated packet")
            return

        if data.status_code != 200:
            self._handle_error('API server returned an error: ' + str(data.status_code))
            return

        try:
            asset = data.json()
        except Exception:
            # Before, a KeyError happened when an asynchronous response comes in
            # for a previously selected asset pair (see upstream issue #27)
            self._handle_error('Invalid response for ' + str(self.pair))
            return

        results = self._parse_ticker(asset)
        self.indicator.latest_response = command.timestamp
        logging.info(
            'Response comes in with timestamp ' + str(command.timestamp) +
            ', last response at ' + str(self.indicator.latest_response))

        for item in CATEGORY:
            if results.get(item):
                self.indicator.prices[item] = self._decimal_auto(results.get(item))

        self.error.reset()

        GLib.idle_add(command.callback)

    ##
    # Rounds a number to a meaningful number of decimal places
    # and returns it as a string
    #
    def _decimal_auto(self, number):
        number = float(number)
        if number < 10:
            for i in range(8, 0, -1):
                if number < 10**-i:
                    break
        elif number >= 100:
            i = -2
        elif number >= 10:
            i = -1

        return ('{0:.' + str(i + 2) + 'f}').format(number)
开发者ID:nilgradisnik,项目名称:coinprice-indicator,代码行数:104,代码来源:exchange.py


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