python 标准库基础学习之开发工具部分1学习

2023-06-12,,

#2个标准库模块放一起学习,这样减少占用地方和空间
#标准库之compileall字节编译源文件
import compileall,re,sys
#作用是查找到python文件,并把它们编译成字节码表示,将结果保存到.pyc,pyo文件中
#编译一个目录
#compile_dirr()递归地扫描一个目录,并对其中文件完成字节编译
compileall.compile_dir(r'b')
#默认情况下,所有子目录都会扫描,直到尝试达到10
#筛选目录,可以使用rx参数提供一个正则表达式来匹配要排除的目录名
compileall.compile_dir(r'b',rx=re.compile(r'c'))

#maxlevels参数控制递归深度:编译指定目录.py文件
compileall.compile_dir(r'b',maxlevels=0,rx=re.compile(r'c/.doc'))

#编译sys.path骼,就可以编译sys.path中找到所有python源文件
sys.path[:]=[r'python.py path']
print sys.path
compileall.compile_path()

#从命令行执行
#win在cmp下面
"""
>>>python -m compileall -h
option -h not recognized
usage: python compileall.py [-l] [-f] [-q] [-d destdir] [-x regexp] [-i list] [d
irectory|file ...]

arguments: zero or more file and directory names to compile; if no arguments giv
en,
           defaults to the equivalent of -l sys.path

options:
-l: don't recurse into subdirectories
-f: force rebuild even if timestamps are up-to-date
-q: output only error messages
-d destdir: directory to prepend to file paths for use in compile-time traceback
s and in
            runtime tracebacks in cases where the source file is unavailable
-x regexp: skip files matching the regular expression regexp; the regexp is sear
ched for
           in the full path of each file considered for compilation
-i file: add all the files and directories listed in file to the list considered
 for
         compilation; if "-", names are read from stdin
"""
#如果要创建之前新例子,跳过指定的目录,可以使用以下命令
#<python -m compileall -x '/path' examples>
#'path'指你要跳过的目录
#compileall官方标准库地址:https://docs.python.org/2.7/library/compileall.html?highlight=compileall#module-compileall

print '-'*100
#标准库之pyclbr类浏览器
import pyclbr
#作用:实现一个适用于源代码编辑器api,可以用来建立一个类浏览器
#注意:在pycharm虽然显示可以用,但运行发现报错:ImportError: No module named pyclbr
"""
它可以扫描python源代码来查找类和独立函数,可以课代表和tokenize收集类,方法和函数名及行号有关信息,不用导入代码
"""
#例子
class a(object):
    pass
class a1(a):pass
class b:
    pass
class c(a,b):
    def m1(self):
        return
    def m2(self):
        return

def func():
    pass
#扫描类,有2个公共函数,第一个是readmodule(),是以模式名作为参数,并返回一个字典
#将类名映射到Class对象,其中包含有关源代码的元数据.
import os
from operator import itemgetter
def show_class(name,cls1):
    print name
    file1=os.path.basename(cls1.file)
    print '{0}.{1}'.format(file1,cls1.lineno)
    show_class(name,cls1)
    show_mode1(name,cls1)
    print
    return
def show_mode1(name,cls1):
    for i,j in sorted(cls1.methods.items(),key=itemgetter(1)):
        print '{0}.{1}'.format(i,j)
    return
def show_super_class(name,cls1):
    super_name=[]
    for c_super in cls1.super:
        if c_super=='object':
            continue
        if isinstance(c_super,basestring):
            super_name.append(c_super)
        else:
            super_name.append(c_super.name)
    if c_super:
        print super_name
    return
data=pyclbr.readmodule('exp')#exp是表示py路径
for a,b in sorted(data.items(),key=lambda x:x[1].lineno):
    show_class(a,b)

#说明:类的源数据包括定义这个类的文件所有行号,还包括超类类名,类方法保存为方法名与行号之间的映射
#输出显示这些类和方法,输出显示这些类和方法(根据它们在源文件中行号的排序)

#扫描函数:使用readmodule_ex()方法,它会完成readmodule()所有全部工作,代码和上面类似

#pyclbr官方标准库地址:https://docs.python.org/2.7/library/pyclbr.html?highlight=pyclbr#module-pyclbr

#inspet模块可以发现有关类和函数更多元数据,不过需要导入此模块

#tokenize模块可以将python源代码解析为toke!

python 标准库基础学习之开发工具部分1学习的相关教程结束。

《python 标准库基础学习之开发工具部分1学习.doc》

下载本文的Word格式文档,以方便收藏与打印。