1. 程式人生 > 實用技巧 >人人都喜歡用的十大python標準庫

人人都喜歡用的十大python標準庫


Python有以下三個特點:

· 易用性和靈活性

· 全行業高接受度:Python無疑是業界最流行的資料科學語言

· 用於資料科學的Python庫的數量優勢
做為當今人工智慧和機器學習領域最流行的程式語言之一。Python 以其有用的庫和包而著稱,即使沒有軟體工程背景的人也能程式設計。 本文基於 GitHub 倉庫的示例資料集,探索流行的 Python 標準庫。

Python 擁有一組與 Python 語言一起分發的標準庫,如 DateTime、math 或 Random。本文的目標是在 GitHub 的 Python 倉庫中找到 10 個最有用的標準庫。為實現我們的目標,我們研究了 GitHub 中不同的 Python 倉庫,並收集了它們的舊庫來回答這個問題。

為了開始我們的研究,首先,我們收集了 GitHub 中 5 個著名的 Python 資源庫在過去一年的提交情況。然後,我們對這些倉庫中的 Python 原始檔進行解析,並收集其提交中使用的庫。最後,我們將這些 GitHub 倉庫提交中使用的 10 個最流行的 Python 標準庫進行視覺化。如果有想跟我們一起交流學習的可以來扣扣裙(609616821)互相交流哦

用於資料收集的Python庫

你是否曾遇到過這樣的情況:缺少解決問題的資料?這是資料科學中一個永恆的問題。這也是為什麼學習提取和收集資料對資料科學家來說是一項非常重要的技能。資料提取和收集開闢了前所未有的道路。

有不同的方法可以訪問 GitHub 倉庫中的資料,例如 GitHub torrent、Git API 呼叫或 Google big query。但是,在本文中,我們想要嘗試一個新的非常有用的 Python 包,名為 Pydriller,它可以收集我們需要的資料。Pydriller 速度快,使用方便。我是在攻讀博士學位的時候熟悉了這個有趣的包。你可以在 這裡 檢視 Pydriller 的文件。要從 Pydriller 開始,首先,我們安裝包:

pip install pydriller

  每次提交時,GitHub 中的一個或多個原始檔都可以被修改。在 GitHub 這樣的版本控制系統中,每一次提交都有一個檔案,名為“diff”。它通過提交特定的提交來儲存在原始檔中應用的更改。在 GitHub 倉庫的提交中查詢庫的方法之一是在“diff”檔案中搜索正則表示式。

但在本文中,我們想嘗試一些不同的方法。我們比較兩個不同版本的原始檔“提交之前”和“提交之後”應用提交,然後收集這兩個檔案在庫名中的差異。

通過這種方法,我們就可以發現庫在不同提交中的使用頻率。好訊息是,Pydriller 允許我們在應用提交之前和應用提交之後訪問原始檔的版本。下面是收集資料所需的程式碼:

#import libraries
import pydriller as pyd
from datetime import datetime
#period to collect data
dt1 = datetime(2019, 11, 1)
dt2 = datetime(2020, 11, 1)
#path of 5 Python repositories
path = ["https://github.com/django/django.git","https://github.com/pandas-dev/pandas.git",
        "https://github.com/numpy/numpy","https://github.com/home-assistant/home-assistant.git",
        "https://github.com/donnemartin/system-design-primer.git"]
#collecting a version of a source file before and after applying a commit
tf_source = pd.DataFrame(columns=['commit_ID', 'before_Commit', 'after_Commit'])
for commit in pyd.RepositoryMining(path_to_repo=path, since=dt1, to=dt2).traverse_commits():
    for modified_file in commit.modifications:
        if modified_file.filename.endswith(".py"):
            tf_source = tf_source.append({'commit_ID': commit.hash,'before_Commit': modified_file.source_code_before,
                                          'after_Commit': modified_file.source_code}, ignore_index=True)

  用 Pydriller 在 GitHub 中收集 5 個著名的 Python 庫。

我們在 GitHub 上收集了 5 個大的 Python 專案在去年的提交情況,其中有 Django、Pandas、NumPy、Homeassistant、system-design-primer。“RepositoryMining”是 Pydriller 的主要 API 呼叫之一。

我們可以通過 RepositoryMining 中的兩個引數來定義一個時間段來收集不同倉庫中的提交:since和to。另外,我們考慮所有名稱以“.py”結尾的原始檔的提交,因為這些資源庫中也有其他程式語言的原始檔,但我們關注的是 Python 庫。

我們收集了三個特徵:commit.hash、source_code_before、source_code。Pydriller 中的commit.hash返回提交的id,source_code_before是應用提交前的原始檔版本,source_code則顯示提交後的原始檔內容。下面是我們收集的資料頭:
tf_source.head()
到目前為止,我們已經收集了開始旅程所需的資料。在下一節中,我們將學習如何探索這些原始檔中的庫。

如何解析 Python 原始碼

提取原始碼中資訊的方法之一是將它們轉換成抽象語法樹(Abstract Syntax Tree,AST)。然後,我們就可以遍歷這棵樹,並收集目標節點。

但最重要的一點是,我們只想收集 Python 標準庫,而不是所有在倉庫中使用的包,比如本地定義的庫,它們只有在倉庫中才有意義。Python 標準庫是和 Python 語言一起釋出的。

因此,為了將標準包和其他包分開,我們需要拉取 Python 中所有有效的標準庫。然後,我們可以寫一個函式來收集原始碼中的庫名。

我們可以將本節分為兩步:收集 Python 中所有可用的標準庫的列表、構建基於抽象語法書的函式來收集庫名。

1. 收集 Python 中所有可用的標準庫的列表

在 Python 的網站 上,有一張 Python 中所有標準庫的列表,並附有簡短說明。這個頁面將所有的 Python 標準庫按字母名稱排序,幫助我們對 Python 中所有的標準庫進行拉取。我把所有 Python 標準庫的列表放在 這裡,以 .csv 的格式。

2. 構建基於抽象語法書的函式來收集庫名

既然我們有了所有標準 Python 庫的列表,我們就需要從 Python GitHub 倉庫中收集我們示例資料集中的庫名稱。正如我們提到的,其中一種方法是遍歷抽象語法樹。

在本文中,我們的目標節點是import和importfrom。我們希望有一個函式遍歷解析樹,找到目標節點,並返回庫的名稱。下面是這樣做的類。

#import libraries
import ast
import tokenize
#A class to walk trough AST and collect libraries name
class FuncParser(ast.NodeVisitor):
def visit_Import(self, node):
tempImpo = node.names
if(tempImpo != None):
listImpo = tempImpo[0]
Impo = listImpo.name
if (Impo in api_name):
file_contents.append(Impo)
ast.NodeVisitor.generic_visit(self, node)
else:
ast.NodeVisitor.generic_visit(self, node)
def visit_ImportFrom(self, node):
module=node.module
if(module in api_name):
file_contents.append(module)
else:
ast.NodeVisitor.generic_visit(self, node)
def generic_visit(self, node):
ast.NodeVisitor.generic_visit(self, node)

  在 Python 程式碼中收集庫名的類。

為了更好地理解這個類的工作原理,下面是一段簡單的程式碼。這段示例程式碼只有兩行,分別匯入了兩個不同的庫,其中一個是 python 標準庫:tokenize,另一個是本地庫:assistant。

import tokenize as tz
import assistant as ass

  下面是這個示例程式碼的解析樹的轉儲。可以發現,我們需要收集的是作為alias類中的name引數。

此外,我們還需要檢查庫的名稱是否在我們從 Python 原始網站收集的所有標準庫的列表中。我們將 .csv 檔案儲存在名為api_name的列表中。

如果我們在這個示例程式碼上應用這個類FuncParser,它將只返回tokenize,因為另一個庫assistant在 Python 標準庫列表中不可用。

Module(body=[Import(names=[alias(name='tokenize', asname='tz')]), Import(names=[alias(name='assistant', asname='ass')])])

  

Python 倉庫中基於 GitHub 提交的 10 個最流行標準庫是什麼

到目前為止,我們收集了 GitHub 中 5 個著名的 Python 倉庫的示例資料集,並構建了一個類來收集 Python 程式碼中的庫名。

現在,我們需要將這個函式應用到 GitHub 的示例資料中,並找出這些倉庫的提交中使用的前 10 個庫。正如我們前面所討論的,我們將提交提交之前的原始檔的抽象語法樹和提交提交之後的同一原始檔的抽象語法樹進行比較,然後我們收集不同的庫節點。

然後我們收集不同的庫節點。首先,我會給大家展示一個步驟性的示例,告訴大家如何比較這兩個抽象語法樹,最後,我把所有的程式碼放在一起,以迴圈遍歷整個資料集,並計算每個庫的出現次數。

1. 提交提交之前收集庫名列表

我們將示例資料集儲存在tf_source中,我選擇這個資料集的第一行來解釋整個過程。tf_source’Commit_before’ 返回示例資料集中第一次提交前的程式碼內容。

然後,我們應用FuncParser()來收集這個原始檔中的所有庫名,並在file_contents列表中返回結果。我們建立一個名為tokens_before的資料框架,並存儲這個列表。

text_before=str(tf_source[‘Commit_before’](0))

bf_obj = FuncParser()
bf_tree = ast.parse(text_before)
file_contents = []
bf_obj.visit(bf_tree)
dtobj_before = pd.DataFrame(file_contents, columns=[‘token’])
tokens_before =pd.DataFrame(dtobj_before[‘token’].value_counts())

  2. 提交提交之後收集庫名列表

我們重複與上面的步驟相同的過程,但這次是在提交提交之後對原始檔的內容進行的,tf_source‘Commit_after’。

另外,我們將結果儲存在一個名為tokens_after的資料幀中。

text_after=str(tf_source[‘Commit_after’](0))

 aft_obj = FuncParser()
aft_tree = ast.parse(text_after)
file_contents = []
aft_obj.visit(aft_tree)
dtobj_after = pd.DataFrame(file_contents, columns=[‘token’])
tokens_after =pd.DataFrame(dtobj_after[‘token’].value_counts())

  3. 找出這兩個列表之間的差異

在這一步中,我們從tokens_after中減去tokens_before以計算它們的差異。

diff = tokens_after.subtract(tokens_before)
diff_token = diff[(diff.select_dtypes(include=[‘number’]) != 0).any(1)]
diff_token=diff_token.fillna(0)
diff_token= diff_token.abs()
diff_token = diff_token.reset_index()

  4. 計算庫的數量

最後,我們統計每個庫在diff_token資料幀中出現的次數。為此,我們建立一個名為py_lib的字典,並統計庫的出現次數。

py_lib={}
j=0
for j in range(0,len(diff_token)):
word = diff_token['index'](j).lower()
if word in py_lib:
py_lib[word]+=diff_token['token'](j)
else:
py_lib[word]=1
j+=

  為了將上述步驟應用於我們在前面收集的整個示例資料中,我在步驟的開頭添加了一個迴圈。下面是程式碼:

i=0
error=0
py_lib={}
for row in tf_source.iterrows():

#parsing the source file before applying commit i
if tf_source['Commit_before'](i) is not None:
try:
text_before=str(tf_source['Commit_before'](i))


bf_obj = FuncParser()
bf_tree = ast.parse(text_before)
file_contents = []
bf_obj.visit(bf_tree)
dtobj_before = pd.DataFrame(file_contents, columns=['token'])
tokens_before =pd.DataFrame(dtobj_before['token'].value_counts())

except:
error +=1
else:
file_contents = []
dtobj_before = pd.DataFrame(file_contents, columns=['token'])
tokens_before =pd.DataFrame(dtobj_before['token'].value_counts())

#parsing the source file after applying commit i
if tf_source['Commit_after'](i) is not None:
try:
text_after=str(tf_source['Commit_after'](i))


aft_obj = FuncParser()
aft_tree = ast.parse(text_after)
file_contents = []
aft_obj.visit(aft_tree)
dtobj_after = pd.DataFrame(file_contents, columns=['token'])
tokens_after =pd.DataFrame(dtobj_after['token'].value_counts())

except:
error +=1
else:
file_contents = []
dtobj_after = pd.DataFrame(file_contents, columns=['token'])
tokens_after =pd.DataFrame(dtobj_after['token'].value_counts())


#calculating the differences between two list tokens_before and tokens_after
diff = tokens_after.subtract(tokens_before)
diff_token = diff[(diff.select_dtypes(include=['number']) != 0).any(1)]
diff_token=diff_token.fillna(0)
diff_token= diff_token.abs()
diff_token = diff_token.reset_index()

