翻譯|使用教程|編輯:王香|2019-01-25 10:00:16.000|閱讀 158 次
概述:Azure是微軟的云(免責聲明,我為微軟工作)。它是托管數據,創建機器人和自動執行任務,部署Python應用程序或利用AI的絕佳基礎架構。 在本演示中,在Azure上安裝了一個python 3.4環境和一些庫(Beautiful Soup和Flask)。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
起點是評估他們在執行各種任務時的“簡潔性”(而不是對任何語言的效率或效力做出任何判斷)。
這引出了我Rosettacode.org,這是任何編程愛好者的一個很棒的信息來源。它提供了各種任務(超過870個)和代碼片段來解決它們,在許多編程語言中(超過680個)。
探索Rosettacode.org并比較不同的編程語言。它可以幫助您找到解決您在“本土”語言中遇到的問題的新方法,并找出其他思維方式。為此,我寫了一個小應用程序來促進這個過程,我將在這里與大家分享。
該應用程序,非?;?。它只是將Rosettacode.org的代碼片段長度與不同的任務和語言進行比較,并使用我最喜歡的Javascript庫Highcharts在條形圖上顯示結果。
Azure是微軟的云(免責聲明,我為微軟工作)。它是托管數據,創建機器人和自動執行任務,部署Python應用程序或利用AI的絕佳基礎架構。
在本演示中,我在Azure上安裝了一個python 3.4環境和一些庫(Beautiful Soup和Flask)
第一步是刮掉RosettaCode以獲取所有可用任務。我寫了一個非常簡短的python腳本來進行抓取。我預先選擇了一部分編程語言(見languages_dict
下文),以避免刮掉太多不相關的數據。
# Selected language names and corresponding spelling/encoding as in HTML page languages_dict={"Java":"Java","JavaScript":"JavaScript","C":"C","C.2B.2B":"C++","C.23":"C#","COBOL":"Cobol","Haskell":"Haskell","Python":"Python","R":"R","Julia":"Julia","MATLAB_.2F_Octave":"Matlab","Pascal":"Pascal","Fortran":"Fortran","BASIC":"BASIC","Go":"Go","Ruby":"Ruby","SAS":"SAS","Stata":"Stata","Swift":"Swift","Processing":"Processing","UNIX_Shell":"UNIX Shell","VBA":"VBA","PowerShell":"PowerShell"} #store language names in an array language_name=[] #populate the array from the dict for item in languages_dict:language_name.append(languages_dict[item]) #get all tasks from Rosettacode.org url_task="//www.rosettacode.org/wiki/Category:Programming_Tasks" r = requests.get(url_task) soup = BeautifulSoup(r.text, 'html.parser') table=soup.find("div", {"class": "mw-category"}) #Create an empty dictionary to be filled with the tasks as they appear in the HTML source and name (as header) url_dict={} #get all links (a tags) tags=table('a') #iterate over tag list and fill the task/url dictionary for tag in tags: url_dict[tag.get('title',None)]=tag.get('href',None)[6:] #store task names in an array task_name=[] for item in url_dict: task_name.append(item) array_language=[] count=[] task="" #flask method to get the task the user wants to compare if request.method == "POST": # get url from task the user requested task = request.form['task'] url="//www.rosettacode.org/wiki/"+url_dict[task] r = requests.get(url) soup = BeautifulSoup(r.text, 'html.parser') dict_count={} for language in languages_dict: try: header=soup.find("span", {"id": language}) snippet=BeautifulSoup(header.find_next("pre").text, 'html.parser') dict_count[languages_dict[language]] = len(snippet.text) except: continue #sort dictionary for lang in sorted(dict_count, key=dict_count.get, reverse=True): array_language.append(lang) count.append(dict_count[lang])
當用戶選擇要比較的任務時,上面的腳本查找預先選擇的語言并評估相應片段的長度(在HTML中相應的標題之后的“pre”標簽內)。該腳本將結果存儲在一個數組中,該數組將通過Flask發送到前端到我的Highcharts。
以下是與“ For循環 ” 對應的片段的結果數組:
# array_language ['C', 'Swift', 'Pascal', 'Go', 'BASIC', 'Haskell', 'UNIX Shell', 'Matlab', 'Processing', 'PowerShell', 'Fortran', 'Cobol', 'VBA', 'C++', 'Java', 'Julia', 'Ruby', 'JavaScript', 'Python', 'R', 'SAS', 'Stata', 'C#'] #count (length of the snippets) [90, 73, 144, 167, 70, 115, 400, 103, 103, 116, 1211, 533, 193, 104, 119, 71, 55, 114, 92, 85, 293, 79, 261]
然后這些數組將提供條形圖。
Python和Flask使用和將數據數組發送到Highcharts 。在HTML頁面呈現期間,服務器注入使用python構造的數組。請注意,我使用該選項來避免任何編碼意外。{{ array_language | safe}}
{{count}}
| safe
Highcharts.chart('container', { chart: { type: 'bar' }, title: { text: 'Programming language comparison: {{task | safe}} ' }, subtitle: { text: 'Source: rosettacode.org' }, xAxis: { categories: {{ array_language | safe}}, title: { text: null } }, yAxis: { min: 0, title: { text: 'Task size (char)', align: 'high' }, labels: { overflow: 'justify' } }, tooltip: { valueSuffix: 'chars' }, plotOptions: { bar: { dataLabels: { enabled: true } } }, series: [{ name: '{{task}}', data: {{count}} }] });
我們現在有一種交互方式來比較執行許多任務所需的代碼長度!要添加到圖表中的一個很好的功能是將代碼段顯示為每種語言的工具提示。這將增強我們對Rosettacode的視覺探索。
購買Highcharts正版授權,請點擊“”喲!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn