async def _read_bytes(self, length): if not self._stream: raise QReaderException( 'There is no input data. QReader requires either stream or data chunk' ) if length == 0: return b'' else: data = await self._stream.read_bytes(length) if len(data) == 0: raise QReaderException('Error while reading data') return data
def _read_dictionary(self, qtype=QDICTIONARY, options=READER_CONFIGURATION): if options.pandas: keys = self._read_object(options=options) values = self._read_object(options=options) if isinstance(keys, pandas.DataFrame): if not isinstance(values, pandas.DataFrame): raise QReaderException( 'Keyed table creation: values are expected to be of type pandas.DataFrame. Actual: %s' % type(values)) indices = keys.columns table = keys table.meta = keys.meta table.meta.qtype = QKEYED_TABLE for column in values.columns: table[column] = values[column] table.meta[column] = values.meta[column] table.set_index([column for column in indices], inplace=True) return table else: keys = keys if not isinstance( keys, pandas.Series) else keys.as_matrix() values = values if not isinstance( values, pandas.Series) else values.as_matrix() return QDictionary(keys, values) else: return QReader._read_dictionary(self, qtype=qtype, options=options)
def sync(self, query, *parameters, **options): '''Performs a synchronous query against a q service and returns parsed data. 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``). Executes a q expression: >>> print(q.sync('til 10')) [0 1 2 3 4 5 6 7 8 9] Executes an anonymous q function with a single parameter: >>> print(q.sync('{til x}', 10)) [0 1 2 3 4 5 6 7 8 9] Executes an anonymous q function with two parameters: >>> print(q.sync('{y + til x}', 10, 1)) [ 1 2 3 4 5 6 7 8 9 10] >>> print(q.sync('{y + til x}', *[10, 1])) [ 1 2 3 4 5 6 7 8 9 10] The :func:`.sync` is called from the overloaded :func:`.__call__` function. This allows :class:`.QConnection` instance to be called as a function: >>> print(q('{y + til x}', 10, 1)) [ 1 2 3 4 5 6 7 8 9 10] :Parameters: - `query` (`string`) - query to be executed - `parameters` (`list` or `None`) - parameters for the query :Options: - `raw` (`boolean`) - if ``True`` returns raw data chunk instead of parsed data, **Default**: ``False`` - `numpy_temporals` (`boolean`) - if ``False`` temporal vectors are backed by raw q representation (:class:`.QTemporalList`, :class:`.QTemporal`) instances, otherwise are represented as `numpy datetime64`/`timedelta64` arrays and atoms, **Default**: ``False`` :returns: query result parsed to Python data structures :raises: :class:`.QConnectionException`, :class:`.QWriterException`, :class:`.QReaderException` ''' self.query(MessageType.SYNC, query, *parameters) response = self.receive(data_only = False, **options) if response.type == MessageType.RESPONSE: return response.data else: self._writer.write(QException('nyi: qPython expected response message'), MessageType.ASYNC if response.type == MessageType.ASYNC else MessageType.RESPONSE) raise QReaderException('Received message of type: %s where response was expected')
def _read_dictionary(self, qtype=QDICTIONARY): if self._options.pandas: keys = self._read_object() values = self._read_object() if isinstance(keys, pandas.DataFrame): if not isinstance(values, pandas.DataFrame): raise QReaderException( 'Keyed table creation: values are expected to be of type pandas.DataFrame. Actual: %s' % type(values)) indices = keys.columns table = keys # meta attribute is obsolete, use attrs['meta'] instead if hasattr(keys, 'meta'): table.attrs['meta'] = keys.meta warnings.warn( "Usage of meta attribute is deprecated, please use attrs['meta'] instead", DeprecationWarning) else: table.attrs['meta'] = keys.attrs['meta'] table.attrs['meta'].qtype = QKEYED_TABLE if hasattr(values, 'meta'): warnings.warn( "Usage of meta attribute is deprecated, please use attrs['meta'] instead", DeprecationWarning) for column in values.columns: table[column] = values[column] table.attrs['meta'][column] = values.meta[column] else: for column in values.columns: table[column] = values[column] table.attrs['meta'][column] = values.attrs['meta'][ column] table.set_index([column for column in indices], inplace=True) return table else: keys = keys if not isinstance(keys, pandas.Series) else keys.values values = values if not isinstance( values, pandas.Series) else values.values return QDictionary(keys, values) else: return QReader._read_dictionary(self, qtype=qtype)
async def read_data(self, message_size, is_compressed=False, **options): self._options = MetaData(**CONVERSION_OPTIONS.union_dict(**options)) if is_compressed: if self._stream: self._buffer.wrap(await self._read_bytes(4)) uncompressed_size = -8 + self._buffer.get_int() compressed_data = (await self._read_bytes(message_size - 12)) \ if self._stream else self._buffer.raw(message_size - 12) raw_data = numpy.fromstring(compressed_data, dtype=numpy.uint8) if uncompressed_size <= 0: raise QReaderException('Error while data decompression.') raw_data = uncompress(raw_data, numpy.intc(uncompressed_size)) raw_data = numpy.ndarray.tostring(raw_data) self._buffer.wrap(raw_data) elif self._stream: raw_data = await self._read_bytes(message_size - 8) self._buffer.wrap(raw_data) if not self._stream and self._options.raw: raw_data = self._buffer.raw(message_size - 8) return raw_data if self._options.raw else self._read_object()