示例#1
0
    def __init__(
        self,
        import_manager: ImportManager,
        name: str,
        key_type: str,
        value_type: str,
        description: Optional[str] = None,
        default_val: Any = None,
        self_ref: bool = False,
    ):
        if not key_type == "str":
            # This string value constraint is to conform with JSON format
            #  requirement: https://www.json.org/json-en.html
            raise CodeGenerationException(
                f"Dictionary keys can only be string values, find {key_type} "
                f"at {name}.")

        self.value_is_forte_type = import_manager.is_imported(value_type)
        type_str = ("forte.data.ontology.core.FDict"
                    if self.value_is_forte_type else "typing.Dict")
        super().__init__(
            import_manager,
            name,
            type_str,
            description=description,
            default_val=default_val,
        )
        self.key_type: str = key_type
        self.value_type: str = value_type
        self.self_ref: bool = self_ref
示例#2
0
    def add_object_to_import(self, full_name: str):
        if self.__fix_modules:
            # After fixing the modules, we should not add objects for import.
            raise CodeGenerationException(
                f'The module [{self.__module_name}] is fixed, cannot add '
                f'more objects.')

        if full_name not in self.__imported_names:
            if full_name not in SUPPORTED_PRIMITIVES:
                as_name = self.__assign_as_name(full_name)
                self.__imported_names[full_name] = as_name
                self.create_import_statement(full_name, as_name)
示例#3
0
    def add_defining_objects(self, full_name: str):
        if self.__fix_modules:
            # After fixing the modules, we should not add objects for import.
            raise CodeGenerationException(
                f'The module [{self.__module_name}] is fixed, cannot add '
                f'more objects.')

        if full_name not in self.__defining_names:
            class_name = full_name.split('.')[-1]
            if class_name not in self.__short_name_pool:
                self.__short_name_pool.add(class_name)
            else:
                logging.warning(
                    "Re-declared a new class named [%s]"
                    ", which is probably used in import.", class_name)
            self.__defining_names[full_name] = class_name