zoukankan      html  css  js  c++  java
  • 用django统计代码行数+注释行数

    实现统计代码行数:

    1、首先在url.py中配置

    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^upload/', views.upload),
    
    ]

    2、写视图函数:实现统计代码的行数以及注释行数,其中以.py文件为例,注释是以“#”或者““”“”“””开头的,去掉空格

    from django.shortcuts import render,redirect
    
    # Create your views here.
    
    
    def upload(request):
        if request.method == "POST":
            file_obj = request.FILES.get("file")
            with open(file_obj.name, "wb") as f:
                for chunk in file_obj.chunks():
                    f.write(chunk)
                # 已经将文件保存在服务端,现在开始进行代码统计
            code_line = 0
            comment_line = 0
            flag = False
            with open(file_obj.name, "r", encoding="utf-8") as f2:
                for line in f2:
                    if line.strip():
                        if flag and not line.strip().startswith('"""'):
                            comment_line += 1
                        else:
                            if line.strip().startswith("#"):
                                comment_line += 1
                            elif line.strip().startswith('"""'):
                                if not flag:
                                    flag = True
                                else:
                                    flag = False
                                comment_line += 1
                            else:
                                code_line += 1
            return render(request, "show.html", {"filename": file_obj.name, "code_line": code_line, "comment_line": comment_line})
        return render(request, "upload.html")

    3、写html代码:

    upload.html的代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>上传代码</title>
        <link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.css">
    </head>
    <body>
    <form action="" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <div class="form-group" style="margin-top: 40px;margin-left: 200px;">
            <input type="file" name="file" id="exampleInputFile">
        </div>
        <button type="submit" class="btn btn-success" style="margin-left: 200px">提交</button>
    </form>
    </body>
    </html>

    show.html的代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>代码数</title>
    </head>
    <body>
    <ul style="margin-left: 200px;margin-top: 40px;">
        <li>文件名:{{filename}}</li>
        <li>代码行数:{{code_line}}</li>
        <li>注释行数:{{comment_line}}</li>
    </ul>
    </body>
    </html>

    引入bootstrap样式的时候,一定要配置static:

    STATIC_URL = '/static/'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]
  • 相关阅读:
    【CSS】4种CSS方法设置元素垂直水平居中
    js数据类型:引用和5种基本数据类型
    iOS——CALayer的shadow无效问题
    iOS——自定义Segue实现总结
    iOS动画
    iOS——Command-Line 查看当前SDK版本并修改默认SDK版本
    iOS——Swift开发中的单例设计模式(摘译,非原创)
    Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案
    Windows 8.1 开发过程中遇到的小问题(2)
    Windows 8.1 开发过程中遇到的小问题
  • 原文地址:https://www.cnblogs.com/hnlmy/p/9544633.html
Copyright © 2011-2022 走看看