Example #1
0
    def _parse_response(self, command, format):
        response = self.read()[:-2]  # strip last two characters (\r\n)
        if not response:
            self.disconnect()
            raise StandardError("Socket closed on remote end")

        # server returned a null value
        if response in ('$-1', '*-1'):
            return None
        reply_type, response = response[0], response[1:]

        # server returned an error
        if reply_type == '-':
            if response.startswith('ERR '):
                response = response[4:]
            raise Exception(response)
        # single value
        elif reply_type == '+':
            return response
        # bulk response
        elif reply_type == '$':
            length = int(response)
            response = length and self.read(length) or ''
            self.read(2) # read the \r\n delimiter

            if format == 'json':
                return json_decode(response)
            elif format == 'npy':
                qdata = StringIO(response)
                return np.load(qdata)
            else:
                return response

        raise Exception("Unknown response type for: %s" % command)
Example #2
0
 def parse(rawdata):
     # don't known why & escaped.
     data = rawdata.strip()[3:].replace('\\x', '')
     parsed = json_decode(data)
     return (GoogleReport(x) for x in parsed)