def readData(self, key_num=0, rev_order=False, itemperpage=9, page=1): # 處理 itemperpage 與 page 為 0 或小於 0 的情況, 因為由 URL 取回的變數型別為字串, 因此以字串判別 if itemperpage == "0" or page == "0" or int(itemperpage) < 0 or int(page) < 0 : itemperpage = 9 page = 1 # 個人資料 = [] 資料計數 = 0 # 以下改為以 pybean 儲存資料 library = Store(SQLiteWriter(SQLite_data_dir+"/database.sqlite", frozen=False)) # 動態建立 student 資料表 student = library.new("student") # 列出目前的資料庫內容 if (library.count("student") == 0): outString = "沒有資料, 請先輸入資料!<br />" outString += self.menuLink() return outString else: # 這裡在查找所有資料"1 order by id limit $starti,$item_per_page" # 利用 page 與 itemperpage 計算 startnum 與 endnum # # 由資料庫中的 student 資料表計算資料筆數 totalitem = library.count("student") totalpage = math.ceil(totalitem/int(itemperpage)) # 若索引從 1 開始 #starti = int(itemperpage) * (int(page) - 1) + 1 # 若索引從 0 開始 starti = int(itemperpage) * (int(page) - 1) endi = starti + int(itemperpage) - 1 # 必須要送到 Mako 的變數值 totalitem, page, itemperpage # 這裡要考量送回全部資料, 或者只送出特定頁面的所屬資料數列 #for 資料 in library.find("student","1"): for 資料 in library.find("student","1 order by stud_number limit "+str(starti)+", "+str(itemperpage)): #for 資料 in library.find("student","1 order by stud_number limit 0, 5"): #for 資料 in library.find("student","1 order by stud_number limit 1,10"): 學號 = 資料.stud_number 姓名 = 資料.stud_name 科系 = 資料.school_dept 專長 = 資料.major 薪水 = 資料.expt_salary #更新連結 = "<a href=\"updateForm?stud_number="+學號+"\">"+學號+"</a>" # 配合 pybean 0.2.1 將 uuid 改為 id 更新連結 = "<a href=\"updateForm?id="+str(資料.id)+"\">"+學號+"</a>" 更新 = "<a href=\"updateForm?id="+str(資料.id)+"\">更新</a>" 刪除 = "<a href=\"deleteForm?id="+str(資料.id)+"\">刪除</a>" 個人資料.append([更新連結,姓名,科系,專長,薪水,更新,刪除]) # # 根據數列中各 tuple 中的學號(亦即 key data[0])進行排序 # 個人資料排列後, 必須加以指定成變數, 否則個人資料的內容排序後並未改變 #個人資料 = sorted(個人資料, key=lambda data: data[0], reverse=True) 個人資料 = sorted(個人資料, key=lambda data: data[int(key_num)], reverse=int(rev_order)) #print(個人資料) #outString = self.htmlList(個人資料) outString = self.pageList(個人資料,totalitem,itemperpage,page) return outString
def showPage(page): itemperpage = 16 db = Store(SQLiteWriter("taobao.db", frozen=True)) rs = [] items = db.find("freetry", "1 order by date desc,gailv limit %s,%s" % ((page - 1) * itemperpage, itemperpage)) for item in items: item.uuid = item.itemId rs.append(dict(item.__dict__)) return json.dumps(rs)
def showPage(page): itemperpage = 16 db = Store(SQLiteWriter("taobao.db", frozen=True)) rs = [] items = db.find( "freetry", "1 order by date desc,gailv limit %s,%s" % ((page - 1) * itemperpage, itemperpage)) for item in items: item.uuid = item.itemId rs.append(dict(item.__dict__)) return json.dumps(rs)
def doCheck(self, word=None): if word == None: return "<br /><a href=\"/\">首頁</a>|<a href=\"./\">重新查詢</a>" #vocabulary = Store(SQLiteWriter(os.environ['OPENSHIFT_REPO_DIR']+"/wsgi/webster_vocabulary.sqlite", frozen=True)) vocabulary = Store(SQLiteWriter(data_dir+"/webster_vocabulary.sqlite", frozen=True)) if (vocabulary.count("word","lower(word) like ?", [word]) == 0): return "找不到與 "+ word.title() + "有關的資料!" else: result = vocabulary.find("word","lower(word) like ?", [word]) output = "以下為 webster 字典查詢:"+word+" 所得到的結果<br /><br />" for item in result: output += word.title()+"<br /><br />"+str(self.nl2br(item.defn,True))+"<br />" output += "<br /><a href=\"/\">首頁</a>|<a href=\"./\">重新查詢</a>" return output
def doCheck(): word = request.form["word"] if word == None: return "<br /><a href=\"/\">首頁</a>|<a href=\"./\">重新查詢</a>" vocabulary = Store( SQLiteWriter(data_dir + "/webster_vocabulary.sqlite", frozen=True)) if (vocabulary.count("word", "lower(word) like ?", [word]) == 0): return "找不到與 " + word.title() + "有關的資料!" else: result = vocabulary.find("word", "lower(word) like ?", [word]) output = "以下為 webster 字典查詢:" + word + " 所得到的結果<br /><br />" for item in result: output += word.title() + "<br /><br />" + str( nl2br(item.defn, True)) + "<br />" output += "<br /><a href=\"/\">首頁</a>|<a href=\"./\">重新查詢</a>" return output
def get_frozen_save(self): return Store( MySQLWriter(True, user="******", password="******", database="py", host="localhost"))
def get_fluid_save(self): return Store( MySQLWriter(False, user="******", password="******", database="py", host="localhost"))
def interpolation(self, small_gear_no=18, gear_type=1): SQLite連結 = Store(SQLiteWriter(_curdir + "/lewis.db", frozen=True)) # 使用內插法求值 # 找出比目標齒數大的其中的最小的,就是最鄰近的大值 lewis_factor = SQLite連結.find_one("lewis", "gearno > ?", [small_gear_no]) if (gear_type == 1): larger_formfactor = lewis_factor.type1 elif (gear_type == 2): larger_formfactor = lewis_factor.type2 elif (gear_type == 3): larger_formfactor = lewis_factor.type3 else: larger_formfactor = lewis_factor.type4 larger_toothnumber = lewis_factor.gearno # 找出比目標齒數小的其中的最大的,就是最鄰近的小值 lewis_factor = SQLite連結.find_one("lewis", "gearno < ? order by gearno DESC", [small_gear_no]) if (gear_type == 1): smaller_formfactor = lewis_factor.type1 elif (gear_type == 2): smaller_formfactor = lewis_factor.type2 elif (gear_type == 3): smaller_formfactor = lewis_factor.type3 else: smaller_formfactor = lewis_factor.type4 smaller_toothnumber = lewis_factor.gearno calculated_factor = larger_formfactor + ( small_gear_no - larger_toothnumber) * ( larger_formfactor - smaller_formfactor) / (larger_toothnumber - smaller_toothnumber) # 只傳回小數點後五位數 return str(round(calculated_factor, 5))
def doUpdate(self, id=None, stud_number=None, stud_name=None, \ school_dept=None, major=None, expt_salary=None, itemperpage=9, page=1): 個人資料 = [] 資料計數 = 0 # 以下改為以 pybean 儲存資料 library = Store(SQLiteWriter(SQLite_data_dir+"/database.sqlite", frozen=False)) # 動態建立 student 資料表 student = library.new("student") # 以 find_one 找出所要更新的一筆資料 # 配合 pybean 0.2.1 將 uuid 改為 id, 且將 UUID(uuid).bytes 改為 id 更新資料 = library.find_one("student","id=?",[id]) # 是否會發生更新資料為空的情況? if 更新資料 == None: outString = "沒有資料" return outString # 將更新資料存入 pybean 更新資料.stud_number = stud_number 更新資料.stud_name = stud_name 更新資料.school_dept = school_dept 更新資料.major = major 更新資料.expt_salary = expt_salary # 儲存資料表內容 library.save(更新資料) # 配合 pybean 0.2.1 將 commit 移出, save 與 delete 必須要配合 commit 才能更新資料庫 library.commit() # 設法從資料庫中擷取所有資料, 以得到個人資料數列 for 資料 in library.find("student","1"): 學號 = 資料.stud_number 姓名 = 資料.stud_name 科系 = 資料.school_dept 專長 = 資料.major 薪水 = 資料.expt_salary # 配合 pybean 0.2.1 將 uuid 改為 id, 且將 UUID(uuid).bytes 改為 id 更新連結 = "<a href=\"updateForm?id="+str(資料.id)+"\">"+學號+"</a>" # 增加更新與刪除連結 #個人資料.append([更新連結,姓名,科系,專長,薪水]) 更新 = "<a href=\"updateForm?id="+str(資料.id)+"\">更新</a>" 刪除 = "<a href=\"deleteForm?id="+str(資料.id)+"\">刪除</a>" 個人資料.append([更新連結,姓名,科系,專長,薪水,更新,刪除]) # # 根據數列中各 tuple 中的學號(亦即 key data[0])進行排序 # 個人資料排列後, 必須加以指定成變數, 否則個人資料的內容排序後並未改變 個人資料 = sorted(個人資料, key=lambda data: data[0], reverse=True) #print(個人資料) # 改用 HTML 進行資料列印 #outString = self.htmlList(個人資料) totalitem = library.count("student") outString = self.pageList(個人資料,totalitem,itemperpage,page) return outString
def updateForm(self,id=None): # 必須加入 id 無值時的處理 # 設法根據 stud_number 查出對應的欄位資料 # 改為利用 uuid 查出對應的欄位資料, uuid 為各筆資料的唯一代號 個人資料 = [] 資料計數 = 0 # 以下改為以 pybean 儲存資料 library = Store(SQLiteWriter(SQLite_data_dir+"/database.sqlite", frozen=False)) # 動態建立 student 資料表 student = library.new("student") # 以 find_one 找出所要的一筆資料 #一筆資料 = library.find_one("student","stud_number=?",[stud_number]) #UUID(uuid).bytes 為正確的 uuid 欄位資料 # 配合 pybean 0.2.1 將 UUID(uuid).bytes 改為 id 一筆資料 = library.find_one("student","id=?",[id]) # 若查詢後為空資料, 則回覆沒有資料或回傳發生錯誤. if 一筆資料 == None: outString = "沒有資料" return outString stud_number = 一筆資料.stud_number stud_name = 一筆資料.stud_name school_dept = 一筆資料.school_dept major = 一筆資料.major expt_salary = 一筆資料.expt_salary outString = ''' 資料更新表單 <br /><br /> <form method=\"post\" action=\"doUpdate\"> ''' outString += "學號:<input type=\"text\" name=\"stud_number\" value=\""+str(stud_number)+"\"><br />" outString += "姓名:<input type=\"text\" name=\"stud_name\" value=\""+str(stud_name)+"\"><br />" outString += "高中科系:<input type=\"text\" name=\"school_dept\" value=\""+str(school_dept)+"\"><br />" outString += "專長:<input type=\"text\" name=\"major\" value=\""+str(major)+"\"><br />" outString += "預計薪資:<input type=\"text\" name=\"expt_salary\" value=\""+str(expt_salary)+"\"><br />" # 將資料對應的 uuid 以隱藏資料送回, 這裡或許會有資料安全疑慮? outString += "<input type=\"hidden\" name=\"id\" value=\""+str(id)+"\"><br />" outString += ''' <input type=\"submit\" value=\"更新\"> <input type=\"reset\" value=\"重填\"> </form> '''+self.menuLink() return outString
def 材料(序號): # 根據資料庫查詢傳回對應的材料 unsno 與 treatment # 利用 Store 建立資料庫檔案對應物件, 並且設定 frozen=True 表示不要開放動態資料表的建立 SQLite連結 = Store(SQLiteWriter("lewis.db", frozen=True)) material = SQLite連結.find_one("steel", "serialno = ?", [序號]) # 針對 material.treatment 中的空白與 big5 資料進行處理 treatment = material.treatment.replace(" ", "_") data = str(material.unsno) + "_" + str(treatment) # 以下的 split() 目的在測試如何分割 unsno 與 treatment # 只針對第一個底線進行分割 unsno, treatment = data.split("_", 1) return unsno + "_" + treatment
def saveData(self, stud_number=None, stud_name=None, \ school_dept=None, major=None, expt_salary=None): ''' # 以 append 附加的方式將資料存入檔案, 以逗點隔開 檔案 = open("c1w10out.txt", 'a', encoding="utf-8") 內容 = str(stud_number)+","+str(stud_name)+","\ +str(school_dept)+","+str(major)+','+str(expt_salary)+"\n" 檔案.write(內容) 檔案.close() ''' # 改為將資料透過 pybean 存入資料庫 library = Store(SQLiteWriter(SQLite_data_dir+"/database.sqlite", frozen=False)) # 動態建立 student 資料表 student = library.new("student") # 改為以 pybean 儲存資料 student.stud_number = stud_number student.stud_name = stud_name student.school_dept = school_dept student.major = major student.expt_salary = expt_salary # 儲存資料表內容 library.save(student) # 配合 pybean 0.2.1 將 commit 移出, save 與 delete 必須要配合 commit 才能更新資料庫 library.commit() # 設法改為 Mako based return str(stud_number)+","+str(stud_name)+","\ +str(school_dept)+","+str(major)+','+str(expt_salary)+", 已經存檔"
def deleteForm(self,id=None): # 設法根據 stud_number 查出對應的欄位資料 # 改為利用 uuid 查出對應的欄位資料, uuid 為各筆資料的唯一代號 個人資料 = [] 資料計數 = 0 # 以下改為以 pybean 儲存資料 library = Store(SQLiteWriter(SQLite_data_dir+"/database.sqlite", frozen=False)) # 動態建立 student 資料表 student = library.new("student") # 以 find_one 找出所要的一筆資料 #一筆資料 = library.find_one("student","stud_number=?",[stud_number]) #UUID(uuid).bytes 為正確的 uuid 欄位資料 # 配合 pybean 0.2.1 將 UUID(uuid).bytes 改為 id 一筆資料 = library.find_one("student","id=?",[id]) # 考量當一筆資料為空時 if 一筆資料 == None: outString = "沒有資料" return outString stud_number = 一筆資料.stud_number stud_name = 一筆資料.stud_name school_dept = 一筆資料.school_dept major = 一筆資料.major expt_salary = 一筆資料.expt_salary outString = ''' 資料刪除表單 <br /><br /> 按下刪除後, 下列資料將會刪除 <br /><br /> <form method=\"post\" action=\"doDelete\"> ''' outString += "學號:"+str(stud_number)+"<br />" outString += "姓名:"+str(stud_name)+"<br />" outString += "高中科系:"+str(school_dept)+"<br />" outString += "專長:"+str(major)+"<br />" outString += "預計薪資:"+str(expt_salary)+"<br />" # 將資料對應的 uuid 以隱藏資料送回, 這裡或許會有資料安全疑慮? outString += "<input type=\"hidden\" name=\"id\" value=\""+str(id)+"\"><br />" outString += ''' <input type=\"submit\" value=\"刪除\"> </form> '''+self.menuLink() return outString
def index(self, *args, **kwargs): # 進行資料庫檔案連結, 並且取出所有資料 try: # 利用 Store 建立資料庫檔案對應物件, 並且設定 frozen=True 表示不要開放動態資料表的建立 # 因為程式以 application 所在目錄執行, 因此利用相對目錄連結 lewis.db 資料庫檔案 SQLite連結 = Store(SQLiteWriter(_curdir + "/lewis.db", frozen=True)) #material = SQLite連結.find_one("steel","serialno = ?",[序號]) # str(SQLite連結.count("steel")) 將傳回 70, 表示資料庫中有 70 筆資料 material = SQLite連結.find("steel") # 所傳回的 material 為 iterator ''' outstring = "" for material_item in material: outstring += str(material_item.serialno) + ":" + material_item.unsno + "_" + material_item.treatment + "<br />" return outstring ''' except: return "抱歉! 資料庫無法連線<br />" outstring = ''' <form method=\"post\" action=\"gear_width"> 請先下載齒輪的檔案,放在V槽跟目錄下,存成黨名gear.prt,在開creo打本網址即可使用<br /> <a href="https://drive.google.com/open?id=0B1DlKZ6juyQeQ0ZMTnBLejJQVkE&authuser=0">creo2載點</a> PS:壓力角內定20度,不可更改<br /> 請填妥下列參數,以完成適當的齒面寬尺寸大小設計。<br /> 馬達馬力:<input type=text name=horsepower id=horsepower value=100 size=10>horse power<br /> 馬達轉速:<input type=text name=rpm id=rpm value=1120 size=10>rpm<br /> 齒輪減速比: <input type=text name=ratio id=ratio value=2 size=10><br /> 齒形:<select name=toothtype id=toothtype> <option value=type1>壓力角20度,a=0.8,b=1.0 <option value=type2>壓力角20度,a=1.0,b=1.25 </select><br /> 安全係數:<input type=text name=safetyfactor id=safetyfactor value=3 size=10><br /> 齒輪材質:<select name=material_serialno id=material_serialno><br /> ''' for material_item in material: outstring += "<option value=" + str(material_item.serialno) + ">UNS - " + \ material_item.unsno + " - " + material_item.treatment outstring += "</select><br />" outstring += "小齒輪齒數:<input type=text name=npinion id=npinion value=12 size=10><br />模數:<input type=\"text\" name=\"m\"><br />" outstring += "<input type=submit id=submit value=進行運算>" outstring += "</form>" return outstring
def index(self, *args, **kwargs): # 進行資料庫檔案連結, 並且取出所有資料 try: # 利用 Store 建立資料庫檔案對應物件, 並且設定 frozen=True 表示不要開放動態資料表的建立 # 因為程式以 application 所在目錄執行, 因此利用相對目錄連結 lewis.db 資料庫檔案 SQLite連結 = Store(SQLiteWriter(_curdir + "/lewis.db", frozen=True)) #material = SQLite連結.find_one("steel","serialno = ?",[序號]) # str(SQLite連結.count("steel")) 將傳回 70, 表示資料庫中有 70 筆資料 material = SQLite連結.find("steel") # 所傳回的 material 為 iterator ''' outstring = "" for material_item in material: outstring += str(material_item.serialno) + ":" + material_item.unsno + "_" + material_item.treatment + "<br />" return outstring ''' except: return "抱歉! 資料庫無法連線<br />" outstring = ''' <form id=entry method=post action="gear_width"> 請填妥下列參數,以完成適當的齒尺寸大小設計。<br /> 齒數:<input type=text name=n><br /> 模數:<input type=text name=module><br /> 馬達馬力:<input type=text name=horsepower id=horsepower value=100 size=10>horse power<br /> 馬達轉速:<input type=text name=rpm id=rpm value=1120 size=10>rpm<br /> 齒輪減速比: <input type=text name=ratio id=ratio value=4 size=10><br /> 齒形:<select name=toothtype id=toothtype> <option value=type1>壓力角20度,a=0.8,b=1.0 <option value=type2>壓力角20度,a=1.0,b=1.25 <option value=type3>壓力角25度,a=1.0,b=1.25 <option value=type4>壓力角25度,a=1.0,b=1.35 </select><br /> 安全係數:<input type=text name=safetyfactor id=safetyfactor value=3 size=10><br /> 齒輪材質:<select name=material_serialno id=material_serialno> ''' for material_item in material: outstring += "<option value=" + str(material_item.serialno) + ">UNS - " + \ material_item.unsno + " - " + material_item.treatment outstring += "</select><br />" outstring += "小齒輪齒數:<input type=text name=npinion id=npinion value=18 size=10><br />" outstring += "<input type=submit id=submit value=進行運算>" outstring += "</form>" return outstring
def gentable(self): ''' 2013.01.20 改為 pybean 資料庫存取 本程式的目的在讀取 401231_out2.txt 學生分組名單, 依據各學員學號姓名以及座號, 建立所需的資料連結表格, 並透過連結允許使用者附加資料. ''' library = Store(SQLiteWriter(SQLite_data_dir+"/student.sqlite", frozen=False)) grouping = library.new("st908306") # 建立一個空數列 st_array = list() # 建立一個空字典 st_dict = {} 組別 = 0 #=============================================================== # 接著進入程式的第二階段 #=============================================================== # 接下來進行上述 st_array 的應用 # 用來判定表格是否有資料的變數 有資料 = 0 # 開始準備列印網際表單 fileout = open("400232_out_w13.html","w",encoding='utf-8') # 列出目前的資料庫中 st908306 資料表的內容 if (library.count("st908306") == 0): outString = "沒有資料, 請先輸入資料!<br />" outString += self.menuLink() return outString else: total = library.count("st908306") # 這裡有兩種選擇, 從資料庫中弄出與先前相同的 st_array 或者直接由資料庫中查出所要的資料 # 改為從資料庫中的資料表計算總學員數 #total = len(st_array) # 建立 table 表格 # 加入超文件標頭 超文件標頭 = ''' <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>分組座位表</title> </head> <body> ''' fileout.write(超文件標頭) # 除了寫檔外, 利用 outString 來收集超文件字串, 最後再以 return 傳回 outString = 超文件標頭 fileout.write("<center>黑板<br /><br /><table border=1>") outString += "<center>黑板<br /><br /><table border=1>" #_23_56_89_11,12 # 因為最多 12 組, 增加的組連結行數共有 3 行 組加行 = 3 # 每一列可容納的組數為 3 每列組數 = 3 組累計 = 0 列累計 = 0 目前組別 = 0 for 列 in range(1,10+組加行): # 希望在第 1, 4 ,7 列加上組別連結 if 列 in [1, 4, 7, 10]: 組累計 += 1 fileout.write("<tr>") outString += "<tr>" for 行 in range(9,0,-1): # 組別連結將會放在第 2, 5, 8 行的位置, 且各橫跨 3 行 if 行 in [2, 5, 8] : 列累計 += 1 #目前組別 = 列+每列組數*組累計-列累計 fileout.write("<td colspan=\"3\" align=\"center\">第"+str(列+每列組數*組累計-列累計)+"組</td>") outString += "<td colspan=\"3\" align=\"center\">第"+str(列+每列組數*組累計-列累計)+"組</td>" fileout.write("</tr>") outString += "</tr>" else: # 將組累計數從增加的列數中扣除, 得到在未加上組連結列之前的列序號, 列 -= 組累計 fileout.write("<tr>") outString += "<tr>" for 行 in range(9,0,-1): # 設法逐一掃過所有資料, 以便找出符合列印行數與列數的資料 for 資料 in library.find("st908306","1"): if(資料.col == 行 and 資料.row == 列): fileout.write("<td>"+str(資料.stud_number)+str(資料.stud_name)+"</td>") outString += "<td><a href=index?stud_number="+str(資料.stud_number)+\ "&stud_name="+str(資料.stud_name)+">"+str(資料.stud_number)+\ str(資料.stud_name)+"</a></td>" 有資料 = 1 if(有資料 == 0): fileout.write("<td> </td>") outString += "<td> </td>" else: 有資料 = 0 fileout.write("</tr>") outString += "</tr>" fileout.write("</table></center>") outString += "</table></center>" 超文件收尾 = ''' </body> </html> ''' fileout.write(超文件收尾) outString += 超文件收尾 # 最後記得附加連結表單 outString += self.menuLink() # 改為資料庫存取後, 沒有 filein #filein.close() fileout.close() # 將已經拼湊好的 outString 傳回瀏覽器 return outString
def gear_width(self, horsepower=100, rpm=1000, ratio=4, toothtype=1, safetyfactor=2, material_serialno=1, npinion=18): SQLite連結 = Store(SQLiteWriter(_curdir + "/lewis.db", frozen=True)) outstring = "" # 根據所選用的齒形決定壓力角 if (toothtype == 1 or toothtype == 2): 壓力角 = 20 else: 壓力角 = 25 # 根據壓力角決定最小齒數 if (壓力角 == 20): 最小齒數 = 18 else: 最小齒數 = 12 # 直接設最小齒數 if int(npinion) <= 最小齒數: npinion = 最小齒數 # 大於400的齒數則視為齒條(Rack) if int(npinion) >= 400: npinion = 400 # 根據所選用的材料查詢強度值 # 由 material之序號查 steel 表以得材料之降伏強度S單位為 kpsi 因此查得的值要成乘上1000 # 利用 Store 建立資料庫檔案對應物件, 並且設定 frozen=True 表示不要開放動態資料表的建立 #SQLite連結 = Store(SQLiteWriter("lewis.db", frozen=True)) # 指定 steel 資料表 steel = SQLite連結.new("steel") # 資料查詢 #material = SQLite連結.find_one("steel","unsno=? and treatment=?",[unsno, treatment]) material = SQLite連結.find_one("steel", "serialno=?", [material_serialno]) # 列出 steel 資料表中的資料筆數 #print(SQLite連結.count("steel")) #print (material.yield_str) strengthstress = material.yield_str * 1000 # 由小齒輪的齒數與齒形類別,查詢lewis form factor # 先查驗是否有直接對應值 on_table = SQLite連結.count("lewis", "gearno=?", [npinion]) if on_table == 1: # 直接進入設計運算 #print("直接運算") #print(on_table) lewis_factor = SQLite連結.find_one("lewis", "gearno=?", [npinion]) #print(lewis_factor.type1) # 根據齒形查出 formfactor 值 if (toothtype == 1): formfactor = lewis_factor.type1 elif (toothtype == 2): formfactor = lewis_factor.type2 elif (toothtype == 3): formfactor = lewis_factor.type3 else: formfactor = lewis_factor.type4 else: # 沒有直接對應值, 必須進行查表內插運算後, 再執行設計運算 #print("必須內插") #print(interpolation(npinion, gear_type)) formfactor = self.interpolation(npinion, toothtype) # 開始進行設計運算 ngear = int(npinion) * int(ratio) # 重要的最佳化設計---儘量用整數的diametralpitch # 先嘗試用整數算若 diametralpitch 找到100 仍無所獲則改用 0.25 作為增量再不行則宣告 fail counter = 0 i = 0.1 facewidth = 0 circularpitch = 0 while (facewidth <= 3 * circularpitch or facewidth >= 5 * circularpitch): diametralpitch = i #circularpitch = 3.14159/diametralpitch circularpitch = math.pi / diametralpitch pitchdiameter = int(npinion) / diametralpitch #pitchlinevelocity = 3.14159*pitchdiameter*rpm/12 pitchlinevelocity = math.pi * pitchdiameter * float(rpm) / 12 transmittedload = 33000 * float(horsepower) / pitchlinevelocity velocityfactor = 1200 / (1200 + pitchlinevelocity) # formfactor is Lewis form factor # formfactor need to get from table 13-3 and determined ty teeth number and type of tooth # formfactor = 0.293 # 90 is the value get from table corresponding to material type facewidth = transmittedload * diametralpitch * float( safetyfactor) / velocityfactor / formfactor / strengthstress if (counter > 5000): outstring += "超過5000次的設計運算,仍無法找到答案!<br />" outstring += "可能所選用的傳遞功率過大,或無足夠強度的材料可以使用!<br />" # 離開while迴圈 break i += 0.1 counter += 1 facewidth = round(facewidth, 4) if (counter < 5000): # 先載入 cube 程式測試 #outstring = self.cube_weblink() # 再載入 gear 程式測試 outstring = self.gear_weblink() outstring += "進行" + str( counter) + "次重複運算後,得到合用的facewidth值為:" + str(facewidth) return outstring
#@+leo-ver=4-thin #@+node:amd_yen.20130501090735.2391:@shadow c2/get_score.py #encoding: utf-8 '''這個程式將配合 TCExam 所轉出來的 txt 成績檔案, 以遞迴方式讀取檔案 並將要取出的資料存入資料庫中 ''' import os, fnmatch # 導入 pybean 模組與所要使用的 Store 及 SQLiteWriter 方法 from pybean import Store, SQLiteWriter # 利用 Store 建立資料庫檔案對應物件, 並且設定 frozen=False 表示要開放動態資料表的建立 library = Store(SQLiteWriter("2013exam.sqlite", frozen=False)) # 定義一個可以遞迴進入目錄搜尋特定檔案的函式 def find_files(directory, pattern): for root, dirs, files in os.walk(directory): for basename in files: if fnmatch.fnmatch(basename, pattern): filename = os.path.join(root, basename) yield filename # 定義一個讀取檔案所有內容的函式 def read_lines(input_filename): output_list = [] f = open(input_filename, encoding="UTF-8") while True: l = f.readlines() if len(l) == 0: break output_list.append(l) f.close() return output_list
# 利用 len() 求出數列元素個數 total = len(st_array) # 每組人數設為 num num = 6 # 辨識組別的變數 order, 由第 1 組開始 order = 1 # 用來計算學生序號的 count 變數 count = 0 # 利用 for 迴圈逐一進行分組資料列印 for i in range(0, total): 學生分組資料.append(st_array[i][0] + st_array[i][1].strip() + "," + str(seat2(10)[i][0]) + "," + str(seat2(10)[i][1]) + "," + str(order)) # 以 pybean 儲存資料, 若非在每次存檔之前都執行下兩行, 資料庫只會儲存最後一筆資料 (會進行資料覆蓋?) library = Store(SQLiteWriter("student.sqlite", frozen=False)) # 動態建立 student 資料表 grouping = library.new("st908306") grouping.stud_number = st_array[i][0] grouping.stud_name = st_array[i][1].strip() grouping.col = seat2(10)[i][0] grouping.row = seat2(10)[i][1] grouping.grp = order # 儲存資料表內容 library.save(grouping) if (i % num == 0): fileout2.write("第" + str(order) + "組:------\n") count = count + 1 # 寫出學號, 姓名 fileout2.write(st_array[i][0] + st_array[i][1].strip()) # 寫出座號, seat2 為電腦教室編號, seat 則為 spread sheet 上的編號
def get_fluid_save(self): return Store(SQLiteWriter(":memory:", False))
def get_frozen_save(self): return Store(SQLiteWriter(":memory:"))
def doSearch(self, keyword=None, key_num=0, rev_order=False, itemperpage=9, page=1): # 這裡的搜尋結果分頁, 先確定搜尋筆數, 然後配合每頁筆數與所在頁面進行局部搜尋, 並將搜尋關鍵字存在 session, 在 Mako 版型檔中取出配合不同頁面進行顯示 # 以下改為以 pybean 儲存資料 # 取 count session 值, 內定為 0: cherrypy.session.get('count', 0) # 設定 session 中 count 的值: cherrypy.session['count'] = count # 先將 keyword 放到 session 中 if keyword != "": cherrypy.session['keyword'] = keyword else: # 利用 Mako 傳回相關資料 return "請輸入查詢關鍵字(非 Mako)" library = Store(SQLiteWriter(SQLite_data_dir+"/database.sqlite", frozen=False)) # 動態建立 student 資料表 student = library.new("student") # 以 find 找出所要的一筆資料 # 設法利用所有欄位名稱拼湊查詢字串 次序 = 1 查詢字串 = "" for 索引 in ["stud_number", "stud_name", "school_dept", "major", "expt_salary"]: if 次序 == 1: 查詢字串 += "("+索引+" like \"%"+keyword+"%\")" else: 查詢字串 += "OR ("+索引+" like \"%"+keyword+"%\")" 次序 += 1 #查詢資料 = library.find("student",查詢字串) # 先確認查詢總筆數 totalitem = library.count("student",查詢字串) # 處理查詢無資料傳回的情況 if totalitem == 0: return "查無對應資料" totalpage = math.ceil(totalitem/int(itemperpage)) # 若索引從 1 開始 #starti = int(itemperpage) * (int(page) - 1) + 1 # 若索引從 0 開始 starti = int(itemperpage) * (int(page) - 1) endi = starti + int(itemperpage) - 1 查詢資料 = library.find("student", 查詢字串+" order by stud_number limit "+str(starti)+", "+str(itemperpage)) 個人資料 = [] for 資料 in 查詢資料: #for 資料 in library.find("student","1 order by stud_number limit 1,10"): 學號 = 資料.stud_number 姓名 = 資料.stud_name 科系 = 資料.school_dept 專長 = 資料.major 薪水 = 資料.expt_salary #更新連結 = "<a href=\"updateForm?stud_number="+學號+"\">"+學號+"</a>" # 配合 pybean 0.2.1 將 uuid 改為 id 更新連結 = "<a href=\"updateForm?id="+str(資料.id)+"\">"+學號+"</a>" 更新 = "<a href=\"updateForm?id="+str(資料.id)+"\">更新</a>" 刪除 = "<a href=\"deleteForm?id="+str(資料.id)+"\">刪除</a>" 個人資料.append([更新連結,姓名,科系,專長,薪水,更新,刪除]) # 是否要將搜尋所得的個人資料, 以 Mako 套稿處理 # # 根據數列中各 tuple 中的學號(亦即 key data[0])進行排序 # 個人資料排列後, 必須加以指定成變數, 否則個人資料的內容排序後並未改變 #個人資料 = sorted(個人資料, key=lambda data: data[0], reverse=True) 個人資料 = sorted(個人資料, key=lambda data: data[int(key_num)], reverse=int(rev_order)) #print(個人資料) #outString = self.htmlList(個人資料) # 由於搜尋結果的分頁顯示, 為特定內容的分頁顯示與一般全部資料的顯示是否要進行區分, 必須在此作決定 # 由於所有資料的分頁顯示, 是以 itemperpage 與 page 針對所有資料進行資料庫查詢後, 逐批送到 pageList 進行顯示 # 而資料搜尋是否要每次利用相同的流程處理顯示, 抑或透過將搜尋資料存入 session 然後再由 session 分頁取出顯示, 效益有何差別 # 也必須在此進行分析 outString = self.spageList(個人資料,totalitem,itemperpage,page) return outString
def gear_width(馬力, 轉速, 減速比, 齒形, 安全係數, 材料, 小齒輪齒數): # 根據所選用的齒形決定壓力角 if(齒形 == 1 or 齒形 == 2): 壓力角 = 20 else: 壓力角 = 25 # 根據壓力角決定最小齒數 if(壓力角== 20): 最小齒數 = 18 else: 最小齒數 = 12 # 直接設最小齒數 if 小齒輪齒數 <= 最小齒數: 小齒輪齒數 = 最小齒數 # 大於400的齒數則視為齒條(Rack) if 小齒輪齒數 >= 400: 小齒輪齒數 = 400 # 根據所選用的材料查詢強度值 # 由 material之序號查 steel 表以得材料之降伏強度S單位為 kpsi 因此查得的值要成乘上1000 # 利用 Store 建立資料庫檔案對應物件, 並且設定 frozen=True 表示不要開放動態資料表的建立 SQLite連結 = Store(SQLiteWriter("lewis.db", frozen=True)) # 指定 steel 資料表 steel = SQLite連結.new("steel") # 資料查詢 # 將 unsno 與 treatment 從材料字串中隔開 unsno, treatment = 材料.split("_") #print(unsno, treatment) material = SQLite連結.find_one("steel","unsno=? and treatment=?",[unsno, treatment]) # 列出 steel 資料表中的資料筆數 #print(SQLite連結.count("steel")) print (material.yield_str) strengthstress = material.yield_str*1000 # 由小齒輪的齒數與齒形類別,查詢lewis form factor # 先查驗是否有直接對應值 on_table = SQLite連結.count("lewis","gearno=?",[小齒輪齒數]) if on_table == 1: # 直接進入設計運算 print("直接運算") print(on_table) lewis_factor = SQLite連結.find_one("lewis","gearno=?",[小齒輪齒數]) #print(lewis_factor.type1) # 根據齒形查出 formfactor 值 if(齒形 == 1): formfactor = lewis_factor.type1 elif(齒形 == 2): formfactor = lewis_factor.type2 elif(齒形 == 3): formfactor = lewis_factor.type3 else: formfactor = lewis_factor.type4 else: # 沒有直接對應值, 必須進行查表內插運算後, 再執行設計運算 print("必須內插") #print(interpolation(小齒輪齒數, 齒形)) formfactor = interpolation(小齒輪齒數, 齒形) # 開始進行設計運算 ngear = 小齒輪齒數 * 減速比 # 重要的最佳化設計---儘量用整數的diametralpitch # 先嘗試用整數算若 diametralpitch 找到100 仍無所獲則改用 0.25 作為增量再不行則宣告 fail counter = 0 i = 0.1 facewidth = 0 circularpitch = 0 while (facewidth <= 3 * circularpitch or facewidth >= 5 * circularpitch): diametralpitch = i #circularpitch = 3.14159/diametralpitch circularpitch = math.pi/diametralpitch pitchdiameter = 小齒輪齒數/diametralpitch #pitchlinevelocity = 3.14159*pitchdiameter*轉速/12 pitchlinevelocity = math.pi * pitchdiameter * 轉速/12 transmittedload = 33000 * 馬力/pitchlinevelocity velocityfactor = 1200/(1200 + pitchlinevelocity) # formfactor is Lewis form factor # formfactor need to get from table 13-3 and determined ty teeth number and type of tooth # formfactor = 0.293 # 90 is the value get from table corresponding to material type facewidth = transmittedload * diametralpitch * 安全係數/velocityfactor/formfactor/strengthstress if(counter>5000): print("超過5000次的設計運算,仍無法找到答案!") print("可能所選用的傳遞功率過大,或無足夠強度的材料可以使用!") # 離開while迴圈 break i += 0.1 counter += 1 facewidth = round(facewidth, 4) if(counter<5000): print("進行"+str(counter)+"次重複運算後,得到合用的facewidth值為:"+str(facewidth))
#@+leo-ver=4-thin #@+node:amd_yen.20130501090735.3343:@shadow c2/list_score.py #encoding: utf-8 from get_score import * from pybean import Store, SQLiteWriter library = Store(SQLiteWriter("2013exam.sqlite", frozen=False)) def get_studid(filename, course_title, count): print(course_title) fail = 0 student_list = read_lines(filename) for 索引 in range(len(student_list[0])): item = None student_id = ((student_list[0][索引]).rstrip()).split('\t')[0] score = get_score(course_title, student_id) total = 0 average = 0 for item in score: points = float(item.points) if points < 0: points = 0 total += points #print(item.student, item.title, item.exam, item.points) # 假設大家都 100 分 期中報告成績 = 100 報告比例 = 0.2 考試比例 = 0.8 #(分組加分) 全班加分 = 2
def gentable2(self): library = Store(SQLiteWriter(SQLite_data_dir+"/student.sqlite", frozen=False)) grouping = library.new("st908306") # 建立一個空數列 st_array = list() mako_array = list() # 資料庫查詢 1 表示全部資料, row 欄位正向排序, col 則逆向排序 for 資料 in library.find("st908306","1 order by row,col DESC"): #print(資料.stud_number,資料.stud_name,資料.row,資料.col) mako_array.append("<a href=>"+資料.stud_number+"<br />"+資料.stud_name+"</a>") #print(mako_array) #套稿查詢 = TemplateLookup(directories=['./templates'], output_encoding='utf-8', encoding_errors='replace') # templates 為絕對目錄 套稿查詢 = TemplateLookup(directories=[template_root_dir+"/templates"]) # 必須要從 templates 目錄取出 index.html 座位套稿 = 套稿查詢.get_template("gentable.html") #座位套稿 = Template(filename='templates/index.html', format_exceptions=True, lookup=套稿查詢) #return 座位套稿.render(currentTime=datetime.datetime.now()) return 座位套稿.render(st_array=mako_array) # ############################################# # 以上使用 Mako 套稿處理頁面, 以下程式段應該不再需要 # ############################################# # 建立一個空字典 st_dict = {} 組別 = 0 有資料 = 0 # 開始準備列印網際表單 fileout = open("400232_out_w13.html","w",encoding='utf-8') # 列出目前的資料庫中 st908306 資料表的內容 if (library.count("st908306") == 0): outString = "沒有資料, 請先輸入資料!<br />" outString += self.menuLink() return outString else: total = library.count("st908306") 超文件標頭 = ''' <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>分組座位表</title> </head> <body> ''' fileout.write(超文件標頭) # 除了寫檔外, 利用 outString 來收集超文件字串, 最後再以 return 傳回 outString = 超文件標頭 fileout.write("<center>黑板<br /><br /><table border=1>") outString += "<center>黑板<br /><br /><table border=1>" #_23_56_89_11,12 # 因為最多 12 組, 增加的組連結行數共有 3 行 組加行 = 3 # 每一列可容納的組數為 3 每列組數 = 3 組累計 = 0 列累計 = 0 目前組別 = 0 for 列 in range(1,10+組加行): # 希望在第 1, 4 ,7 列加上組別連結 if 列 in [1, 4, 7, 10]: 組累計 += 1 fileout.write("<tr>") outString += "<tr>" for 行 in range(9,0,-1): # 組別連結將會放在第 2, 5, 8 行的位置, 且各橫跨 3 行 if 行 in [2, 5, 8] : 列累計 += 1 #目前組別 = 列+每列組數*組累計-列累計 fileout.write("<td colspan=\"3\" align=\"center\">第"+str(列+每列組數*組累計-列累計)+"組</td>") outString += "<td colspan=\"3\" align=\"center\">第"+str(列+每列組數*組累計-列累計)+"組</td>" fileout.write("</tr>") outString += "</tr>" else: # 將組累計數從增加的列數中扣除, 得到在未加上組連結列之前的列序號, 列 -= 組累計 fileout.write("<tr>") outString += "<tr>" for 行 in range(9,0,-1): # 設法逐一掃過所有資料, 以便找出符合列印行數與列數的資料 for 資料 in library.find("st908306","1"): if(資料.col == 行 and 資料.row == 列): fileout.write("<td>"+str(資料.stud_number)+str(資料.stud_name)+"</td>") outString += "<td><a href=index?stud_number="+str(資料.stud_number)+\ "&stud_name="+str(資料.stud_name)+">"+str(資料.stud_number)+\ str(資料.stud_name)+"</a></td>" 有資料 = 1 if(有資料 == 0): fileout.write("<td> </td>") outString += "<td> </td>" else: 有資料 = 0 fileout.write("</tr>") outString += "</tr>" fileout.write("</table></center>") outString += "</table></center>" 超文件收尾 = ''' </body> </html> ''' fileout.write(超文件收尾) outString += 超文件收尾 # 最後記得附加連結表單 outString += self.menuLink() # 改為資料庫存取後, 沒有 filein #filein.close() fileout.close() # 將已經拼湊好的 outString 傳回瀏覽器 return outString
# -*- coding: utf-8 -*- # http://try.taobao.com/json/ajax_get_item_list.htm?tab=2&page=13&anchor=true&t=1367128346939&_input_charset=utf-8 import urllib2 import time import json from pybean import Store, SQLiteWriter db = Store(SQLiteWriter("taobao.db", frozen=False)) def getList(page): # page >=1 print "page:", page url = "http://try.taobao.com/json/ajax_get_item_list.htm?tab=2&page=%s&anchor=true&t=1367128346939&_input_charset=utf-8" % ( page, ) js = urllib2.urlopen(url).read() items = json.loads(js) now = time.time() tm = time.gmtime(now + 8 * 3600) date = tm.tm_year * 10000 + tm.tm_mon * 100 + tm.tm_mday for item in items["data"]: print item["itemId"] tb = db.new("freetry") for key in item: tb.__setattr__(key, item[key]) tb.gailv = item["requestNum"] / item["totalNum"] # item["_id"] = item["itemId"] # item["gailv"] = item["requestNum"] / item["totalNum"] if db.find_one("freetry", "itemId=?", (item["itemId"], )):
from pybean import Store, SQLiteWriter library = Store(SQLiteWriter(":memory:", frozen=False)) book = library.new("book") book.title = "Boost development with pybean" book.author = "Charles Xavier" library.save(book) for book in library.find("book", "author like ?", ["Charles Xavier"]): print book.title library.delete(book) library.commit()
random.shuffle(st_array) #print(st_array) # 利用 len() 求出數列元素個數 total = len(st_array) # 每組人數設為 num num = 6 # 辨識組別的變數 order, 由第 1 組開始 order = 1 # 用來計算學生序號的 count 變數 count = 0 # 利用 for 迴圈逐一進行分組資料列印 for i in range(0,total): 學生分組資料.append(st_array[i][0]+st_array[i][1].strip()+","+str(seat2(10)[i][0])+","+str(seat2(10)[i][1])+","+str(order)) # 以 pybean 儲存資料, 若非在每次存檔之前都執行下兩行, 資料庫只會儲存最後一筆資料 (會進行資料覆蓋?) library = Store(SQLiteWriter("student.sqlite", frozen=False)) # 動態建立 student 資料表 grouping = library.new("st908306") grouping.stud_number = st_array[i][0] grouping.stud_name = st_array[i][1].strip() grouping.col= seat2(10)[i][0] grouping.row = seat2(10)[i][1] grouping.grp = order # 儲存資料表內容 library.save(grouping) if(i%num == 0): fileout2.write("第"+str(order)+"組:------\n") count = count + 1 # 寫出學號, 姓名 fileout2.write(st_array[i][0]+st_array[i][1].strip()) # 寫出座號, seat2 為電腦教室編號, seat 則為 spread sheet 上的編號
def gear_width(self, horsepower=100, rpm=1000, ratio=4, toothtype=1, safetyfactor=2, material_serialno=1, npinion=18, facewidth=None, n=None, m=None): SQLite連結 = Store(SQLiteWriter(_curdir + "/lewis.db", frozen=True)) outstring = "" # 根據所選用的齒形決定壓力角 if (toothtype == 1 or toothtype == 2): 壓力角 = 20 else: 壓力角 = 25 # 根據壓力角決定最小齒數 if (壓力角 == 20): 最小齒數 = 18 else: 最小齒數 = 12 # 直接設最小齒數 if int(npinion) <= 最小齒數: npinion = 最小齒數 # 大於400的齒數則視為齒條(Rack) if int(npinion) >= 400: npinion = 400 # 根據所選用的材料查詢強度值 # 由 material之序號查 steel 表以得材料之降伏強度S單位為 kpsi 因此查得的值要成乘上1000 # 利用 Store 建立資料庫檔案對應物件, 並且設定 frozen=True 表示不要開放動態資料表的建立 #SQLite連結 = Store(SQLiteWriter("lewis.db", frozen=True)) # 指定 steel 資料表 steel = SQLite連結.new("steel") # 資料查詢 #material = SQLite連結.find_one("steel","unsno=? and treatment=?",[unsno, treatment]) material = SQLite連結.find_one("steel", "serialno=?", [material_serialno]) # 列出 steel 資料表中的資料筆數 #print(SQLite連結.count("steel")) #print (material.yield_str) strengthstress = material.yield_str * 1000 # 由小齒輪的齒數與齒形類別,查詢lewis form factor # 先查驗是否有直接對應值 on_table = SQLite連結.count("lewis", "gearno=?", [npinion]) if on_table == 1: # 直接進入設計運算 #print("直接運算") #print(on_table) lewis_factor = SQLite連結.find_one("lewis", "gearno=?", [npinion]) #print(lewis_factor.type1) # 根據齒形查出 formfactor 值 if (toothtype == 1): formfactor = lewis_factor.type1 elif (toothtype == 2): formfactor = lewis_factor.type2 elif (toothtype == 3): formfactor = lewis_factor.type3 else: formfactor = lewis_factor.type4 else: # 沒有直接對應值, 必須進行查表內插運算後, 再執行設計運算 #print("必須內插") #print(interpolation(npinion, gear_type)) formfactor = self.interpolation(npinion, toothtype) # 開始進行設計運算 ngear = int(npinion) * int(ratio) # 重要的最佳化設計---儘量用整數的diametralpitch # 先嘗試用整數算若 diametralpitch 找到100 仍無所獲則改用 0.25 作為增量再不行則宣告 fail counter = 0 i = 0.1 facewidth = 0 circularpitch = 0 while (facewidth <= 3 * circularpitch or facewidth >= 5 * circularpitch): diametralpitch = i #circularpitch = 3.14159/diametralpitch circularpitch = math.pi / diametralpitch pitchdiameter = int(npinion) / diametralpitch #pitchlinevelocity = 3.14159*pitchdiameter*rpm/12 pitchlinevelocity = math.pi * pitchdiameter * float(rpm) / 12 transmittedload = 33000 * float(horsepower) / pitchlinevelocity velocityfactor = 1200 / (1200 + pitchlinevelocity) # formfactor is Lewis form factor # formfactor need to get from table 13-3 and determined ty teeth number and type of tooth # formfactor = 0.293 # 90 is the value get from table corresponding to material type facewidth = transmittedload * diametralpitch * float( safetyfactor) / velocityfactor / formfactor / strengthstress if (counter > 5000): outstring += "超過5000次的設計運算,仍無法找到答案!<br />" outstring += "可能所選用的傳遞功率過大,或無足夠強度的材料可以使用!<br />" # 離開while迴圈 break i += 0.1 counter += 1 facewidth = round(facewidth, 4) if (counter < 5000): # 先載入 cube 程式測試 #outstring = self.cube_weblink() # 再載入 gear 程式測試 outstring = '''<script type="text/javascript" src="/static/weblink/pfcUtils.js"></script> <script type="text/javascript" src="/static/weblink/wl_header.js">// <![CDATA[ document.writeln ("Error loading Pro/Web.Link header!"); // ]]></script> <script type="text/javascript" language="JavaScript">// <![CDATA[ if (!pfcIsWindows()) netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); // 若第三輸入為 false, 表示僅載入 session, 但是不顯示 // ret 為 model open return var ret = document.pwl.pwlMdlOpen("gear.prt", "v:/", false); if (!ret.Status) { alert("pwlMdlOpen failed (" + ret.ErrorCode + ")"); } //將 ProE 執行階段設為變數 session var session = pfcGetProESession(); // 在視窗中打開零件檔案, 並且顯示出來 var window = session.OpenFile(pfcCreate("pfcModelDescriptor").CreateFromFileName("gear.prt")); var solid = session.GetModel("gear.prt",pfcCreate("pfcModelType").MDL_PART); var length,width,myf,myn,i,j,volume,count,d1Value,d2Value; // 將模型檔中的 length 變數設為 javascript 中的 length 變數 length = solid.GetParam("n"); // 將模型檔中的 width 變數設為 javascript 中的 width 變數 width = solid.GetParam("face_width"); //改變零件尺寸 //myf=20; //myn=20; volume=0; count=0; try { // 以下採用 URL 輸入對應變數 //createParametersFromArguments (); // 以下則直接利用 javascript 程式改變零件參數 for(i=0;i<=5;i++) { //for(j=0;j<=2;j++) //{ myf=''' + str(n) + '''; myn=''' + str(facewidth) + ''' mym=''' + str(m) + ''' // 設定變數值, 利用 ModelItem 中的 CreateDoubleParamValue 轉換成 Pro/Web.Link 所需要的浮點數值 //d1Value = pfcCreate ("MpfcModelItem").CreateDoubleParamValue(myf); d1Value = pfcCreate ("MpfcModelItem").CreateIntParamValue(myf); d2Value = pfcCreate ("MpfcModelItem").CreateDoubleParamValue(myn); // 將處理好的變數值, 指定給對應的零件變數 length.Value = d1Value; width.Value = d2Value; //零件尺寸重新設定後, 呼叫 Regenerate 更新模型 solid.Regenerate(void null); //利用 GetMassProperty 取得模型的質量相關物件 properties = solid.GetMassProperty(void null); //volume = volume + properties.Volume; volume = properties.Volume; count = count + 1; alert("執行第"+count+"次,零件總體積:"+volume); // 將零件存為新檔案 var newfile = document.pwl.pwlMdlSaveAs("gear.prt", "v:/", "mygear_"+count+".prt"); if (!newfile.Status) { alert("pwlMdlSaveAs failed (" + newfile.ErrorCode + ")"); } //} // 內圈 for 迴圈 } //外圈 for 迴圈 //alert("共執行:"+count+"次,零件總體積:"+volume); //alert("零件體積:"+properties.Volume); //alert("零件體積取整數:"+Math.round(properties.Volume)); } catch(err) { alert ("Exception occurred: "+pfcGetExceptionType (err)); } // ]]></script> ''' outstring += "進行" + str(counter) + "次重複運算後,得到合用的facewidth值為:" + str( facewidth) + '' + str(n) + '' + str(m) + '' return outstring
#encoding: utf-8 '''這個程式將配合 TCExam 所轉出來的 txt 成績檔案, 以遞迴方式讀取檔案 並將要取出的資料存入資料庫中 ''' import os, fnmatch # 導入 pybean 模組與所要使用的 Store 及 SQLiteWriter 方法 from pybean import Store, SQLiteWriter # 利用 Store 建立資料庫檔案對應物件, 並且設定 frozen=False 表示要開放動態資料表的建立 library = Store(SQLiteWriter("2013exam.sqlite", frozen=False)) # 定義一個可以遞迴進入目錄搜尋特定檔案的函式 def find_files(directory, pattern): for root, dirs, files in os.walk(directory): for basename in files: if fnmatch.fnmatch(basename, pattern): filename = os.path.join(root, basename) yield filename # 定義一個讀取檔案所有內容的函式 def read_lines(input_filename): output_list = [] f = open(input_filename, encoding="UTF-8") while True: l = f.readlines() if len(l) == 0: break output_list.append(l) f.close() return output_list
"其中 S 為齒面的材料彎曲應力強度"; "設計要求:控制所選齒的尺寸大小,在滿足強度與傳輸負載的要求下,讓齒面厚度介於3倍周節與5倍周節之間。"; "設計者可以選擇的參數:"; "安全係數(建議值為3以上)"; "齒輪減速比"; "馬達傳輸功率,單位為 horse power"; "馬達轉速,單位為 rpm"; "齒制(Gear System)"; "齒輪材料與強度"; ''' # 這個程式要計算正齒輪的齒面寬, 資料庫連結希望使用 pybean 與 SQLite # 導入 pybean 模組與所要使用的 Store 及 SQLiteWriter 方法 from pybean import Store, SQLiteWriter import math SQLite連結 = Store(SQLiteWriter("lewis.db", frozen=True)) # 執行 formfactor 內插運算的函式 def interpolation(小齒輪齒數, 齒形): global SQLite連結 # 使用內插法求值 # 找出比目標齒數大的其中的最小的,就是最鄰近的大值 lewis_factor = SQLite連結.find_one("lewis","gearno > ?",[小齒輪齒數]) if(齒形 == 1): larger_formfactor = lewis_factor.type1 elif(齒形 == 2): larger_formfactor = lewis_factor.type2 elif(齒形 == 3): larger_formfactor = lewis_factor.type3 else: larger_formfactor = lewis_factor.type4