python 标准库基础学习之开发工具部分1学习
#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标准库基础之mmap:内存映射文件
#作用:建立内存映射文件而不是直接读取内容文本信息内容:如下(名称是text.txt) Lorem ipsum dolor sit amet, consectetuer adipiscing elit ...
- python linecache标准库基础学习
#python标准库基础之:linecacge:高效读取文本文件#说明与作用"""可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件 ...
- Python标准库07 信号 (signal包,部分os包)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux的信号基础之后,Python标准库中的signal包就很容易学习 ...
- Python 标准库 urllib2 的使用细节[转]
转自[http://zhuoqiang.me/python-urllib2-usage.html] Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节描述的并不清楚,比 ...
- Python 标准库 urllib2 的使用细节
刚好用到,这篇文章写得不错,转过来收藏. 转载自 道可道 | Python 标准库 urllib2 的使用细节 Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节 ...
- Python 标准库 urllib2 的使用细节(转)
http://www.cnblogs.com/yuxc/archive/2011/08/01/2123995.html http://blog.csdn.net/wklken/article/deta ...
- 转Python 标准库 urllib2 的使用细节
Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节描述的并不清楚,比如 urllib2 这个 HTTP 客户端库.这里总结了一些 urllib2 库的使用细节. 1 P ...
- Python 标准库一览(Python进阶学习)
转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...
- python标准库学习-SimpleHTTPServer
这是一个专题 记录学习python标准库的笔记及心得 简单http服务 SimpleHTTPServer 使用 python -m SimpleHTTPServer 默认启动8000端口 源码: &q ...
随机推荐
- hdu2209翻纸牌游戏
翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- Python函数对象
秉承着一切皆对象的理念,我们再次回头来看函数(function).函数也是一个对象,具有属性(可以使用dir()查询).作为对象,它还可以赋值给其它对象名,或者作为参数传递. lambda函数 在展开 ...
- saltstack对递归依赖条件(死循环依赖)的处理
本文将对saltstack中状态文件中require条件产生死循环的情形进行简单的测试和分析 测试思路: 写一个包含递归依赖条件的状态文件,进行测试: A依赖于B B依赖于C ...
- Webstorm10.0.3破解程序及汉化包下载、Webstorm配置入门指南
核心提示: WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为“Web前端开发神器”.“最强大的HTML5编辑器”.“最智能的JavaSscri ...
- OD: Writing Small Shellcode
第 5.6 节讲述如何精简 shellcode,并实现一个用于端口绑定的 shellcode.原书中本节内容来自于 NGS 公司的安全专家 Dafydd Stuttard 的文章 “Writing S ...
- css中的clear的正真意义
网上包括w3cschool 的说法都是 不允许浮动.消除浮动 这样的说法,却看了还是不知道什么意思,一些地方说不通. 所以找到w3c css 的英文文档,如下是节选: 'clear'Values ha ...
- js获取页面名称
function pageName() { var strUrl = location.href; var arrUrl = strUrl.split("/"); ...
- Lesson 2: Dive Into Typography (排版)
Lesson 2: Dive Into Typography (排版) 排版是字的艺术,是关于字的一切:字体.字号.行高.行长.字重(斜体/加粗/正常).字距(kerning).行距(leading) ...
- MySql不支持主外键
创建表不支持主外键,能够添加外键成功,但是无法外键约束.查资料发现MySql的默认ENGINE 为MyISAM ,不支持外键,需要修改为 INNODB 修改前: Create Table CREAT ...
- Comparable与compareTo
Comparable 1.什么是Comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它的自然比较方法 .实现此接口 ...