django + celery + channels.websocket 非同步任務
作業要求參見https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11206
1、功能1小檔案輸入。 為表明程式能跑,結果真實而不是迫害老五,請他親自鍵
盤在控制檯下輸入命令。
功能一重點難點:1、由於一開始沒有頭緒,不知道該使用什麼語言來寫更加方便,經過請教學長和我所掌握的語言的熟練程度最終選擇用python語言來完成。
2、由於使用控制檯和命令列輸入和輸出,所以要把.py檔案轉換成.exe檔案,這就用到pyinstalle。
功能一實現程式碼介紹:先定義計算檔案單詞的函式,然後開啟檔案,利用read()讀取檔案全部內容,然後呼叫統計單詞的函式。
def jisuanTotal(word): user_counters=Counter(word) total=0 for user_counter in user_counters: total+=1 print("total "+str(total)+" words\n") lsts=user_counters.most_common(10) for lst in lsts: print("%s \t\t\t%d"%(lst[0],lst[1])) def word_list(filename): with open(filename,encoding='utf-8') as f: content=f.read() words=re.findall(r'[\w^-]+',content)
結果展示:
2、功能二 支援命令列輸入英文作品的檔名,請老五親自錄入。
重點程式碼展示:首先開啟檔案,用read()方法讀取檔案全部內容,然後再使用功能一的函式對單詞進行輸出。功能二改進了功能一所沒有的異常處理,檔案不存在時輸出檔名+does not exist.
def file_name(path): path=path+'.txt' try: with open(path,encoding='utf-8') as f: content=f.read() except FileNotFoundError: msg="The file"+path+"does not exist." print(msg) else: words=re.findall(r'[\w^-]+',content) jisuanTotal(words)
結果展示:
3、功能三:支援命令列輸入儲存有英文作品檔案的目錄名,批量統計。
程式碼實現:首先輸入儲存有英文作品的檔名,然後判斷是否為檔案,os.path.splitext(“檔案路徑”)用於分離檔名與副檔名;預設返回(fname,fextension)元組
def file_floder(path): dirs = os.listdir(path) for file in dirs: if os.path.isfile(file): print(os.path.splitext(file)[0]) with open(file, encoding='utf-8') as f: words = re.findall(r'[\w^-]+', f.read()) jisuanTotal(words) print('----')
執行結果:
4、功能四從控制檯讀入英文單篇作品,這不是為了打臉老五,而是為了向你女朋
友炫酷,表明你能提供更適合嵌入指令碼中的作品(或者如她所說,不過是更靈活
的介面)。如果讀不懂需求,請教師兄師姐,或者 bing: linux 重定向,儘管
這個功能在windows下也有,搜尋關鍵詞中加入linux有利於迅速找到。
由於不懂重定向,查閱資料後最終也沒能解決
5、psp
6、coding net地址:https://e.coding.net/weichenaa/word/wordfrequency.git
github地址:https://github.com/Wei-chenTF/word