You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
K_class/code/经过Code Quality Analysis工具的分...

11 KiB

经过Code Quality Analysis工具的分析并消除所有的警告

经过查询资料选择了pylint进行分析

Pylint

Pylint是一个非常流行的Python代码静态分析工具可以帮助你检测代码中的错误、风格问题和复杂度问题。它会为你的代码打分并列出所有警告和错误。你可以逐一修复这些问题以提高代码质量。

以下是一些改进的记录:

(.venv) PS E:\Crawler> pylint a_wordcloud.py ************* Module a_wordcloud a_wordcloud.py:38:0: C0304: Final newline missing (missing-final-newline) a_wordcloud.py:1:0: C0114: Missing module docstring (missing-module-docstring) a_wordcloud.py:8:0: C0116: Missing function or method docstring (missing-function-docstring) a_wordcloud.py:9:11: C0209: Formatting a regular string which could be an f-string (consider-using-f-string) a_wordcloud.py:8:20: W0613: Unused argument 'word' (unused-argument) a_wordcloud.py:8:26: W0613: Unused argument 'font_size' (unused-argument) a_wordcloud.py:8:37: W0613: Unused argument 'position' (unused-argument) a_wordcloud.py:8:47: W0613: Unused argument 'orientation' (unused-argument) a_wordcloud.py:8:60: W0613: Unused argument 'random_state' (unused-argument) a_wordcloud.py:8:0: W0613: Unused argument 'kwargs' (unused-argument) a_wordcloud.py:18:0: C0116: Missing function or method docstring (missing-function-docstring) a_wordcloud.py:18:25: W0621: Redefining name 'dm' from outer scope (line 13) (redefined-outer-name)


Your code has been rated at 2.94/10

(.venv) PS E:\Crawler> pylint bvid.py ************* Module bvid bvid.py:8:0: C0301: Line too long (1272/100) (line-too-long) bvid.py:18:0: C0301: Line too long (145/100) (line-too-long) bvid.py:27:0: C0301: Line too long (335/100) (line-too-long) bvid.py:65:0: C0304: Final newline missing (missing-final-newline) bvid.py:1:0: C0114: Missing module docstring (missing-module-docstring) bvid.py:22:0: C0103: Constant name "cid_num" doesn't conform to UPPER_CASE naming style (invalid-name) bvid.py:25:0: C0116: Missing function or method docstring (missing-function-docstring) bvid.py:25:0: C0103: Function name "Get_Source" doesn't conform to snake_case naming style (invalid-name) bvid.py:25:15: W0621: Redefining name 'page' from outer scope (line 53) (redefined-outer-name) bvid.py:31:4: W0621: Redefining name 'source' from outer scope (line 55) (redefined-outer-name) bvid.py:29:15: W3101: Missing timeout argument for method 'requests.get' can cause your program to hang indefinitely (missing-timeout) bvid.py:36:0: C0116: Missing function or method docstring (missing-function-docstring) bvid.py:36:0: C0103: Function name "Get_Bv" doesn't conform to snake_case naming style (invalid-name) bvid.py:36:11: W0621: Redefining name 'source' from outer scope (line 55) (redefined-outer-name) bvid.py:37:4: W0621: Redefining name 'url_list' from outer scope (line 57) (redefined-outer-name) bvid.py:37:15: R1734: Consider using [] instead of list() (use-list-literal) bvid.py:45:0: C0116: Missing function or method docstring (missing-function-docstring) bvid.py:45:0: C0103: Function name "Save_Bv" doesn't conform to snake_case naming style (invalid-name) bvid.py:45:12: W0621: Redefining name 'url_list' from outer scope (line 57) (redefined-outer-name) bvid.py:52:4: C0103: Constant name "bv_count" doesn't conform to UPPER_CASE naming style (invalid-name)


Your code has been rated at 3.55/10

(.venv) PS E:\Crawler> pylint to_excel.py ************* Module to_excel to_excel.py:1:0: C0114: Missing module docstring (missing-module-docstring) to_excel.py:4:0: C0116: Missing function or method docstring (missing-function-docstring) to_excel.py:10:0: C0116: Missing function or method docstring (missing-function-docstring) to_excel.py:24:0: C0116: Missing function or method docstring (missing-function-docstring) to_excel.py:30:0: C0116: Missing function or method docstring (missing-function-docstring)


Your code has been rated at 8.21/10

中间修改了很多次最后勉强从8.21->9.23

