我的helper模块(Python)
生活随笔
收集整理的這篇文章主要介紹了
我的helper模块(Python)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我的helper模塊(Python)
- 前言
- 一、helper模塊
- 1.具體代碼
- 二、使用步驟
- 1.創(chuàng)建helper.py文件
- 2.粘貼代碼
- 3.導(dǎo)入模塊
- 總結(jié)
前言
在我分享我所編寫的代碼時,發(fā)現(xiàn)我的函數(shù)大概率運用了我寫的helper模塊。
所以,我不能直接上傳我所寫的代碼,而是要更改運用了helper模塊的函數(shù)的相關(guān)代碼。
而現(xiàn)在我發(fā)現(xiàn),這樣的工作量實在是太大了,遂我要分享給你們我的helper模塊,以致力于方便我更注重于代碼的分享。
一、helper模塊
1.具體代碼
import random import time from time import perf_counter#Helper函數(shù) def ExitMSG_Return(funcName, MSG):"""此函數(shù)所接受的變量:funcName:str型MSG:str型此函數(shù)的作用:輸出funcName該函數(shù)的錯誤信息MSG,并結(jié)束程序此函數(shù)返回值:None"""if type(funcName) != str and type(MSG) != str:ExitMSG_Return("ExitMSG_Return(funcName, MSG)", "\n\t\t\t\t\t\t\t\t\t1.funcName必須為str型""\n\t\t\t\t\t\t\t\t\t2.MSG必須為str型")if type(funcName) != str:ExitMSG_Return("ExitMSG_Return(funcName, MSG)", "funcName必須為str型")if type(MSG) != str:ExitMSG_Return("ExitMSG_Return(funcName, MSG)", "MSG必須為str型")exit("函數(shù)錯誤:\n%s錯誤:%s" %(funcName, MSG)) def isString(judged_object):return type(judged_object) == str def isInt(judged_object):return type(judged_object) == int def isFloat(judged_object):return type(judged_object) == float def isNumber(judged_object):return isInt(judged_object) or isFloat(judged_object) def isOperator(judged_object):return judged_object == '+' or judged_object == '-' or judged_object == '*' or judged_object == '/' def isDict(judged_object):return type(judged_object) == dict def Start(funcName):if isString(funcName):print('{:=^70}'.format('%s運行中' %funcName))else:ExitMSG_Return("Start(funcName)", "funcName必須為str型") def Done(funcName):if isString(funcName):print('{:=^70}'.format('%s運行完成' % funcName))else:ExitMSG_Return("Start(funcName)", "funcName必須為str型") def MakeList(a , b):"""此函數(shù)所接受的變量:a:int型b:int型此函數(shù)的作用:生成并返回一個從Int型a到Int型b的前閉后閉數(shù)組此函數(shù)返回值:List:int型數(shù)組"""if type(a) != int and type(b) != int:ExitMSG_Return("MakeList(a , b)", "\n\t\t\t\t\t1.a必須為int型""\n\t\t\t\t\t2.b必須為int型")if type(a) != int:ExitMSG_Return("MakeList(a , b)", "a必須為int型")if type(b) != int:ExitMSG_Return("MakeList(a , b)", "b必須為int型")if a <= b:List = []for i in range(a, b + 1):List.append(i)return Listelse:ExitMSG_Return("MakeList(a , b)", "開頭的數(shù)值不能大于末尾的數(shù)值") def MakeList_FullZero(Num):"""此函數(shù)所接受的變量:Num:int型此函數(shù)的作用:生成并返回一個長度為Num的全零數(shù)組此函數(shù)返回值:List:int型數(shù)組"""if type(Num) != int:ExitMSG_Return("MakeList_FullZero(Num)", "Num必須為int型")if Num < 0:ExitMSG_Return("MakeList_FullZero(Num)", "Num不能低于0(數(shù)組的長度不能為負(fù)數(shù))")List = []for i in range(0, Num):List.append(0)return List def MakeList_RandomNumber(Num, a, b):"""此函數(shù)所接受的變量:Num:int型a:int型b:int型此函數(shù)的作用:生成并返回一個長度為Num,每個數(shù)為【a,b】的隨機(jī)取值的數(shù)組此函數(shù)返回值:lst1:int型數(shù)組"""if type(Num) != int:ExitMSG_Return("MakeList_RandomNumber(Num, a, b)", "Num必須為int型")if type(a) != int:ExitMSG_Return("MakeList_RandomNumber(Num, a, b)", "a必須為int型")if type(b) != int:ExitMSG_Return("MakeList_RandomNumber(Num, a, b)", "b必須為int型")if a > b:ExitMSG_Return("MakeList_RandomNumber(Num, a, b)", "a不能大于b")lst1 = []for i in range(0, Num):lst1.append(random.randint(a, b))return lst1 def MakeList_RandomIncrease(Num, a, b):"""此函數(shù)所接受的變量:Num:int型a:int型b:int型此函數(shù)的作用:每次生成一個數(shù)ri,下一個數(shù)的取值范圍為【a+ri,b+ri】,以此類推共生成Num長度的int型數(shù)組此函數(shù)返回值:lst:int型數(shù)組"""if type(Num) != int:ExitMSG_Return("MakeList_RandomIncrease(Num, a, b)", "Num必須為int型")if type(a) != int:ExitMSG_Return("MakeList_RandomIncrease(Num, a, b)", "a必須為int型")if type(b) != int:ExitMSG_Return("MakeList_RandomIncrease(Num, a, b)", "b必須為int型")if a > b:ExitMSG_Return("MakeList_RandomNumber(Num, a, b)", "a不能大于b")lst = []aTemp = abTemp = bfor i in range(0, Num):ri = random.randint(aTemp, bTemp)lst.append(ri)aTemp += ribTemp += rireturn lst def Range(a , b):"""此函數(shù)所接受的變量:a:int型b:int型此函數(shù)的作用:range函數(shù)的升級版,從原先的前閉后開變成了前閉后閉此函數(shù)返回值:List:int型數(shù)組"""if type(a) != int:ExitMSG_Return("Range(a , b)", "a必須為int型")if type(b) != int:ExitMSG_Return("Range(a , b)", "b必須為int型")if a <= b:List = []for i in range(a, b + 1):List.append(i)return Listelse:ExitMSG_Return("Range(a , b)", "開頭的數(shù)值不能大于末尾的數(shù)值") def Number_Separate(number):number_lst = []change_flag = FalseintRange_length = 0for num_str in str(number):if num_str == '.':change_flag = Truecontinueif change_flag:number_lst.append(int(num_str))else:intRange_length += 1number_lst.append(int(num_str))return [number_lst, intRange_length] def Probabilistic_Operation(probability):upper = round(probability * 100)random_Num = random.randint(0, 100)if 0 <= random_Num <= upper:return Trueelse:return False def Probabilistic_Operation_Function_Judge(probability):base_List = MakeList(1, 100)list_1 = base_List + [probability]# print(list_1)t_account = 0for i in range(0, 10000):if Probabilistic_Operation(probability):t_account += 1list_2 = base_List + [t_account / 10000]# print(list_2)length = len(list_1)X = list_1Y = list_2XY = []for i in range(0, length):XY.append(list_1[i] * list_2[i])X2 = []for i in list_1:X2.append(i ** 2)Y2 = []for i in list_2:Y2.append(i ** 2)X_A = sum(X) / len(X)Y_A = sum(Y) / len(Y)XY_A = sum(XY) / len(XY)X2_A = sum(X2) / len(X2)Y2_A = sum(Y2) / len(Y2)R = (XY_A - X_A * Y_A) / ((X2_A - X_A ** 2) ** (1 / 2) * (Y2_A - Y_A ** 2) ** (1 / 2))return R class Watcher:def __init__(self, funcName):self.funcName = funcNamedef Start(self):Start(self.funcName)def Done(self):Done(self.funcName) class Stack:def __init__(self, max_size):self.__stack_list = []self.__max_size = max_sizeself.__stack_length = 0def initialize(self):self.__stack_list.clear()self.__stack_length = 0def push(self, element):if self.__max_size == self.__stack_length:return Falseelse:self.__stack_length += 1self.__stack_list.append(element)def pop(self):if self.__stack_length == 0:return Falseelse:self.__stack_length -= 1return self.__stack_list.pop()def stack_length(self):return self.__stack_lengthdef get_top_element(self):if self.__stack_length == 0:return Falsereturn self.__stack_list[self.__stack_length - 1]def stack_self(self):return self.__stack_list二、使用步驟
1.創(chuàng)建helper.py文件
2.粘貼代碼
3.導(dǎo)入模塊
在您所寫的代碼中導(dǎo)入該模塊即可。
總結(jié)
以上就是我的helper模塊的分享,并會在以后的日子里持續(xù)更新。
總結(jié)
以上是生活随笔為你收集整理的我的helper模块(Python)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 儿童节html5小游戏,2016六一儿童
- 下一篇: 房地产开发建设项目管理(全程房地产典范企