冠亚体育手机网站:android获取手机cpu并判断是单核还是多核,Python实现代码行数自动统计实例

复制代码 代码如下:

此功能用到了2个常用的Python标准库os和sys模块就解决。

/**
* Gets the number of cores available in this device, across all
processors.
* Requires: Ability to peruse the filesystem at
“/sys/devices/system/cpu”
* @return The number of cores, or 1 if failed to get result
*/
private int getNumCores() {
//Private Class to display only CPU devices in the directory listing
class CpuFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
//Check if filename is “cpu”, followed by a single digit number
if(Pattern.matches(“cpu[0-9]”, pathname.getName())) {
return true;
}
return false;
}
}

可能是因为这段时间在做的一个Python项目,用的是一个公司内部的IDE环境,而这个IDE环境产生的py代码并不是以文本方式存储,都是放在二进制文件里面的。

try {
//Get directory containing CPU info
File dir = new File(“/sys/devices/system/cpu/”);
//Filter to only list the devices we care about
File[] files = dir.listFiles(new CpuFilter());
//Return the number of cores (virtual CPU devices)
return files.length;
} catch(Exception e) {
//Default to return 1 core
return 1;
}
}

由于这门语言外界几乎接触不到,所以没有针对它的代码统计程序。一个模块完成后要统计代码行数会很困难的,要统计的话只能手工来操作,这不符合我们程序员的一惯风格。

您可能感兴趣的文章:

  • Android获取SD卡路径及SDCard内存的方法
  • 解析Android获取系统cpu信息,内存,版本,电量等信息的方法详解
  • Android实现获取SD卡总容量,可用大小,机身内存总容量及可用大小的方法
  • android实用工具类分享(获取内存/检查网络/屏幕高度/手机分辨率)
  • Android获取设备CPU核数、时钟频率以及内存大小的方法
  • Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法
  • Android
    获取屏幕高度,标题高度,状态栏高度(实例代码)
  • Android有效获取状态栏(StatusBar)高度的方法
  • Android编程实现获取标题栏、状态栏的高度、屏幕大小及模拟Home键的方法
  • Android中获取状态栏高度的两种方法分享
  • Android编程实现获取系统内存、CPU使用率及状态栏高度的方法示例

在这期间我一直在关注python语言,但是还没有动手真正的写python程序
。今天就利用中午休息的时间写了一个简单的代码统计程序。对输入路径作递归查找代码文件,对每一个代码文件计算注释行数、空行数、真正的代码行数。

统计代码行数Python代码如下:

 代码如下

#coding=utf-8
#iplaypython.py
import sys;
import os;

class LineCount:
    def trim(self,docstring):
        if not docstring:
            return ”
        lines = docstring.expandtabs().splitlines()
       
        indent = sys.maxint
        for line in lines[1:]:
            stripped = line.lstrip()
            if stripped:
                indent = min(indent, len(line) – len(stripped))
       
        trimmed = [lines[0].strip()]
        if indent < sys.maxint:
            for line in lines[1:]:
                trimmed.append(line[indent:].rstrip())
       
        while trimmed and not trimmed[-1]:
            trimmed.pop()
        while trimmed and not trimmed[0]:
            trimmed.pop(0)
       
        return ‘n’.join(trimmed)
   
    def FileLineCount(self,filename):
        (filepath,tempfilename) = os.path.split(filename);
        (shotname,extension) = os.path.splitext(tempfilename);
        if extension == ‘.txt’ or extension == ‘.hol’ : # file type
            file = open(filename,’r’);
            self.sourceFileCount += 1;
            allLines = file.readlines();
            file.close();
           
            lineCount    =0;
            commentCount = 0;
            blankCount   = 0;
            codeCount    = 0;
            for eachLine in allLines:
                if eachLine != ” ” :
                    eachLine = eachLine.replace(” “,””); #remove
space
                    eachLine = self.trim(eachLine);      #remove
