Пример #1
0
def test_get_parameters(mocker):
    mocker.patch.dict("braincube_connector.parameters._default_parameters",
                      {"page_size": 8})
    mocker.patch.dict("braincube_connector.instances.instances",
                      {"parameter_set": {}})
    assert parameters.get_parameter("Nonexistent") is None
    assert parameters.get_parameter("page_size") == 8
    parameters.set_parameter({"page_size": 5})
    assert parameters.get_parameter("page_size") == 5
    assert instances.instances["parameter_set"]["page_size"] == 5
Пример #2
0
def _extract_format_data(raw_dataset: Dict[str, Any]) -> Dict[int, Any]:
    """Extract the requested data from the json.

    The function extracts the data keys and types and convert the columns
    using the types.

    Args:
        raw_dataset: An unformated dataset received from braindata.

    Returns:
        A formated dictionary {column_key: formated column data}
    """
    formatted_dataset = {}
    for col in raw_dataset["datadefs"]:
        col_id = int(col["id"].split("/d")[1])
        if col["type"] == "DATETIME" and parameters.get_parameter("parse_date"):
            formatted_dataset[col_id] = _to_datetime(col[DATACOL])
        elif col["type"] == "NUMERIC":
            try:
                formatted_dataset[col_id] = list(map(int, col[DATACOL]))
            except ValueError:
                formatted_dataset[col_id] = list(map(float, col[DATACOL]))
        else:
            formatted_dataset[col_id] = col[DATACOL]
    return formatted_dataset
Пример #3
0
def test_reset(mocker):
    mocker.patch.dict("braincube_connector.parameters._default_parameters",
                      {"page_size": 8})
    mocker.patch.dict("braincube_connector.instances.instances",
                      {"parameter_set": {}})
    parameters.set_parameter({"page_size": 5})
    parameters.reset_parameter()
    assert parameters.get_parameter("page_size") == 8
    assert instances.instances["parameter_set"] == {}
Пример #4
0
    def get_parameter_key(cls, key) -> str:
        """Get baseEntity_name_key parameter.and name_bcid parameter.

        Args:
            key: "name" to get the name key or "bcid" to get the bcid key

        Returns:
            The name key of the baseEntity or The bcid key of the baseEntity]
        """
        parameter_key = parameters.get_parameter("{0}_{1}_key".format(cls.__name__, key))

        return (
            parameter_key if parameter_key else cls.__base__.get_parameter_key(key)  # type: ignore
        )
Пример #5
0
    def create_collection_from_path(
        cls,
        request_path: str,
        entity_path: str,
        caller,
        page: int = -1,
        page_size: int = -1,
        braincube_name: str = constants.EMPTY_STRING,
        **kwargs,
    ) -> List[Any]:
        """Create many memory_base from a request path.

        Args:
            request_path: Webservice path to request.
            entity_path: Path of the entity on the webservice.
            caller: class used to call this method, useful to indicate which is the parent entity.
            page: Index of page to return, all pages are return if page=-1
            page_size: Number of memory_base per page.
            braincube_name: name of the braincube linked to this entity,
                            useful if you use a placeholder in your config.
            **kwargs: Additional keyword argument to pass to an object initialization.

        Returns:
            The list of created created entity.
        """
        if page_size == -1:
            page_size = parameters.get_parameter("page_size")
        offset = 0 if page < 0 else page * page_size
        entity_list: List[Any] = []

        while True:
            json_data = client.request_ws(
                "{path}?offset={offset}&size={size}".format(
                    path=request_path.format(webservice="braincube"), offset=offset, size=page_size,
                ),
                braincube_name=braincube_name,
            )
            new_entities = [
                cls.create_from_json(elmt, entity_path, caller, **kwargs)
                for elmt in json_data["items"]
            ]
            entity_list += new_entities
            if not new_entities or page > -1:
                break
            else:
                offset += page_size
        return entity_list