# counting the numer of each libraries which are added or removed by commit i
j=0
for j in range(0,len(diff_token)):
word = diff_token['index'](j).lower()
if word in py_lib:
py_lib[word]+=diff_token['token'](j)
else:
py_lib[word]=1
j+=1

i+=1

  在整個示例資料集中收集庫。

現在我們收集了 GitHub 中所有 Python 倉庫的庫及其提交頻率,我們想在py_lib字典中找到前 10 個庫。我們可以用下面的程式碼將前 10 個庫的值收集到一個字典中。

我們可以看到,從示例資料集來看,warnings、sys或datetime等庫都在 Python 標準庫的前 10 名列表中。

from operator import itemgetter
d=sorted(py_lib.items(), key=itemgetter(1),reverse=True)[:10]
[('warnings', 96.0),
('sys', 73.0),
('datetime', 28.0),
('test', 27.0),
('os', 22.0),
('collections', 18.0),
('io', 16.0),
('gc', 10.0),
('functools', 9.0),
('threading', 7.0)]

  基於 GitHub 示例資料集的 Python 十大標準庫。


另外,我們還可以繪製 Python 庫的詞雲圖及其頻率。

import matplotlib.pyplot as plt
from wordcloud import WordCloud
wordcloud = WordCloud(background_color='black',max_font_size = 50)
wordcloud.generate_from_frequencies(frequencies=py_lib)
plt.figure(figsize=(8,6))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()

  基於 GitHub 示例資料集的流行 Python 庫的詞雲圖。

總 結

、在本文中,我們嘗試基於一個示例資料集收集 10 個最受歡迎的 Python 庫。這個資料集包含了 GitHub 中 5 個著名的 Python 倉庫最近一年的提交情況。

我使用 Pydriller 來收集 GitHub 的資料。我們對提交之前和提交之後的原始檔抽象語法樹進行了比較,並收集了這些提交中使用的庫列表。最後,我在詞雲圖中繪製了最流行的 Python 庫。最後歡迎各路大佬萌新一起來Q裙交流探討++609616831


Python有以下三個特點:

· 易用性和靈活性

· 全行業高接受度:Python無疑是業界最流行的資料科學語言

· 用於資料科學的Python庫的數量優勢
做為當今人工智慧和機器學習領域最流行的程式語言之一。Python 以其有用的庫和包而著稱,即使沒有軟體工程背景的人也能程式設計。 本文基於 GitHub 倉庫的示例資料集,探索流行的 Python 標準庫。

Python 擁有一組與 Python 語言一起分發的標準庫,如 DateTime、math 或 Random。本文的目標是在 GitHub 的 Python 倉庫中找到 10 個最有用的標準庫。為實現我們的目標,我們研究了 GitHub 中不同的 Python 倉庫,並收集了它們的舊庫來回答這個問題。

為了開始我們的研究,首先,我們收集了 GitHub 中 5 個著名的 Python 資源庫在過去一年的提交情況。然後,我們對這些倉庫中的 Python 原始檔進行解析,並收集其提交中使用的庫。最後,我們將這些 GitHub 倉庫提交中使用的 10 個最流行的 Python 標準庫進行視覺化。如果有想跟我們一起交流學習的可以來扣扣裙(609616821)互相交流哦

用於資料收集的Python庫

你是否曾遇到過這樣的情況:缺少解決問題的資料?這是資料科學中一個永恆的問題。這也是為什麼學習提取和收集資料對資料科學家來說是一項非常重要的技能。資料提取和收集開闢了前所未有的道路。

有不同的方法可以訪問 GitHub 倉庫中的資料,例如 GitHub torrent、Git API 呼叫或 Google big query。但是,在本文中,我們想要嘗試一個新的非常有用的 Python 包,名為 Pydriller,它可以收集我們需要的資料。Pydriller 速度快,使用方便。我是在攻讀博士學位的時候熟悉了這個有趣的包。你可以在 這裡 檢視 Pydriller 的文件。要從 Pydriller 開始,首先,我們安裝包:

pip install pydriller
  • 1

