# -*- coding: utf-8 -*-## This file is subject to the terms and conditions defined in# file 'LICENSE.txt', which is part of this source code package.#fromMuscat.IO.IOFactoryimportRegisterWriterClass,RegisterReaderClassfromtypingimportAnyimportpickleas__picklefromMuscat.IO.ReaderBaseimportReaderBase
[docs]classIOHelper:"""helper Class that represent the data from a file"""def__init__(self,data)->None:self.unnamed=data[0]self.named=data[1]def__str__(self)->str:res=" named : "+str(self.named)+"\n"res+=" unnamed : "+str(self.unnamed)+"\n"returnres
[docs]defSaveData(filename:str,*argv,**kwargs)->int:"""Save the variables into the disk and return 0 if all ok Save variables into the disk, you can use unnamed or named variables (keyword) Parameters ---------- filename : str File name to Returns ------- int 0 if everything ok """withopen(filename,"wb")aspickle_file:pickler_data=__pickle.Pickler(pickle_file)pickler_data.dump([argv,kwargs])return0return1# pragma: no cover
[docs]defLoadData(filename:str)->Any:"""Load data from disk using pickle format Load data saved with the 'saveData' from file Parameters ---------- filename : str File name to use Returns ------- IOHelper | None return an instance of IOHelper if ok return None if not ok """withopen(filename,"rb")aspickle_file:unpickler_data=__pickle.Unpickler(pickle_file,encoding="latin1")data=unpickler_data.load()returnIOHelper(data)returnNone# pragma: no cover
[docs]classPickleReader(ReaderBase):"""Class handling the reading of data using pickle"""def__init__(self,fileName:str=""):super().__init__(fileName=fileName)self.canHandleTemporal=False
[docs]defRead(self):"""Reads data using pickle Returns ------- any read data """internalReader=LoadData(self.fileName)returninternalReader.named["mesh"]
[docs]classPickleWriter(object):"""Class handling the writing of data using pickle"""def__init__(self):super().__init__()self.filename=""self.canHandleBinaryChange=False
[docs]defWrite(self,mesh,PointFields=None,CellFields=None,GridFields=None,PointFieldsNames=None,CellFieldsNames=None,GridFieldsNames=None):"""Writes data using pickle"""ifPointFieldsNamesisnotNone:nodeFields={k:vfork,vinzip(PointFieldsNames,PointFields)}else:nodeFields={}ifCellFieldsNamesisnotNone:elemFields={k:vfork,vinzip(CellFieldsNames,CellFields)}else:elemFields={}importcopycmesh=copy.copy(mesh)cmesh.nodeFields=nodeFieldscmesh.elemFields=elemFieldsSaveData(self.filename,mesh=cmesh)
[docs]defCheckIntegrity(GUI:bool=False):"""AutoTest routine"""importnumpyasnpfromMuscat.Helpers.IO.TemporaryDirectoryimportTemporaryDirectory# create a temp filetempdir=TemporaryDirectory.GetTempPath()SaveData(tempdir+"testFile.data","two",3,(3,5),toto=10)# load datab=LoadData(tempdir+"testFile.data")# test correct datanp.testing.assert_equal(b.unnamed[0],"two")np.testing.assert_equal(b.unnamed[1],3)np.testing.assert_equal(b.unnamed[2],(3,5))np.testing.assert_equal(b.named["toto"],10)print(b)fromMuscat.Containers.MeshCreationToolsimportCreateUniformMeshOfBarsbarmesh=CreateUniformMeshOfBars(0,8,10)PointFields=[np.arange(barmesh.GetNumberOfNodes())]PointFieldsNames=["PointData"]CellFields=[np.arange(barmesh.GetNumberOfElements())]CellFieldsNames=["PointData"]print(barmesh)pw=PickleWriter()pw.SetBinary()# this has no effectpw.Open(tempdir+"testFile.pickle")pw.Write(barmesh)pw.Write(barmesh,PointFieldsNames=PointFieldsNames,PointFields=PointFields,CellFields=CellFields,CellFieldsNames=CellFieldsNames)pw.Close()# this has no effectpr=PickleReader()pr.SetFileName(tempdir+"testFile.pickle")barmeshII=pr.Read()fromMuscat.Containers.MeshToolsimportIsCloseIsClose(barmesh,barmeshII)print(barmeshII)return"Ok"
if__name__=="__main__":# import time# stime = time.time()print(CheckIntegrity())# pragma: no cover# print(time.time()-stime)