鱼C论坛

 找回密码
 立即注册
查看: 1565|回复: 2

[已解决]求Python 统计代码量程序

[复制链接]
发表于 2021-5-29 08:46:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
有吧友有能用的Python 统计代码量的代码吗?
有的话希望发一下
谢谢
最佳答案
2021-5-29 09:53:01
  1. import easygui as g
  2. import os

  3. def show_result(star_dir):
  4.     lines = 0
  5.     total = 0
  6.     text = ''

  7.     for i in source_list:
  8.         lines = source_list[i]
  9.         total+=lines
  10.         text+='%s源文件%d个,源代码%d行\n'%(i,file_list[i],lines)
  11.     title = '统计结果'
  12.     msg = '您目前累计编写了%d行代码,完成进度:%.2f%%\n离10w行代码还差%d行'%(total,total/1000,100000-total)
  13.     g.textbox(msg,title,text)

  14. def calc_code(file_name):
  15.     lines = 0
  16.     with open(file_name) as f:
  17.         print('正在分析:%s'%file_name)
  18.         try:
  19.             for each_line in f:
  20.                 lines+=1
  21.         except UnicodeDecodeError:
  22.             pass
  23.         return lines


  24. def search_file(start_dir):
  25.     os.chdir(start_dir)

  26.     for each_file in os.listdir(os.curdir):
  27.         ext = os.path.splitext(each_file)[1]
  28.         if ext in target:
  29.             lines = calc_code(each_file)

  30.             try:
  31.                 file_list[ext]+=1
  32.             except KeyError:
  33.                 file_list[ext]=1

  34.             try:
  35.                 source_list[ext]+=lines
  36.             except KeyError:
  37.                 source_list[ext]=lines

  38.         if os.path.isdir(each_file):
  39.             search_file(each_file)
  40.             os.chdir(os.pardir)
  41.             #递归调用

  42. target = ['.py']
  43. file_list = {}
  44. source_list = {}

  45. g.msgbox('请打开代码所在文件夹','统计代码量')
  46. path = g.diropenbox('请打开代码库:')

  47. search_file(path)
  48. show_result(path)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-29 09:53:01 | 显示全部楼层    本楼为最佳答案   
  1. import easygui as g
  2. import os

  3. def show_result(star_dir):
  4.     lines = 0
  5.     total = 0
  6.     text = ''

  7.     for i in source_list:
  8.         lines = source_list[i]
  9.         total+=lines
  10.         text+='%s源文件%d个,源代码%d行\n'%(i,file_list[i],lines)
  11.     title = '统计结果'
  12.     msg = '您目前累计编写了%d行代码,完成进度:%.2f%%\n离10w行代码还差%d行'%(total,total/1000,100000-total)
  13.     g.textbox(msg,title,text)

  14. def calc_code(file_name):
  15.     lines = 0
  16.     with open(file_name) as f:
  17.         print('正在分析:%s'%file_name)
  18.         try:
  19.             for each_line in f:
  20.                 lines+=1
  21.         except UnicodeDecodeError:
  22.             pass
  23.         return lines


  24. def search_file(start_dir):
  25.     os.chdir(start_dir)

  26.     for each_file in os.listdir(os.curdir):
  27.         ext = os.path.splitext(each_file)[1]
  28.         if ext in target:
  29.             lines = calc_code(each_file)

  30.             try:
  31.                 file_list[ext]+=1
  32.             except KeyError:
  33.                 file_list[ext]=1

  34.             try:
  35.                 source_list[ext]+=lines
  36.             except KeyError:
  37.                 source_list[ext]=lines

  38.         if os.path.isdir(each_file):
  39.             search_file(each_file)
  40.             os.chdir(os.pardir)
  41.             #递归调用

  42. target = ['.py']
  43. file_list = {}
  44. source_list = {}

  45. g.msgbox('请打开代码所在文件夹','统计代码量')
  46. path = g.diropenbox('请打开代码库:')

  47. search_file(path)
  48. show_result(path)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-29 10:48:03 | 显示全部楼层

这个无论是否是自己编写的都统计了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 09:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表