每次提交時,GitHub 中的一個或多個原始檔都可以被修改。在 GitHub 這樣的版本控制系統中,每一次提交都有一個檔案,名為“diff”。它通過提交特定的提交來儲存在原始檔中應用的更改。在 GitHub 倉庫的提交中查詢庫的方法之一是在“diff”檔案中搜索正則表示式。

但在本文中,我們想嘗試一些不同的方法。我們比較兩個不同版本的原始檔“提交之前”和“提交之後”應用提交,然後收集這兩個檔案在庫名中的差異。

通過這種方法,我們就可以發現庫在不同提交中的使用頻率。好訊息是,Pydriller 允許我們在應用提交之前和應用提交之後訪問原始檔的版本。下面是收集資料所需的程式碼:

#import libraries
import pydriller as pyd
from datetime import datetime
#period to collect data
dt1 = datetime(2019, 11, 1)
dt2 = datetime(2020, 11, 1)
#path of 5 Python repositories
path = ["https://github.com/django/django.git","https://github.com/pandas-dev/pandas.git",
        "https://github.com/numpy/numpy","https://github.com/home-assistant/home-assistant.git",
        "https://github.com/donnemartin/system-design-primer.git"]
#collecting a version of a source file before and after applying a commit
tf_source = pd.DataFrame(columns=['commit_ID', 'before_Commit', 'after_Commit'])
for commit in pyd.RepositoryMining(path_to_repo=path, since=dt1, to=dt2).traverse_commits():
    for modified_file in commit.modifications:
        if modified_file.filename.endswith(".py"):
            tf_source = tf_source.append({'commit_ID': commit.hash,'before_Commit': modified_file.source_code_before,
                                          'after_Commit': modified_file.source_code}, ignore_index=True)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

用 Pydriller 在 GitHub 中收集 5 個著名的 Python 庫。

我們在 GitHub 上收集了 5 個大的 Python 專案在去年的提交情況,其中有 Django、Pandas、NumPy、Homeassistant、system-design-primer。“RepositoryMining”是 Pydriller 的主要 API 呼叫之一。

我們可以通過 RepositoryMining 中的兩個引數來定義一個時間段來收集不同倉庫中的提交:since和to。另外,我們考慮所有名稱以“.py”結尾的原始檔的提交,因為這些資源庫中也有其他程式語言的原始檔,但我們關注的是 Python 庫。

我們收集了三個特徵:commit.hash、source_code_before、source_code。Pydriller 中的commit.hash返回提交的id,source_code_before是應用提交前的原始檔版本,source_code則顯示提交後的原始檔內容。下面是我們收集的資料頭:
tf_source.head()
到目前為止,我們已經收集了開始旅程所需的資料。在下一節中,我們將學習如何探索這些原始檔中的庫。

如何解析 Python 原始碼

提取原始碼中資訊的方法之一是將它們轉換成抽象語法樹(Abstract Syntax Tree,AST)。然後,我們就可以遍歷這棵樹,並收集目標節點。

但最重要的一點是,我們只想收集 Python 標準庫,而不是所有在倉庫中使用的包,比如本地定義的庫,它們只有在倉庫中才有意義。Python 標準庫是和 Python 語言一起釋出的。

因此,為了將標準包和其他包分開,我們需要拉取 Python 中所有有效的標準庫。然後,我們可以寫一個函式來收集原始碼中的庫名。

我們可以將本節分為兩步:收集 Python 中所有可用的標準庫的列表、構建基於抽象語法書的函式來收集庫名。

1. 收集 Python 中所有可用的標準庫的列表

在 Python 的網站 上,有一張 Python 中所有標準庫的列表,並附有簡短說明。這個頁面將所有的 Python 標準庫按字母名稱排序,幫助我們對 Python 中所有的標準庫進行拉取。我把所有 Python 標準庫的列表放在 這裡,以 .csv 的格式。

2. 構建基於抽象語法書的函式來收集庫名

既然我們有了所有標準 Python 庫的列表,我們就需要從 Python GitHub 倉庫中收集我們示例資料集中的庫名稱。正如我們提到的,其中一種方法是遍歷抽象語法樹。

在本文中,我們的目標節點是import和importfrom。我們希望有一個函式遍歷解析樹,找到目標節點,並返回庫的名稱。下面是這樣做的類。

