[docs]classBaseParser(ABC):"""Initializes the parser given a file name. This is a base class that must be extended on child classes. The purposes of the parser classes are: - Read a file with chemical data. - Parse the file to extract the atoms and bonds data. - Return a dictionary with the atoms and bonds data. """def__init__(self,filename:Union[str,bytes,os.PathLike])->None:self.file_data:str=self.read_file(filename)parsed_data=self.parse_file_data()ifisinstance(parsed_data,list):self.molecular_data=parsed_dataself.atoms_data,self.bonds_data=(None,None)elifisinstance(parsed_data,tuple):self.molecular_data=Noneself.atoms_data,self.bonds_data=parsed_data
[docs]@staticmethod@abstractmethoddefread_file(filename:Union[str,bytes,os.PathLike])->Any:""" Reads the file and converts it to a string. Returns: str: String with the file data. """...
[docs]@staticmethod@abstractmethoddefdata_parser(data:Any)->Tuple[Dict,Dict]|List[Tuple[Dict,Dict]]:"""Parses the atoms and bonds data and returns a tuple of dictionaries with each data. The atom data follows the structure: {<atom_index>: {"element": <atom_element>, "position": [<x_pos>, <y_pos>, <z_pos>]}} The bond data follows the structure: {<bond_index>: {"from_atom_index": <from_atom_index>, "to_atom_index": <to_atom_index>, "bond_type": <bond_type>}} """...
[docs]defparse_file_data(self)->Tuple[Dict,Dict]|List[Tuple[Dict,Dict]]:""" Receives the file data as a string and uses the string_parser. Returns: Tuple[Dict, Dict] | List[Tuple[Dict, Dict]]: (atom_data, bond_data) """returnself.data_parser(self.file_data)
@propertydefmolecule_data(self):""" Returns molecule data: atoms_data and bonds_data. """ifall([self.atoms_data,self.bonds_data]):returnself.atoms_data,self.bonds_dataelifself.molecular_data:returnself.molecular_dataraiseException(f"Atoms data, bonds data and molecular data are not correct: {self.atoms_data}{self.bonds_data}{self.molecular_data}")@propertydefatoms(self):""" Returns atoms data. """returnself.atoms_data@propertydefbonds(self):""" Returns bonds data. """returnself.bonds_data