设为首页 收藏本站
查看: 659|回复: 0

[经验分享] python 标准库基础学习之开发工具部分1学习

[复制链接]

尚未签到

发表于 2015-12-3 09:27:28 | 显示全部楼层 |阅读模式
#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!
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-146642-1-1.html 上篇帖子: python基础教程学习笔记---(3)字符串 下篇帖子: python list 的+、+=和extend操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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