Exemplo n.º 1
0
    def add_to_upload_queue(
        self,
        rows: Union[
            Dict[int, List[Union[int, float, str]]],
            List[Tuple[int, Union[int, float, str]]],
            List[Dict[str, Any]],
            SequenceData,
        ],
        column_external_ids: Optional[List[str]] = None,
        id: int = None,
        external_id: str = None,
    ) -> None:
        """
        Add sequence rows to upload queue. Mirrors implementation of SequenceApi.insert. Inserted rows will be
        cached until uploaded

        Args:
            rows: The rows to be inserted. Can either be a list of tuples, a list of ["rownumber": ..., "values": ...]
                objects, a dictionary of rowNumber: data, or a SequenceData object.
            column_external_ids: List of external id for the columns of the sequence
            id: Sequence internal ID
                Use if external_id is None
            external_id: Sequence external ID
                Us if id is None
        """

        if len(rows) == 0:
            pass

        either_id = EitherId(id=id, external_id=external_id)

        if isinstance(rows, SequenceData):
            # Already in desired format
            pass
        elif isinstance(rows, dict):
            rows = [{"rowNumber": row_number, "values": values} for row_number, values in rows.items()]

            rows = SequenceData(id=id, external_id=id, rows=rows, columns=column_external_ids)

        elif isinstance(rows, list):
            if isinstance(rows[0], tuple) or isinstance(rows[0], list):
                rows = [{"rowNumber": row_number, "values": values} for row_number, values in rows]

            rows = SequenceData(id=id, external_id=id, rows=rows, columns=column_external_ids)
        else:
            raise TypeError("Unsupported type for sequence rows: {}".format(type(rows)))

        with self.lock:
            seq = self.upload_queue.get(either_id)
            if seq is not None:
                # Update sequence
                seq.values = [*seq.values, *rows.values]
                seq.row_numbers = [*seq.values, *rows.row_numbers]

                self.upload_queue[either_id] = seq
            else:
                self.upload_queue[either_id] = rows
Exemplo n.º 2
0
    def retrieve(
        self,
        start: int,
        end: int,
        column_ids: Optional[List[int]] = None,
        column_external_ids: Optional[List[str]] = None,
        external_id: str = None,
        id: int = None,
        limit: int = None,
    ) -> SequenceData:
        """Retrieve data from a sequence

        Args:
            start (int): Row number to start from (inclusive).
            end (int): Upper limit on the row number (exclusive).
            column_ids (Optional[List[int]]): List of ids for the columns of the sequence.
                If 'None' is passed to both column_ids and columns_external_ids, all columns will be retrieved.
            column_external_ids (Optional[List[str]]): List of external id for the columns of the sequence.
            id (int): Id of sequence.
            external_id (str): External id of sequence.
            limit (int): Maximum number of rows to return.


        Returns:
            List of sequence data

        Examples:

                >>> from cognite.client.experimental import CogniteClient
                >>> c = CogniteClient()
                >>> res = c.sequences.data.retrieve(id=0, start=0, end=None)
                >>> tuples = [(r,v) for r,v in res.items()] # You can use this iterator in for loops and list comprehensions,
                >>> single_value = res[23] # ... get the values at a single row number,
                >>> col = res.get_column(external_id='columnExtId') # ... get the array of values for a specific column,
                >>> df = res.to_pandas() # ... or convert the result to a dataframe
        """
        utils._auxiliary.assert_exactly_one_of_id_or_external_id(
            id, external_id)
        post_obj = self._process_ids(id, external_id, wrap_ids=True)[0]
        post_obj.update(
            self._process_columns(column_ids=column_ids,
                                  column_external_ids=column_external_ids))
        post_obj.update({
            "inclusiveFrom": start,
            "exclusiveTo": end,
            "limit": limit
        })
        seqdata = []
        column_response = self._fetch_data(post_obj,
                                           lambda data: seqdata.extend(data))
        return SequenceData(id=id,
                            external_id=external_id,
                            rows=seqdata,
                            columns=column_response)
Exemplo n.º 3
0
 def _fetch_sequence(post_obj):
     post_obj.update(
         self._process_columns(column_external_ids=column_external_ids))
     post_obj.update({"start": start, "end": end, "limit": limit})
     seqdata = []
     columns = []
     for data, columns in self._fetch_data(post_obj):
         seqdata.extend(data)
     return SequenceData(id=post_obj.get("id"),
                         external_id=post_obj.get("externalId"),
                         rows=seqdata,
                         columns=columns)