Exemple #1
0
    def _deserialise_issue_request(
        self,
        message_body: bytes,
    ) -> List[IndexedIssue]:
        """Deserialise an incoming issue classification request.

        The input should have be serialised in JSON consisting of a JSON array
        of JSON objects encoded in either UTF-8, UTF-16 or UTF-32. Each JSON
        object should consist of two key-value pairs: the issue body as a JSON
        string with the key "body" (without quotes), and the issue index as a
        JSON string with the key "id" (without quotes). This id should be unique
        on the client's side (preferably globally irrespective of issues
        currently in the classifier itself) so that the client can map the
        classification results back to their original issues based on that id.

        Parsing is performed in two steps: the input is parsed from bytes into
        JSON using json.loads, then the result is parsed into pydantic
        IndexedIssue objects. Both of these operations are performed by the
        single function call parse_raw_as, which acts as a wrapper for
        json.loads and parse_raw.

        Args: message_body (bytes): The input issue to be classified.

        Returns: List[IndexedIssue]: The list of issues parsed as pydantic
        IndexedIssues.
        """
        logging.info("Deserialising issues...")
        indexed_issues: List[IndexedIssue] = []
        indexed_issues = parse_raw_as(List[IndexedIssue], message_body)
        logging.info("Issues deserialised successfully.")

        return indexed_issues
Exemple #2
0
def test_raw_as():
    class Item(BaseModel):
        id: int
        name: str

    item_data = '[{"id": 1, "name": "My Item"}]'
    items = parse_raw_as(List[Item], item_data)
    assert items == [Item(id=1, name='My Item')]
class ComputationalServices(BaseCustomSettings):
    DEFAULT_MAX_NANO_CPUS: NonNegativeInt = _DEFAULT_MAX_NANO_CPUS_VALUE
    DEFAULT_MAX_MEMORY: ByteSize = parse_raw_as(
        ByteSize, f"{_DEFAULT_MAX_MEMORY_VALUE}")
    DEFAULT_RUNTIME_TIMEOUT: NonNegativeInt = 0

    @validator("DEFAULT_MAX_NANO_CPUS", pre=True)
    @classmethod
    def set_default_cpus_if_negative(cls, v):
        if v is None or v == "" or int(v) <= 0:
            v = _DEFAULT_MAX_NANO_CPUS_VALUE
        return v

    @validator("DEFAULT_MAX_MEMORY", pre=True)
    @classmethod
    def set_default_memory_if_negative(cls, v):
        if v is None or v == "" or int(v) <= 0:
            v = _DEFAULT_MAX_MEMORY_VALUE
        return v