#import libraries
import ast
import tokenize
#A class to walk trough AST and collect libraries name
class FuncParser(ast.NodeVisitor):
def visit_Import(self, node):
tempImpo = node.names
if(tempImpo != None):
listImpo = tempImpo[0]
Impo = listImpo.name
if (Impo in api_name):
file_contents.append(Impo)
ast.NodeVisitor.generic_visit(self, node)
else:
ast.NodeVisitor.generic_visit(self, node)
def visit_ImportFrom(self, node):
module=node.module
if(module in api_name):
file_contents.append(module)
else:
ast.NodeVisitor.generic_visit(self, node)
def generic_visit(self, node):
ast.NodeVisitor.generic_visit(self, node)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在 Python 程式碼中收集庫名的類。

為了更好地理解這個類的工作原理,下面是一段簡單的程式碼。這段示例程式碼只有兩行,分別匯入了兩個不同的庫,其中一個是 python 標準庫:tokenize,另一個是本地庫:assistant。

import tokenize as tz
import assistant as ass
  • 1
  • 2

下面是這個示例程式碼的解析樹的轉儲。可以發現,我們需要收集的是作為alias類中的name引數。

此外,我們還需要檢查庫的名稱是否在我們從 Python 原始網站收集的所有標準庫的列表中。我們將 .csv 檔案儲存在名為api_name的列表中。

如果我們在這個示例程式碼上應用這個類FuncParser,它將只返回tokenize,因為另一個庫assistant在 Python 標準庫列表中不可用。

Module(body=[Import(names=[alias(name='tokenize', asname='tz')]), Import(names=[alias(name='assistant', asname='ass')])])
  • 1

Python 倉庫中基於 GitHub 提交的 10 個最流行標準庫是什麼

到目前為止,我們收集了 GitHub 中 5 個著名的 Python 倉庫的示例資料集,並構建了一個類來收集 Python 程式碼中的庫名。

現在,我們需要將這個函式應用到 GitHub 的示例資料中,並找出這些倉庫的提交中使用的前 10 個庫。正如我們前面所討論的,我們將提交提交之前的原始檔的抽象語法樹和提交提交之後的同一原始檔的抽象語法樹進行比較,然後我們收集不同的庫節點。

然後我們收集不同的庫節點。首先,我會給大家展示一個步驟性的示例,告訴大家如何比較這兩個抽象語法樹,最後,我把所有的程式碼放在一起,以迴圈遍歷整個資料集,並計算每個庫的出現次數。

1. 提交提交之前收集庫名列表

我們將示例資料集儲存在tf_source中,我選擇這個資料集的第一行來解釋整個過程。tf_source’Commit_before’ 返回示例資料集中第一次提交前的程式碼內容。

然後,我們應用FuncParser()來收集這個原始檔中的所有庫名,並在file_contents列表中返回結果。我們建立一個名為tokens_before的資料框架,並存儲這個列表。

text_before=str(tf_source[‘Commit_before’](0))

bf_obj = FuncParser()
bf_tree = ast.parse(text_before)
file_contents = []
bf_obj.visit(bf_tree)
dtobj_before = pd.DataFrame(file_contents, columns=[‘token’])
tokens_before =pd.DataFrame(dtobj_before[‘token’].value_counts())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2. 提交提交之後收集庫名列表
我們重複與上面的步驟相同的過程,但這次是在提交提交之後對原始檔的內容進行的,tf_source‘Commit_after’。

另外,我們將結果儲存在一個名為tokens_after的資料幀中。

text_after=str(tf_source[‘Commit_after’](0))

 aft_obj = FuncParser()
aft_tree = ast.parse(text_after)
file_contents = []
aft_obj.visit(aft_tree)
dtobj_after = pd.DataFrame(file_contents, columns=[‘token’])
tokens_after =pd.DataFrame(dtobj_after[‘token’].value_counts())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3. 找出這兩個列表之間的差異
在這一步中,我們從tokens_after中減去tokens_before以計算它們的差異。

diff = tokens_after.subtract(tokens_before)
diff_token = diff[(diff.select_dtypes(include=[‘number’]) != 0).any(1)]
diff_token=diff_token.fillna(0)
diff_token= diff_token.abs()
diff_token = diff_token.reset_index()
  • 1
  • 2
  • 3
  • 4
  • 5

