Beispiel #1
0
    def query(self, msg_type, query, *parameters):
        '''Performs a query against a q service.
        
        In typical use case, `query` is the name of the function to call and 
        `parameters` are its parameters. When `parameters` list is empty, the 
        query can be an arbitrary q expression (e.g. ``0 +/ til 100``).
        
        Calls a anonymous function with a single parameter:
        
            >>> q.query(qconnection.MessageType.SYNC,'{til x}', 10)
        
        Executes a q expression:
        
            >>> q.query(qconnection.MessageType.SYNC,'til 10')
        
        :Parameters:
         - `msg_type` (one of the constants defined in :class:`.MessageType`) - 
           type of the query to be executed
         - `query` (`string`) - query to be executed
         - `parameters` (`list` or `None`) - parameters for the query
        
        :raises: :class:`.QConnectionException`, :class:`.QWriterException`
        '''
        if not self._connection:
            raise QConnectionException('Connection is not established.')

        if parameters and len(parameters) > 8:
            raise QWriterException('Too many parameters.')

        if not parameters or len(parameters) == 0:
            self._writer.write(query, msg_type)
        else:
            self._writer.write([query] + list(parameters), msg_type)
Beispiel #2
0
    def query(self, msg_type, query, *parameters):
        if parameters and len(parameters) > 8:
            raise QWriterException('Too many parameters.')

        if not parameters or len(parameters) == 0:
            self.transport.write(self._writer.write(query, msg_type))
        else:
            self.transport.write(
                self._writer.write([query] + list(parameters), msg_type))
Beispiel #3
0
    def _write_pandas_series(self, data, qtype=None):
        if qtype is not None:
            qtype = -abs(qtype)

        if qtype is None and hasattr(data, 'meta'):
            qtype = -abs(data.meta.qtype)

        if qtype is None and 'meta' in data.attrs:
            qtype = -abs(data.attrs['meta'].qtype)

        if data.dtype == '|S1':
            qtype = QCHAR

        if qtype is None:
            qtype = Q_TYPE.get(data.dtype.type, None)

        if qtype is None and data.dtype.type in (numpy.datetime64,
                                                 numpy.timedelta64):
            qtype = TEMPORAL_PY_TYPE.get(str(data.dtype), None)

        if qtype is None:
            # determinate type based on first element of the numpy array
            qtype = Q_TYPE.get(type(data.iloc[0]), QGENERAL_LIST)

            if qtype == QSTRING:
                # assume we have a generic list of strings -> force representation as symbol list
                qtype = QSYMBOL

        if qtype is None:
            raise QWriterException('Unable to serialize pandas series %s' %
                                   data)

        if qtype == QGENERAL_LIST:
            self._write_generic_list(data.values)
        elif qtype == QCHAR:
            self._write_string(
                data.replace(numpy.nan,
                             ' ').values.astype(numpy.string_).tostring())
        elif data.dtype.type not in (numpy.datetime64, numpy.timedelta64):
            data = data.fillna(QNULLMAP[-abs(qtype)][1])
            data = data.values

            if PY_TYPE[qtype] != data.dtype:
                data = data.astype(PY_TYPE[qtype])

            self._write_list(data, qtype=qtype)
        else:
            data = data.values
            data = data.astype(TEMPORAL_Q_TYPE[qtype])
            self._write_list(data, qtype=qtype)
Beispiel #4
0
    def _write_pandas_series(self, data, qtype=None):
        if qtype is not None:
            qtype = -abs(qtype)

        if qtype is None and hasattr(data, 'meta'):
            qtype = -abs(data.meta.qtype)

        if data.dtype == '|S1':
            qtype = QCHAR

        if qtype is None:
            qtype = Q_TYPE.get(data.dtype.type, None)

        if qtype is None and data.dtype.type in (numpy.datetime64,
                                                 numpy.timedelta64):
            qtype = TEMPORAL_PY_TYPE.get(str(data.dtype), None)

        if qtype is None:
            # determinate type based on first element of the numpy array
            qtype = Q_TYPE.get(type(data[0]), QGENERAL_LIST)

        if qtype is None:
            raise QWriterException('Unable to serialize pandas series %s' %
                                   data)

        if qtype == QGENERAL_LIST:
            self._write_generic_list(data.as_matrix())
        elif qtype == QCHAR:
            self._write_string(
                data.replace(numpy.nan,
                             ' ').as_matrix().astype(numpy.string_).tostring())
        elif data.dtype.type not in (numpy.datetime64, numpy.timedelta64):
            data = data.fillna(QNULLMAP[-abs(qtype)][1])
            data = data.as_matrix()

            if PY_TYPE[qtype] != data.dtype:
                data = data.astype(PY_TYPE[qtype])

            self._write_list(data, qtype=qtype)
        else:
            data = data.as_matrix()
            data = data.astype(TEMPORAL_Q_TYPE[qtype])
            self._write_list(data, qtype=qtype)