def connect(self): if not os.path.isdir(self.MainDir): raise __QS_Error__("不存在主目录: %s!" % self.MainDir) AllTables = listDirDir(self.MainDir) _TableFactorDict = {} if not os.path.isfile(self.MainDir + os.sep + "LockFile"): open(self.MainDir + os.sep + "LockFile", mode="a").close() self._LockFile = self.MainDir + os.sep + "LockFile" self._DataLock = fasteners.InterProcessLock(self._LockFile) with self._DataLock: for iTable in AllTables: iTablePath = self.MainDir + os.sep + iTable iFactors = set(listDirFile(iTablePath, suffix=self._Suffix)) if not iFactors: continue try: iDataType = readNestedDictFromHDF5( iTablePath + os.sep + "_TableInfo.h5", "/DataType") except: iDataType = None if (iDataType is None) or (iFactors != set(iDataType.index)): iDataType = {} for ijFactor in iFactors: with h5py.File(iTablePath + os.sep + ijFactor + "." + self._Suffix, mode="r") as ijDataFile: iDataType[ijFactor] = ijDataFile.attrs["DataType"] iDataType = pd.Series(iDataType) writeNestedDict2HDF5(iDataType, iTablePath + os.sep + "_TableInfo.h5", "/DataType") _TableFactorDict[iTable] = iDataType self._TableFactorDict = _TableFactorDict self._isAvailable = True return 0
def importInfo(info_file, info_resource): TableInfo = pd.read_excel(info_resource, "TableInfo").set_index(["TableName"]) FactorInfo = pd.read_excel(info_resource, 'FactorInfo').set_index(['TableName', 'FieldName']) try: from QuantStudio.Tools.DataTypeFun import writeNestedDict2HDF5 writeNestedDict2HDF5(TableInfo, info_file, "/TableInfo") writeNestedDict2HDF5(FactorInfo, info_file, "/FactorInfo") except: pass return (TableInfo, FactorInfo)