4. 計算庫的數量

最後,我們統計每個庫在diff_token資料幀中出現的次數。為此,我們建立一個名為py_lib的字典,並統計庫的出現次數。

py_lib={}
j=0
for j in range(0,len(diff_token)):
word = diff_token['index'](j).lower()
if word in py_lib:
py_lib[word]+=diff_token['token'](j)
else:
py_lib[word]=1
j+=
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

為了將上述步驟應用於我們在前面收集的整個示例資料中,我在步驟的開頭添加了一個迴圈。下面是程式碼:

i=0
error=0
py_lib={}
for row in tf_source.iterrows():

#parsing the source file before applying commit i
if tf_source['Commit_before'](i) is not None:
try:
text_before=str(tf_source['Commit_before'](i))


bf_obj = FuncParser()
bf_tree = ast.parse(text_before)
file_contents = []
bf_obj.visit(bf_tree)
dtobj_before = pd.DataFrame(file_contents, columns=['token'])
tokens_before =pd.DataFrame(dtobj_before['token'].value_counts())

except:
error +=1
else:
file_contents = []
dtobj_before = pd.DataFrame(file_contents, columns=['token'])
tokens_before =pd.DataFrame(dtobj_before['token'].value_counts())

#parsing the source file after applying commit i
if tf_source['Commit_after'](i) is not None:
try:
text_after=str(tf_source['Commit_after'](i))


aft_obj = FuncParser()
aft_tree = ast.parse(text_after)
file_contents = []
aft_obj.visit(aft_tree)
dtobj_after = pd.DataFrame(file_contents, columns=['token'])
tokens_after =pd.DataFrame(dtobj_after['token'].value_counts())

except:
error +=1
else:
file_contents = []
dtobj_after = pd.DataFrame(file_contents, columns=['token'])
tokens_after =pd.DataFrame(dtobj_after['token'].value_counts())


#calculating the differences between two list tokens_before and tokens_after
diff = tokens_after.subtract(tokens_before)
diff_token = diff[(diff.select_dtypes(include=['number']) != 0).any(1)]
diff_token=diff_token.fillna(0)
diff_token= diff_token.abs()
diff_token = diff_token.reset_index()

# counting the numer of each libraries which are added or removed by commit i
j=0
for j in range(0,len(diff_token)):
word = diff_token['index'](j).lower()
if word in py_lib:
py_lib[word]+=diff_token['token'](j)
else:
py_lib[word]=1
j+=1

i+=1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

在整個示例資料集中收集庫。

現在我們收集了 GitHub 中所有 Python 倉庫的庫及其提交頻率,我們想在py_lib字典中找到前 10 個庫。我們可以用下面的程式碼將前 10 個庫的值收集到一個字典中。

我們可以看到,從示例資料集來看,warnings、sys或datetime等庫都在 Python 標準庫的前 10 名列表中。

from operator import itemgetter
d=sorted(py_lib.items(), key=itemgetter(1),reverse=True)[:10]
[('warnings', 96.0),
('sys', 73.0),
('datetime', 28.0),
('test', 27.0),
('os', 22.0),
('collections', 18.0),
('io', 16.0),
('gc', 10.0),
('functools', 9.0),
('threading', 7.0)]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

基於 GitHub 示例資料集的 Python 十大標準庫。
另外,我們還可以繪製 Python 庫的詞雲圖及其頻率。

import matplotlib.pyplot as plt
from wordcloud import WordCloud
wordcloud = WordCloud(background_color='black',max_font_size = 50)
wordcloud.generate_from_frequencies(frequencies=py_lib)
plt.figure(figsize=(8,6))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

基於 GitHub 示例資料集的流行 Python 庫的詞雲圖。

總 結

、在本文中,我們嘗試基於一個示例資料集收集 10 個最受歡迎的 Python 庫。這個資料集包含了 GitHub 中 5 個著名的 Python 倉庫最近一年的提交情況。

我使用 Pydriller 來收集 GitHub 的資料。我們對提交之前和提交之後的原始檔抽象語法樹進行了比較,並收集了這些提交中使用的庫列表。最後,我在詞雲圖中繪製了最流行的 Python 庫。最後歡迎各路大佬萌新一起來Q裙交流探討++609616831