(.venv) PS E:\Crawler> pylint E:\Crawler\code\a_wordcloud.py E:\Crawler\code\b_wordcloud.py E:\Crawler\code\bvid.py E:\Crawler\code\to_allexcel.py E:\Crawler\code\to_danmu.py E:\Crawler\code\to_excel.py ************* Module bvid code\bvid.py:27:15: W0621: Redefining name 'page_number' from outer scope (line 52) (redefined-outer-name) code\bvid.py:40:11: W0621: Redefining name 'source_code' from outer scope (line 53) (redefined-outer-name) code\bvid.py:44:12: W0621: Redefining name 'bv_list' from outer scope (line 54) (redefined-outer-name) code\bvid.py:51:4: C0103: Constant name "bv_count" doesn't conform to UPPER_CASE naming style (invalid-name) ************* Module to_danmu code\to_danmu.py:33:19: W0621: Redefining name 'bv_numbers' from outer scope (line 58) (redefined-outer-name)
code\to_danmu.py:35:4: W0621: Redefining name 'video_cids' from outer scope (line 59) (redefined-outer-name)
code\to_danmu.py:43:22: W0621: Redefining name 'output_file' from outer scope (line 56) (redefined-outer-name) code\to_danmu.py:55:4: C0103: Constant name "bv_file_path" doesn't conform to UPPER_CASE naming style (invalid-name) code\to_danmu.py:56:4: C0103: Constant name "output_file" doesn't conform to UPPER_CASE naming style (invalid-name) ************* Module to_excel code\to_excel.py:1:0: R0801: Similar lines in 2 files ==bvid:[7:28] ==to_danmu:[8:29] HEADERS = { 'authority': 'api.bilibili.com', 'accept': 'application/json, text/plain, /', 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'cookie': '...', 'origin': 'https://www.bilibili.com', 'referer': 'https://space.bilibili.com/1760559884?spm_id_from=333.788.0.0', 'sec-ch-ua': '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', 'sec-fetch-dest': 'document', 'sec-fetch-mode': 'navigate', 'sec-fetch-site': 'same-origin', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0' }


Your code has been rated at 9.23/10

(.venv) PS E:\Crawler> pylint E:\Crawler\code\a_wordcloud.py E:\Crawler\code\bvid.py E:\Crawler\code\to_allexcel.py E:\Crawler\code\to_danmu.py E:\Crawler\code\to_excel.py ************* Module to_danmu code\to_danmu.py:38:4: C0103: Variable name "BV_FILE_PATH" doesn't conform to snake_case naming style (invalid-name) code\to_danmu.py:39:4: C0103: Variable name "DANMU_OUTPUT_FILE" doesn't conform to snake_case naming style (invalid-name)


Your code has been rated at 9.84/10 (previous run: 9.59/10, +0.24)

这个时候是最兴奋的,觉得胜利在望


Your code has been rated at 10.00/10 (previous run: 9.84/10, +0.16)

再改了两次后成功了!


以下是关于提升过程的总结

8.21->10

一、关键问题与解决方法

问题1命名风格不符合PEP 8规范

警告

code\to_danmu.py:38:4: C0103: Variable name "BV_FILE_PATH" doesn't conform to snake_case naming style (invalid-name)

解决方法 将常量命名风格改为符合PEP 8规范的小写加下划线的snake_case,如将BV_FILE_PATH改为bv_file_path

思考 PEP 8建议变量名使用snake_case风格,常量名使用UPPER_CASE风格。遵循这些命名规范能让代码更具可读性和可维护性,也能让团队成员更容易理解和接手代码。


问题2未使用的参数

警告

code\a_wordcloud.py:10:20: W0613: Unused argument 'word' (unused-argument)

解决方法 删除未使用的参数,或者将参数名改为_,以表明这个参数未被使用。

思考 保留未使用的参数可能会使代码显得冗余和混乱,清理这些无用的部分不仅能减少误解,还能提高代码的简洁性和可维护性。


问题3变量重定义

警告

code\bvid.py:9:15: W0621: Redefining name 'page_number' from outer scope (line 34) (redefined-outer-name)

解决方法 通过更改变量名称以避免在不同作用域中重定义变量,例如将page_number改为page_num

思考 在不同作用域中使用相同的变量名可能会导致代码逻辑混乱,特别是在复杂的函数或循环中。为每个作用域赋予独特的变量名,能避免潜在的逻辑错误和混淆。


问题4重复代码

警告

code\to_excel.py:1:0: R0801: Similar lines in 2 files

解决方法 将重复的代码片段提取到一个独立的函数或模块中然后在需要的地方调用它。例如将重复使用的HTTP请求头封装到一个独立的模块中并在所有相关文件中引用。

思考 重复代码不仅增加了维护的复杂性,还可能导致不同部分的代码逻辑不一致。通过提取公共部分,能够使代码更加模块化、清晰且易于维护。


问题5行长度超过限制

警告

code\a_wordcloud.py:26:0: C0301: Line too long (135/100) (line-too-long)

解决方法 将超长的代码行进行拆分或者使用字符串格式化如f-string来将行长度控制在100字符以内。

思考 控制代码行的长度有助于提高代码的可读性,特别是在显示器宽度有限或代码被打印出来时。保持代码简洁清晰,可以减少阅读和理解的时间。


问题6未使用的导入

警告

code\b_wordcloud.py:8:0: C0411: standard import "re" should be placed before third party imports "pandas", "numpy", "jieba.analyse" (wrong-import-order)

解决方法 按PEP 8的建议调整导入顺序将标准库的导入放在文件的顶部第三方库放在其后并删除未使用的导入。

思考 按规范组织导入可以提高代码的整洁性和可读性,还可以避免未使用导入对程序产生不必要的影响。保持代码的简洁和结构化有助于减少潜在的错误。

总结

  • 命名规范遵循PEP 8的命名规范能显著提高代码的可读性和一致性尤其在多人协作时尤为重要。
  • 参数与变量:清理未使用的参数和避免变量重定义,能防止不必要的混淆和错误。
  • 代码重复:通过提取公共部分来减少代码重复,使代码更加模块化和易于维护。
  • 行长度控制:控制行长度有助于提高代码的可读性,尤其是在显示器或打印输出有限的情况下。
  • 导入顺序:按规范组织导入,提高代码的整洁性,减少潜在的冲突和错误。