Example #1
0
    def readData(self, rawstring, datatype):
        """Read the data part of the message coming from stream

        Arguments:
        rawstring -- raw data bytes
        datatype -- data type

        Return value:
        the data read
        """
        data = rawstring[:-1]  #remove last NULL byte

        if datatype == ERROR:
            return data
        elif datatype == STRING or datatype == DOUBLE:
            # try to convert data to a more appropriate type
            try:
                data = int(data)
            except:
                try:
                    data = float(data)
                except:
                    pass

            return data
        elif datatype == ASSOC:
            return rawtodictonary(rawstring)
        elif SpecArray.isArrayType(datatype):
            #Here we read cols and rows... which are *supposed* to be received in the header!!!
            #better approach: data contains this information (since it is particular to that data type)
            return SpecArray.SpecArray(rawstring, datatype, self.rows,
                                       self.cols)
        else:
            raise TypeError
Example #2
0
    def readData(self, rawstring, datatype):
        """Read the data part of the message coming from stream

        Arguments:
        rawstring -- raw data bytes
        datatype -- data type

        Return value:
        the data read
        """
        data = rawstring[:-1]  # remove last NULL byte

        if datatype == ERROR:
            return data
        elif datatype == STRING or datatype == DOUBLE:
            # try to convert data to a more appropriate type
            try:
                data = int(data)
            except:
                try:
                    data = float(data)
                except:
                    pass

            return data
        elif datatype == ASSOC:
            return rawtodictonary(rawstring)
        elif SpecArray.isArrayType(datatype):
            # Here we read cols and rows... which are *supposed* to be received in the header!!!
            # better approach: data contains this information (since it is particular to that data type)
            return SpecArray.SpecArray(rawstring, datatype, self.rows, self.cols)
        else:
            raise TypeError
Example #3
0
    def sendingDataString(self, data, datatype):
        """Return the string representing the data part of the message."""
        rawstring = ''

        if datatype in (ERROR, STRING, DOUBLE):
            rawstring = str(data)
        elif datatype == ASSOC:
            rawstring = dictionarytoraw(data)
        elif SpecArray.isArrayType(datatype):
            rawstring = data.tostring()

        if len(rawstring) > 0:
            rawstring += NULL

        return rawstring
Example #4
0
    def sendingDataString(self, data, datatype):
        """Return the string representing the data part of the message."""
        rawstring = ""

        if datatype in (ERROR, STRING, DOUBLE):
            rawstring = str(data)
        elif datatype == ASSOC:
            rawstring = dictionarytoraw(data)
        elif SpecArray.isArrayType(datatype):
            rawstring = data.tostring()

        if len(rawstring) > 0:
            rawstring += NULL

        return rawstring