Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"). You 

4# may not use this file except in compliance with the License. A copy of 

5# the License is located at 

6# 

7# http://aws.amazon.com/apache2.0/ 

8# 

9# or in the "license" file accompanying this file. This file is 

10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF 

11# ANY KIND, either express or implied. See the License for the specific 

12# language governing permissions and limitations under the License 

13from __future__ import annotations 

14 

15from importlib import import_module 

16 

17from pydantic import BaseModel 

18 

19from braket.schema_common.schema_header import BraketSchemaHeader # noqa: F401 

20 

21 

22class BraketSchemaBase(BaseModel): 

23 """ 

24 BraketSchemaBase which includes the schema header and should be the parent class for all schemas 

25 

26 Attributes: 

27 braketSchemaHeader (BraketSchemaHeader): Schema header 

28 """ 

29 

30 braketSchemaHeader: BraketSchemaHeader 

31 

32 @staticmethod 

33 def import_schema_module(schema: BraketSchemaBase): 

34 """ 

35 Imports the module that holds the schema given the schema 

36 

37 Args: 

38 schema (BraketSchemaBase): The schema 

39 

40 Returns: 

41 Module of the schema 

42 

43 Raises: 

44 ModuleNotFoundError: If the schema module cannot be found according to 

45 schema header 

46 

47 Examples: 

48 >> schema = BraketSchemaBase.parse_raw(json_string) 

49 >> module = import_schema_module(schema) 

50 >> module.AnnealingTaskResult.parse_raw(json_string) 

51 """ 

52 name = schema.braketSchemaHeader.name 

53 version = schema.braketSchemaHeader.version 

54 module_name = name + "_v" + version 

55 return import_module(module_name) 

56 

57 @staticmethod 

58 def parse_raw_schema(json_str: str) -> BraketSchemaBase: 

59 """ 

60 Return schema object given JSON string 

61 

62 Args: 

63 json_str (str): The JSON string of the schema 

64 

65 Returns: 

66 BraketSchemaBase: The schema object. This can also be an 

67 instance of a subclass of BraketSchemaBase. 

68 """ 

69 schema = BraketSchemaBase.parse_raw(json_str) 

70 module = BraketSchemaBase.import_schema_module(schema) 

71 name = schema.braketSchemaHeader.name 

72 class_name = "".join([s.capitalize() for s in name.split(".")[-1].split("_")]) 

73 schema_class = getattr(module, class_name) 

74 return schema_class.parse_raw(json_str)