码迷,mamicode.com
首页 > 编程语言 > 详细

python学习笔记:easygui的简单示例

时间:2015-08-27 14:52:07      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

学习python的图形用户界面EasyGui,转一例实例,实现的功能是统计指定文件夹下的代码行数,测试下自己所写的代码离10万行还差多少距离

 

先贴代码,代码不是自己写的,之前自己写的代码 无法运行,很囧,初学者伤不起,代码用来学习理解EasyGui的用法。

 

import easygui as g
import os

def show_result(start_dir):
    lines = 0
    total = 0
    text = ""

    for i in source_list:
        lines = source_list[i]
        total += lines
        text += "【%s】源文件 %d 个,源代码 %d 行\n" % (i, file_list[i], lines)
    title = 统计结果
    msg = 您目前共累积编写了 %d 行代码,完成进度:%.2f %%\n离 10 万行代码还差 %d 行,请继续努力! % (total, total/1000, 100000-total)
    g.textbox(msg, title, text)

def calc_code(file_name):
    lines = 0
    with open(file_name) as f:
        print(正在分析文件:%s ... % file_name)
        try:
            for each_line in f:
                lines += 1
        except UnicodeDecodeError:
            pass # 不可避免会遇到格式不兼容的文件,这里忽略掉......
    return lines

def search_file(start_dir) :
    os.chdir(start_dir)
    
    for each_file in os.listdir(os.curdir) :
        ext = os.path.splitext(each_file)[1]
        if ext in target :
            lines = calc_code(each_file) # 统计行数
            # 还记得异常的用法吗?如果字典中不存,抛出 KeyError,则添加字典键
            # 统计文件数
            try:
                file_list[ext] += 1
            except KeyError:
                file_list[ext] = 1
            # 统计源代码行数
            try:
                source_list[ext] += lines
            except KeyError:
                source_list[ext] = lines
            
        if os.path.isdir(each_file) :
            search_file(each_file) # 递归调用
            os.chdir(os.pardir) # 递归调用后切记返回上一层目录
            
target = [.c, .cpp, .py, .cc, .cs,.java, .pas, .asm]
file_list = {}
source_list = {}

g.msgbox("请打开您存放所有代码的文件夹......", "统计代码量")
path = g.diropenbox("请选择您的代码库:")

search_file(path)
show_result(path)

 

 

测试程序,写放图

技术分享

 

哈哈,别被我的代码量吓到,其实我搜索的文件夹是我存放各种参考实例代码的文件夹,程序在idle里跑了估计有三分钟。

个人代码量不过万,新手伤不起,慢慢爬,慢慢努力~

python学习笔记:easygui的简单示例

标签:

原文地址:http://www.cnblogs.com/trey/p/4763096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!