tabIndent
                    if  eachLine.find(‘–‘) == 0 :  #LINECOMMENT
                        commentCount += 1;
                    else :
                        if eachLine == “”:
                            blankCount += 1;
                        else :
                            codeCount += 1;
                lineCount = lineCount + 1;
            self.all += lineCount;
            self.allComment += commentCount;
            self.allBlank += blankCount;
            self.allSource += codeCount;
            print filename;
            print ‘           Total      :’,lineCount ;
            print ‘           Comment    :’,commentCount;
            print ‘           Blank      :’,blankCount;
            print ‘           Source     :’,codeCount;
                   
    def CalulateCodeCount(self,filename):
        if os.path.isdir(filename) :
            if not filename.endswith(”):
                filename += ”;
            for file in os.listdir(filename):
                if os.path.isdir(filename + file):
                    self.CalulateCodeCount(filename + file);
                else:
                    self.FileLineCount(filename + file);
        else:
            self.FileLineCount(filename);

    # Open File
    def __init__(self):
        self.all = 0;
        self.allComment =0;
        self.allBlank = 0;
        self.allSource = 0;
        self.sourceFileCount = 0;
        filename = raw_input(‘Enter file name: ‘);
        self.CalulateCodeCount(filename);
        if self.sourceFileCount == 0 :
            print ‘No Code File’;
            pass;
        print ‘n’;
        print ‘*****************  All Files 
**********************’;
        print ‘    Files      :’,self.sourceFileCount;
        print ‘    Total      :’,self.all;
        print ‘    Comment    :’,self.allComment;
        print ‘    Blank      :’,self.allBlank;
        print ‘    Source     :’,self.allSource;
        print
‘****************************************************’;

myLineCount = LineCount();

我们看到代码中 extension == ‘.txt’ or extension ==
‘.hol’这句代码是用来判定文件的后缀名,可以确定是否要计算代码行数。

if  eachLine.find(‘–‘) == 0
:是用来判定当前行是不是单行注释,为了能在其他机器上运行,使用了py2exe来把python脚本生成可执行的exe。

 代码如下

from distutils.core import setup
import py2exe

setup(
 
    version = “0.0.1”,
    description = “LineCount”,
    name = “LineCount”,
 
    console = [“LineCount.py”],
    )

利用Python统计源代码行数以及对源代码排版

近段时间写的一个软件需要做软件著作权登记,需要统计源代码行数以及提交部分源代码,刚好利用Python写了这么一个小工具。

用法如下:

在windows命令行窗口,使用python python_count_line.py
source_code_root_directory即可。

其中,source_code_root_directory为需要统计的源代码的根目录,源代码目录中如果有其它子目录,也可以递归遍历。

源代码如下:

 代码如下

import os,sys

def count_line(filename):
    “”” count the line number of file”””
    with open(filename) as file:
        lines = file.readlines()
        file.close()
        return len(lines)

def
read_lines_and_write_to_target(source_filename,target_file,begin_linenum):
    linenum = begin_linenum
    with open(source_filename) as source_file:
        lines = source_file.readlines()
        for i in range(len(lines)):
            target_file.write(“%4d”%linenum+”  “)
            target_file.write(lines[i])
            linenum = linenum+1
        source_file.close()
    return linenum
       
def flatten(lst):
    for x in lst:
        if isinstance(x,list):
            for x in flatten(x):
                yield x
        else:
            yield x

def count_files(root_dir,file_filter):
    process_list = get_process_files(root_dir,file_filter)

    process_list = list(flatten(process_list))
    print “Flatten Process List”,
    print process_list

    line_count = 0
    for file in process_list:
        line_count = line_count + count_line(file)
        print file,”line_number”,line_count
   
    #Generate Source Files in One
    line_count = 1
    target_filename=root_dir+””+”result.txt”
    with open(target_filename,”w”) as target_file:
        for file in process_list:
            target_file.write(“///////”+file+”///////n”)
            line_count =
read_lines_and_write_to_target(file,target_file,line_count)
        target_file.close()
    print “Result is generated in target file:”,target_filename

def get_process_files(root_dir,file_filter):
    “”” process all files in directory that matches file_filter “””
    cur_dir = os.path.abspath(root_dir)
    file_list = os.listdir(cur_dir)
    print file_list
    process_list=[]
    #processing the file_list, retrieve all target files to
process_list
    for file in file_list:
        fullfile = cur_dir+””+file
        print fullfile
        if os.path.isfile(fullfile):
            print “is file”
            found_flag = False
            for filter in file_filter:
                if file.rfind(filter) != -1:
                    print “match!”,file
                    process_list.append(fullfile)
                    found_flag = True
            if found_flag==False:
                print “pass this file”,fullfile
        elif os.path.isdir(fullfile):
            print “is dir,recursive”
            dir_extra_list =
get_process_files(fullfile,file_filter)
            print dir_extra_list
            if len(dir_extra_list)!=0:
                process_list.append(dir_extra_list)
        else:
            print “not defined”
    return process_list

if __name__ == ‘__main__’:
    print sys.argv
    root_dir = sys.argv[1]
    print “INFO: Root_Dir:”,root_dir
    file_filter = [“.cpp”,”.h”]
    count_files(root_dir,file_filter)

   

统计某文件夹下源代码行数 Python脚本

写了一个统计某文件夹下各种源代码行数的脚本

仅支持c/c++ java python

有需要可以自己更改程序,我把这个脚本的拓展性做的很高

很容易使之支持其他文本型语言代码

或者给我Email我帮你做 onezeros@yahoo.cn

对于MFC自动生成的代码应该排除在外

可以用一种不太严密的方法:先统计各种mfc生成工程代码数量

然后在分析文件夹时根据mfc文件的一些特征判断目标文件夹是否为mfc工程

如果是,就减去生成的代码量

 代码如下

#!/usr/bin/python
#codelines.py short for files in directory
import sys
import os
import os.path

class Element:
    _fileamount=0#number of files
    _codelines=0 #line number of files
    _commandlines=0;#standalone command lines
   
extension={‘c’:0,’cpp’:0,’C’:0,’cc’:0,’cxx’:0,
        ‘h’:0,’hpp’:0,’hxx’:0,’java’:1,’py’:2}  
language=[‘c/c++’,’java’,’py’]
statistics=[Element() for x in range(0,len(language))]

#count lines in a file
def fileparser(file,ext):
    global extension,statistics
    try:
        f=open(file,’r’)
    except:
        print(“failed to open “+file)
    print(“parsing file “+file)
    #print(“extension “+str(extension[ext]))
    statistics[extension[ext]]._fileamount+=1
    try:
        context=f.readlines()
        i=-1;
        while i<len(context):
            i+=1
            line=context[i].strip()
            if line==”:
                continue
            else:
                if extension[ext]==0 or ext==’java’:#c/cpp#java
                    if line[0:2]==’//’:
                       
statistics[extension[ext]]._commandlines+=1
                    elif line[0:2]==’/*’:
                        while line.find(‘*/’)==-1:
                            i+=1;
                            line=context[i].strip()
                           
statistics[extension[ext]]._commandlines+=1
                       
statistics[extension[ext]]._commandlines+=1
                    else:
                        statistics[extension[ext]]._codelines+=1
                elif ext==’py’:#python
                    if line[0]==’#’:
                       
statistics[extension[ext]]._commandlines+=1
                    else:
                        statistics[extension[ext]]._codelines+=1
    except:
        pass
    f.close()
    return
#find source code file by analyzing extension
def extparser(file):
    global extension
    ext=os.path.splitext(file)[1][1:]
    #print(“extension “+ext)
    if ext in extension:
        #print(“in  extparing  “+file)
        fileparser(file,ext)
    else:
        pass
    return
   
#parse a directory to find files
def dirparser(directory):
    print(“parsing directory “+directory)
    try:
        dirlist=os.listdir(directory)
        for i in dirlist:
            fullpath=directory+’//’+i
            if(os.path.isdir(fullpath)==True):
                dirparser(fullpath)
            else:
                extparser(fullpath)
    except:
        pass
    return

if __name__ == “__main__”:
    if(os.path.isdir(sys.argv[1])==False):
        print(‘%s is not a correct path’,sys.argv[1]);
        sys.exit(0)
    dirparser(sys.argv[1])
    length=[len(‘language:  ‘),len(‘file amount: ‘),
        len(‘code amount: ‘),len(‘command amount’)]
    print(‘language:  file amount:  code amount:  command amount’)
    for i,lang in enumerate(language):
        print(repr(lang).ljust(length[0]),
            repr(statistics[i]._fileamount).ljust(length[1]),
            repr(statistics[i]._codelines).ljust(length[1]),
            repr(statistics[i]._commandlines).ljust(length[1]))
   
    print(‘Done’)

Post Author: admin

发表评论

电子邮件地址不会被公开。 必填项已用*标注