开源软件的一个最大好处是能够重复使用其他人的代码。 重复使用代码有助于节省时间、发现新功能和学习其他编程风格。 重复使用代码有两种主要方法:
- 将代码片段直接复制并粘贴到项目中。 如果你不熟悉编码,这是开始重复使用代码的最快方法。
- 将库导入项目。 虽然此方法需要花一些时间进行学习,但最终会更加轻松高效。 这也是软件开发的基础技能。
在本文中,我们将演示一个示例来了解这两者:重复使用可计算数字阶乘的 Python 代码。
在你的项目中使用其他人的代码片段
刚开始学习编码时,你可能会通过将其他人的代码片段复制并粘贴到项目中来进行重复使用。 这是一种节省时间的好方法,但在复制其他开发人员的代码前,应始终执行几个关键步骤。
1.查找和理解代码片段
首先,你需要查找并了解要重复使用的代码片段。 对于此示例,我们将选择 new2code/python-factorial
存储库。
首先,打开 factorial_finder.py
,这会使用循环实现计算器****:
# Initialize the factorial result to 1
factorial = 1
# Initialize the input number to 6
number = 6
# Loop from 1 to number (inclusive) and multiply factorial by each number
for i in range(1, number + 1):
factorial *= i
print(f"The factorial of {number} is {factorial}")
然后,在文件顶部的菜单栏中,单击“”以开始与 Copilot 的对话。
在聊天窗口中,询问 Copilot:
Explain this program.
Explain this program.
2.了解项目许可
在可以重复使用找到的代码之前,需要了解其许可。 许可证决定了在项目中使用代码的方式,包括是否能够复制、修改和分发该代码。
若要确定 new2code/python-factorial 的许可证,请找到存储库主页上的“About”部分。 在此,你将看到该存储库已根据 MIT 许可证获得许可。 若要阅读许可证,请单击 “MIT 许可证”****。
我们希望复制整个 factorial_finder.py
文件,因此 MIT 许可证指示我们应该在自己的项目中添加许可证的副本。 在 Python 文件的顶部,将许可证粘贴为注释。
Tip
可以使用选择许可证工具了解其他常见许可证所允许的内容。
3.使用和修改代码片段
现在,你已准备好将代码片段粘贴到项目中。 虽然有时可以直接按原样使用代码片段,但经常需要根据自己的具体用例对其进行修改****。 现在我们来练习一下吧!
假设我们想要快速计算 5、7、9 和 10 的阶乘。 我们可以将计算器移动到以参数形式获取数字的函数中,而不是针对每个数字复制粘贴整个程序****。
使用 Copilot Chat 来建议并解释实现。 将当前代码粘贴到聊天窗口中,后接以下提示:
Wrap the Python code above in a function.
Wrap the Python code above in a function.
Copilot 将生成如下所示的代码:
def calculate_factorial(number): # Initialize the factorial result to 1 factorial = 1 # Loop from 1 to number (inclusive) and multiply factorial by each number for i in range(1, number + 1): factorial *= i return factorial
def calculate_factorial(number):
# Initialize the factorial result to 1
factorial = 1
# Loop from 1 to number (inclusive) and multiply factorial by each number
for i in range(1, number + 1):
factorial *= i
return factorial
有了新函数,我们可以轻松地找到数字的阶乘,只需将以下代码添加到项目中,然后运行 Python 程序即可:
print(calculate_factorial(5)) print(calculate_factorial(7)) print(calculate_factorial(9)) print(calculate_factorial(10))
print(calculate_factorial(5))
print(calculate_factorial(7))
print(calculate_factorial(9))
print(calculate_factorial(10))
祝贺你! 你已成功找到、理解和修改了示例代码片段。
在项目中使用库中的代码
现在,我们来了解如何使用库,对开发人员而言,这是标准做法****。 库本质上是其他开发人员编写的用于执行特定任务的代码集合。 可以将库导入到项目中以使用预先编写的代码,从而节省时间和精力。
在本节中,我们将继续使用上一节的 Python 阶乘计算器示例。 下面是我们的当前代码,供你参考:
def calculate_factorial(number): # Initialize the factorial result to 1 factorial = 1 # Loop from 1 to number (inclusive) and multiply factorial by each number for i in range(1, number + 1): factorial *= i return factorial print(calculate_factorial(5)) print(calculate_factorial(7)) print(calculate_factorial(9)) print(calculate_factorial(10))
def calculate_factorial(number):
# Initialize the factorial result to 1
factorial = 1
# Loop from 1 to number (inclusive) and multiply factorial by each number
for i in range(1, number + 1):
factorial *= i
return factorial
print(calculate_factorial(5))
print(calculate_factorial(7))
print(calculate_factorial(9))
print(calculate_factorial(10))
1.查找库
在了解要添加到项目的功能后,即可使用相关代码搜索库。 Copilot Chat 是搜索库的一种简单方法,因为可以使用自然语言准确描述要查找的内容。
查找阶乘是一个相当常见的函数,而且很有可能有人已在现有库中添加了这个函数。 打开 Copilot Chat,然后询问:
Is there a Python library with a function for calculating a factorial?
Is there a Python library with a function for calculating a factorial?
Copilot 将告诉我们标准 Python 库的 math
模块中包含一个阶乘函数。
2.确定项目中安全性的优先级
向项目添加库或模块时,将创建所谓的依赖项****。 依赖项是预先编写的代码包,项目依赖其来正确运行。 如果编写或维护不当,依赖项可能会给工作引入安全漏洞。
值得庆幸的是,可以采取一些措施来为项目提供最佳保护。 现在我们来练习一下。
使用热门库
热门库很有可能是安全的,因为许多开发人员都在积极维护和使用它们。 判断热门程度的一个明显的标志是存储库拥有的星标****。 如果找不到依赖项的 GitHub 存储库,可以寻求 Copilot 的帮助。
打开 Copilot Chat,然后询问:
Find the GitHub repository containing the code for the math module in Python.
Find the GitHub repository containing the code for the math module in Python.
Copilot 将告诉你 math
模块已在拥有超过 64,000 颗星标的 python/cpython
中定义。
为项目启用 Dependabot alerts
启用后,在 Dependabot 检测到依赖项中存在安全问题时,会自动生成 Dependabot alerts,帮助你快速修复漏洞。 Dependabot 可在所有开源 GitHub 存储库上免费使用****。
现在,为存储库打开 Dependabot alerts。 单击项目的 GitHub 存储库的“安全性”选项卡****。 在 Dependabot alerts 旁,单击“启用 Dependabot alerts”。 可以从边栏的“Dependabot”选项卡访问 Dependabot alerts。
3.从库实现代码
现在,你已准备好将库导入项目中,然后在代码中使用其内容。 你可以阅读该库的文档,了解如何自行执行此操作,也可以要求 Copilot 为你建议和解释实现。
打开 Copilot Chat,然后询问:
How do I use the factorial function of the math module in my Python project?
How do I use the factorial function of the math module in my Python project?
然后,Copilot 将建议使用以下代码的版本:
import math # Calculate the factorial of a number number = 5 result = math.factorial(number) print(f"The factorial of {number} is {result}")
import math
# Calculate the factorial of a number
number = 5
result = math.factorial(number)
print(f"The factorial of {number} is {result}")
将项目中的现有代码替换为上述实现后,已成功在示例项目中重复使用库中的代码!