def readRowwithColumnNameandValue(INFO, TableName, ColumnName, Value, isRowCount=True, DBName=None): try: mySQLDB = connectDB(INFO, DBName) cursor = mySQLDB.cursor() cursor.execute( "SELECT * FROM " + str(TableName) + " WHERE " + str(ColumnName) + "=%s", (Value, )) Rows = cursor.fetchall() RowCount = cursor.rowcount tableDict = {} if RowCount: i = 0 for cd in cursor.description: tableDict[cd[0]] = Rows[0][i] i += 1 if isRowCount: return tableDict, RowCount else: return tableDict except Exception as e: logging.exception(e) raise
def checkMethodDBforRDGIndexes(INFO, TableName, Header, Array): try: Found = isNewArrayDB(INFO=INFO, TableName=TableName, Header=Header, Array=Array) Newinput = [] OldOutput = [] OldInput = [] Indexes = [] for i in range(len(Found)): if Found[i] == -1: Newinput.append(Array[i][:]) Indexes.append(-1) else: OldOutput.append( RDGOutputDictoArray( DB.getOutputRowByHash(INFO=INFO, TableName=TableName, Hash=Found[i]))) OldInput.append(Array[i][:]) Indexes.append(1) return OldInput, OldOutput, Newinput, Indexes except Exception as e: logging.exception(e) raise
def dumpTableSetCSV(INFO, TableName, AddressMain, DBName=None): try: mySQLDB = connectDB(INFO, DBName) query = "SELECT * FROM " + str(TableName) A1 = GF.GetAddressTo(AddressMain, "CSV", fileName=str(TableName) + "_" + GF.getDateandTimeUTC(), extension="csv") results = pandas.read_sql_query(query, mySQLDB) results.to_csv(A1, index=False) query = "SELECT * FROM " + str(TableName) + "_Out" A1 = GF.GetAddressTo(AddressMain, "CSV", fileName=str(TableName) + "_Out" + "_" + GF.getDateandTimeUTC(), extension="csv") results = pandas.read_sql_query(query, mySQLDB) results.to_csv(A1, index=False) query = "SELECT * FROM " + str(TableName) + "_Shadow" A1 = GF.GetAddressTo(AddressMain, "CSV", fileName=str(TableName) + "_Shadow" + "_" + GF.getDateandTimeUTC(), extension="csv") results = pandas.read_sql_query(query, mySQLDB) results.to_csv(A1, index=False) except Exception as e: logging.exception(e) raise
def GetRootDirectory(): try: return Path(os.path.dirname(os.path.realpath('__file__'))) except Exception as e: logging.exception(e) raise
def getToleratedArray(Array, Input, Tolerance, uniques): try: B = [] index = [] rows = len(Array) col = len(Array[0]) nearest = [] nearest_Index = [] accepted_Index = [] for i in range(col): nearest.append(findNearest(uniques[i], Input[i])) nearest_Index.append(findIndexNearest(uniques[i], Input[i])) for i in range(col): accepted_Index.append( checkIndex(Tolerance[i], nearest_Index[i], len(uniques[i]) - 1)) for i in range(rows): k = 0 for j in range(col): if (Array[i][j] >= uniques[j][accepted_Index[j][1]]) and ( Array[i][j] <= uniques[j][accepted_Index[j][0]]): k += 1 if k == col: B.append(Array[i][:]) index.append(i) return B, index except Exception as e: logging.exception(e) raise
def ShowTableSummary(self, TableName, Number_Entry=3, Length=20, DBName=None): try: DBConnection = self.ConnectDB(DBName=DBName) cursor = DBConnection.cursor() cursor.execute( f"SELECT * FROM {TableName} ORDER BY ID_{TableName} DESC LIMIT {Number_Entry}" ) results = cursor.fetchmany(Number_Entry) widths = [] columns = [] tavnit = '|' separator = '+' # max_col_length = max(list(map(lambda x: len(str(x[4])), results))) max_col_length = Length for cd in cursor.description: widths.append(max(max_col_length, len(cd[0]))) columns.append(cd[0]) for w in widths: tavnit += " %-" + "%s.%ss |" % (w, w) separator += '-' * w + '--+' print(separator) print(tavnit % tuple(columns)) print(separator) for row in results: print(tavnit % row) print(separator) except Exception as e: logging.exception(e) raise
def insertArrayIntoTable(INFO, TableName, NameArray, Array, giveID=False, DBName=None): try: mySQLDB = connectDB(INFO, DBName) cursor = mySQLDB.cursor() S1 = " (" S2 = " (" for key in NameArray: S1 += str(key) + ", " S2 += "%s, " S1 = S1[:-2] S2 = S2[:-2] S1 += ") " S2 += ")" query = ("INSERT INTO " + str(TableName) + S1 + "VALUES" + S2) cursor.executemany(query, Array) ID = cursor.lastrowid mySQLDB.commit() cursor.close() mySQLDB.close() if giveID: return ID except Exception as e: logging.exception(e) raise
def ReadAllRowsfromTable(self, TableName, DBName=None): try: DBConnection = self.ConnectDB(DBName=DBName) cursor = DBConnection.cursor() cursor.execute("SELECT * FROM " + str(TableName)) Row = cursor.fetchall() columnName = [] i = 0 for cd in cursor.description: columnName.append(cd[0]) return columnName, Row except SQLError as error: DBConnection.rollback() logging.exception(f"Connection to the database failed: {error}") except Exception as e: logging.exception(e) raise finally: if DBConnection.is_connected(): cursor.close() DBConnection.close() logging.info("MySQL connection is closed")
def ReadColwithColumnName(self, TableName, ColumnName, DBName=None): try: def sortList(Target, Index): zipped_pairs = zip(Index, Target) z = [x for _, x in sorted(zipped_pairs)] return z DBConnection = self.ConnectDB(DBName=DBName) cursor = DBConnection.cursor(dictionary=True) cursor.execute("SELECT " + str(ColumnName) + ", " + "ID_" + str(TableName) + " FROM " + str(TableName)) # sql = ('select field1, field2 from table') List1 = [] ID = [] for rows in cursor: List1.append(rows[str(ColumnName)]) ID.append(rows["ID_" + str(TableName)]) List = sortList(List1, ID) return List except SQLError as error: DBConnection.rollback() logging.exception(f"Connection to the database failed: {error}") except Exception as e: logging.exception(e) raise finally: if DBConnection.is_connected(): cursor.close() DBConnection.close() logging.info("MySQL connection is closed")
def ReadRowfromTablewithID(self, TableName, ID=1, DBName=None): try: DBConnection = self.ConnectDB(DBName=DBName) cursor = DBConnection.cursor() cursor.execute( "SELECT * FROM " + str(TableName) + " WHERE " + "ID_" + str(TableName) + "=%s", (ID, )) Row = cursor.fetchall() tableDict = {} i = 0 for cd in cursor.description: tableDict[cd[0]] = Row[0][i] i += 1 return tableDict except SQLError as error: DBConnection.rollback() logging.exception(f"Connection to the database failed: {error}") except Exception as e: logging.exception(e) raise finally: if DBConnection.is_connected(): cursor.close() DBConnection.close() logging.info("MySQL connection is closed")
def IsFileExist(address): try: return os.path.isfile(address) except Exception as e: logging.exception(e) raise
def CheckInputDict(self, inputDict): try: outputDict = {} arrDf_Index = FN.getGoodIndexes(Array=inputDict['Df'], Bound=self._arrDf_Bound) arrKf_Index = FN.getGoodIndexes(Array=inputDict['Kf'], Bound=self._arrKf_Bound) arrRI_Real_Index = FN.getGoodIndexes(Array=inputDict['RI_Real'], Bound=self._arrRI_Real_Bound) arrRI_Imag_Index = FN.getGoodIndexes(Array=inputDict['RI_Imag'], Bound=self._arrRI_Imag_Bound) arrNp_Index = FN.getGoodIndexes(Array=inputDict['Np'], Bound=self._arrNp_Bound) arrMonomerParameter_Index = FN.getGoodIndexes(Array=inputDict['MP'], Bound=self._arrMonomerParameter_Bound) arrPossible_Indexes = FN.findCommonIndex(arrDf_Index, arrKf_Index, arrRI_Real_Index, arrRI_Imag_Index, arrNp_Index, arrMonomerParameter_Index) outputDict['Df'] = FN.getPossibleArray(Array=inputDict['Df'], Indexes=arrPossible_Indexes) outputDict['Kf'] = FN.getPossibleArray(Array=inputDict['Kf'], Indexes=arrPossible_Indexes) outputDict['RI_Real'] = FN.getPossibleArray(Array=inputDict['RI_Real'], Indexes=arrPossible_Indexes) outputDict['RI_Imag'] = FN.getPossibleArray(Array=inputDict['RI_Imag'], Indexes=arrPossible_Indexes) outputDict['Np'] = FN.getPossibleArray(Array=inputDict['Np'], Indexes=arrPossible_Indexes) outputDict['dp'] = FN.getPossibleArray(Array=inputDict['dp'], Indexes=arrPossible_Indexes) outputDict['wL'] = FN.getPossibleArray(Array=inputDict['wL'], Indexes=arrPossible_Indexes) outputDict['chance'] = FN.getPossibleArray(Array=inputDict['chance'], Indexes=arrPossible_Indexes) sum = 0 for i in outputDict['chance']: sum += i outputDict['chanceSum'] = sum #### to do add more if needed logging.info("Boundary issued.") return outputDict except Exception as e: logging.exception(e) raise
def showTableSummary(INFO, TableName, Number_Entry=3, Length=20, DBName=None): try: mySQLDB = connectDB(INFO, DBName) cursor = mySQLDB.cursor() cursor.execute("SELECT * FROM " + str(TableName) + " ORDER BY " + "ID_" + str(TableName) + " DESC LIMIT " + str(Number_Entry)) results = cursor.fetchmany(Number_Entry) widths = [] columns = [] tavnit = '|' separator = '+' # max_col_length = max(list(map(lambda x: len(str(x[4])), results))) max_col_length = Length for cd in cursor.description: widths.append(max(max_col_length, len(cd[0]))) columns.append(cd[0]) for w in widths: tavnit += " %-" + "%s.%ss |" % (w, w) separator += '-' * w + '--+' print(separator) print(tavnit % tuple(columns)) print(separator) for row in results: print(tavnit % row) print(separator) except Exception as e: logging.exception(e) raise
def readColwithColumnName(INFO, TableName, ColumnName, DBName=None): try: def sortList(Target, Index): zipped_pairs = zip(Index, Target) z = [x for _, x in sorted(zipped_pairs)] return z mySQLDB = connectDB(INFO, DBName) cursor = mySQLDB.cursor(dictionary=True) cursor.execute("SELECT " + str(ColumnName) + ", " + "ID_" + str(TableName) + " FROM " + str(TableName)) # sql = ('select field1, field2 from table') List1 = [] ID = [] for rows in cursor: List1.append(rows[str(ColumnName)]) ID.append(rows["ID_" + str(TableName)]) List = sortList(List1, ID) return List except Exception as e: logging.exception(e) raise
def GetVarName(obj, namespace): try: A = [name for name in namespace if namespace[name] is obj] return A[0] except Exception as e: logging.exception(e) raise
def FilterListFromNONE(list): try: return list(filter(None, list)) except Exception as e: logging.exception(e) raise
def DTEM_FromEffectiveDensity_D_Alpha(self, Primary_D_Alpha, Eff_dm): try: K = (2 * Primary_D_Alpha - Eff_dm) / (2 * Primary_D_Alpha - 3) return K except Exception as e: logging.exception(e) raise
def findNearest(Array, input): try: inp = Decimal(input) A = min(Array, key=lambda x: abs(x - inp)) return A except Exception as e: logging.exception(e) raise
def ReInitializeDB(self, DBName=None): try: DBnameToConnect = self.CheckDBName(DBName=DBName) self.DropDB(DBName=DBnameToConnect) self.CreateDB(DBName=DBnameToConnect) except Exception as e: logging.exception(e) raise
def ReportVariable(name, value): try: logging.info(f'{name}: {value}') print(f'{name}: {value}') except Exception as e: logging.exception(e) raise
def RDG_Absorption(self, K, N, Dp, E): try: A = Decimal(-1) * N * Decimal(4) * Decimal(pi) * K * E * ( (Dp / Decimal(2))**Decimal(3)) return A except Exception as e: logging.exception(e) raise
def Scattering_Wave_Vector(self, WaveLength_meter, Theta_radian): try: A = (Decimal(4) * Decimal(pi) / WaveLength_meter) * Decimal( sin(Theta_radian / 2)) return A except Exception as e: logging.exception(e) raise
def findIndexNearest(Array, input): try: inp = Decimal(input) A = min(range(len(Array)), key=lambda i: abs(Array[i] - inp)) return A except Exception as e: logging.exception(e) raise
def getRandomFromArr(self, Array, Number): try: # uniform choice A = np.random.choice(Array, size=int(Number), replace=False) return A except Exception as e: logging.exception(e) raise
def ReadFile(fileAddress): try: A = open(fileAddress).read() return A except Exception as e: logging.exception(e) raise
def RunFile(address): try: # I changed the Fortran code so it can be used here. # p1 = subprocess.Popen([str(Address.resolve())]) os.system(str(address.resolve())) except Exception as e: logging.exception(e) raise
def WriteFile(address, string, mode='w'): try: f = open(address, mode) f.write(string) f.close() except Exception as e: logging.exception(e) raise
def __init__(self, infoDict, calcDict, RDGActive, TmatrixActive): try: self.__info = infoDict self.__calcDict = calcDict self.__RDGActive = RDGActive self.__TmatrixActive = TmatrixActive except Exception as e: logging.exception(e) raise
def ReportDictionary(dictionary): try: for key in dictionary.keys(): logging.info(f'{key}: {dictionary[key]}') print(f'{key}: {dictionary[key]}') except Exception as e: logging.exception(e) raise
def GetDateAndTimeUTCNow(): try: return str(datetime.datetime.utcnow().replace( tzinfo=datetime.timezone.utc).isoformat("T", "seconds")).replace( ":", "") except Exception as e: logging.exception(e) raise