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 ...
随机推荐
- 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
首先呢,还是一贯作风,我们先来看看众多应用中的示例:(这种效果是很常见的,可以说应用的必须品.) 搜狐客户端 ...
- Qt使用AES加密算法对字符串进行加密
因工作需要,需要对字符串进行加密处理,在网上找了很长时间,终于找到了一个可以使用的aes加密算法.其源代码采用c++编写而成,但其头文件引用windows.h,经过修改部分代码,将#inc ...
- easyui 快速开发整理
下面整理了关于easyui的datagrid的开发文档,复制黏贴即刻使用 1: <link href="../../Content/easyUI/themes/default/easy ...
- android 开发工具(转)
一.Android SDK (Android SDK主安装包,包含SDK Manager.AVD Manager.工具包tools,释放后的根文件夹为android-sdk-windows): rev ...
- js获取当前的时间(包含星期)
<script type="text/javascript"> setInterval("www_zzje_net.innerHTML=new ...
- C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询
1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法 ...
- jdbc 获取connection 对象的三种方式
获取数据库连接方法一:驱动实现类 //创建mysql的Driver对象 Driver driver=new com.mysql.jdbc.Driver(); //jdbc url 定位一个数据库: S ...
- C++ list用法
创建一个list实例并赋值: // 创建实例以及赋值 #include <iostream> #include <list> using namespace std; int ...
- css3之background
background background: (1)url(image1.png) right bottom, (2)url(image2.png) center, (3)url(image3.png ...
- php 练习一 5月5日
练习题一:通过登录者找到他的好友并显示在页面上 <title>无标题文档</title> <style type="text/css"> * { ...