-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlib01.13-sys.py
More file actions
278 lines (219 loc) · 6.99 KB
/
lib01.13-sys.py
File metadata and controls
278 lines (219 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
'''
sys 模块
sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分.
'''
'''
处理命令行参数
'''
# 使用 sys 模块获得脚本的参数
# 在解释器启动后, argv 列表包含了传递给脚本的所有参数. 列表的第一个元素为脚本自身的名称.
import sys
print("script name is", sys.argv[0])
if len(sys.argv) > 1:
print('there are', len(sys.argv)-1,'arguments')
for arg in sys.argv[1:]:
print(arg)
else:
print('there are no arguments!')
'''
script name is E:\360\Python\PythonCode\01Study\03-StandardLibrary\lib01.12-sys.py
there are no arguments!
'''
'''
处理模块
path 列表是一个由目录名构成的列表, Python 从中查找扩展模块( Python 源模块, 编译模块,或者二进制扩展).
启动 Python 时,这个列表从根据内建规则,PYTHONPATH 环境变量的内容, 以及注册表( Windows 系统)等进行初始化.
'''
'''
# 使用 sys 模块操作模块搜索路径
import sys
print('path has',len(sys.path),'members')
sys.path.insert(0,'samples')
import sample
sys.path = []
import random
'''
# 使用 sys 模块查找内建模块
# builtin_module_names 列表包含 Python 解释器中所有内建模块的名称
import sys
def dump(module):
print(module,'=>','',end='')
if module in sys.builtin_module_names:
print('<BUILTIN>')
else:
module = __import__(module)
print(module.__file__)
dump('os')
dump('sys')
dump('string')
# Python3 中移除了 audiodev, Bastion, bsddb185, exceptions, linuxaudiodev, md5, MimeWriter, mimify, popen2, rexec, sets, sha, stringold, strop, sunaudiodev, timing和xmllib模块
# dump('strop')
dump('zlib')
'''
os => C:\ProgramFile\Python33\lib\os.py
sys => <BUILTIN>
string => C:\ProgramFile\Python33\lib\string.py
zlib => <BUILTIN>
'''
# 使用 sys 模块查找已导入的模块
# modules 字典包含所有加载的模块. import 语句在从磁盘导入内容之前会先检查这个字典.
import sys
print(len(sys.modules))
print(sys.modules.keys())
'''
58
dict_keys(['_weakrefset', 'os.path', 'collections.abc', 'sys', 'site', 'copyreg', 'stat', '_frozen_importlib', 'winreg', 'zipimport', 'genericpath', '_collections', 'sre_compile', 'sre_parse', 'nt', '_io', 're', 'collections', 'ntpath', '_weakref', 'errno', '_locale', 'abc', '_functools', 'string', 'encodings.cp437', '_imp', 'builtins', 'operator', 'codecs', 'encodings.gbk', 'sysconfig', 'io', 'encodings', '__main__', 'functools', 'locale', '_codecs', 'encodings.utf_8', 'heapq', 'reprlib', '_multibytecodec', 'os', '_warnings', 'itertools', 'encodings.latin_1', 'weakref', 'marshal', '_string', 'encodings.aliases', 'signal', '_sre', 'sre_constants', '_codecs_cn', 'encodings.mbcs', '_heapq', 'keyword', '_thread'])
'''
'''
处理引用记数
'''
# 使用 sys 模块获得引用记数
# getrefcount 函数返回给定对象的引用记数 - 也就是这个对象使用次数.
# Python 会跟踪这个值, 当它减少为 0 的时候, 就销毁这个对象.
import sys
variable = 1234
print(sys.getrefcount(0))
print(sys.getrefcount(variable))
print(sys.getrefcount(None))
# [注] 这个值总是比实际的数量大, 因为该函数本身在确定这个值的时候依赖这个对象.
# 使用 sys 模块获得当前平台
# platform 变量, 它包含主机平台的名称.
import sys
print(sys.platform)
if sys.platform == 'win32':
import ntpath
pathmodule = ntpath
elif sys.platform == 'mac':
import macpath
pathmodule = macpath
else:
import posixpath
pathmodule = posixpath
print(pathmodule)
'''
win32
<module 'ntpath' from 'C:\\ProgramFile\\Python33\\lib\\ntpath.py'>
'''
'''
跟踪程序
'''
# 使用 sys 模块配置分析函数
# setprofile 函数允许你配置一个分析函数(profiling function).
# 这个函数会在每次调用某个函数或方法时被调用(明确或隐含的), 或是遇到异常的时候被调用.
import sys
def test(n):
j = 0
for i in range(n):
j = j + 1
return n
def profiler(frame,event,arg):
print(event,frame.f_code.co_name,frame.f_lineno,'=>',arg)
# 分析函数将在下次函数调用, 返回, 或异常时激活
sys.setprofile(profiler)
# 分析这次函数调用
test(1)
# 禁用分析函数
sys.setprofile(None)
# 不会分析这次函数调用
test(2)
'''
call test 133 => None
return test 137 => 1
c_call <module> 145 => <built-in function setprofile>
'''
# 使用 sys 模块配置单步跟踪函数
# settrace 函数与此类似, 但是 trace 函数会在解释器每执行到新的一行时被调用.
import sys
def test(n):
j = 0
for i in range(n):
j = j + 1
return n
def tracer(frame,event,arg):
print(event,frame.f_code.co_name,frame.f_lineno,'=>',arg)
return tracer
# 分析函数将在下次函数调用, 返回, 或异常时激活
sys.settrace(tracer)
# 分析这次函数调用
test(1)
# 禁用分析函数
sys.settrace(None)
# 不会分析这次函数调用
test(2)
'''
call test 161 => None
line test 162 => None
line test 163 => None
line test 164 => None
line test 163 => None
line test 165 => None
return test 165 => 1
'''
# 基于该函数提供的跟踪功能, pdb 模块提供了完整的调试( debug )框架.
'''
处理标准输出/输入
stdin , stdout , 以及 stderr 变量包含与标准 I/O 流对应的流对象.
如果需要更好地控制输出,而 print 不能满足你的要求, 它们就是你所需要的.
你也可以替换它们, 这时候你就可以重定向输出和输入到其它设备( device ), 或者以非标准的方式处理它们.
'''
# 使用 sys 重定向输出
# 要重定向输出只要创建一个对象, 并实现它的 write 方法.
import sys
class Redirect(object):
"""docstring for Redirect"""
def __init__(self, stdout):
super(Redirect, self).__init__()
self.stdout = stdout
def write(self,s):
self.stdout.write(str.lower(s))
# 重定向标准输出(包括 print 语句)
old_stdout = sys.stdout
sys.stdout = Redirect(sys.stdout)
print('HEJA SVERIGE')
print('FRISKT HUMR')
# 恢复标准输出
sys.stdout = old_stdout
print('MWERWEREWVZDL!')
'''
heja sverige
friskt humr
MWERWEREWVZDL!
'''
'''
退出程序
执行至主程序的末尾时,解释器会自动退出.
但是如果需要中途退出程序, 你可以调用 sys.exit 函数, 它带有一个可选的整数参数返回给调用它的程序.
'''
# 使用 sys 模块退出程序
'''
import sys
print('hello')
sys.exit(1)
print('world')
'''
# 捕获 sys.exit 调用
# 注意 sys.exit 并不是立即退出. 而是引发一个 SystemExit 异常.
# 这意味着你可以在主程序中捕获对 sys.exit 的调用
import sys
print('hello')
try:
sys.exit(1)
except SystemExit:
print('hello world')
print('world')
'''
hello
hello world
world
'''
# 另一种捕获 sys.exit 调用的方法
# 如果准备在退出前自己清理一些东西(比如删除临时文件), 你可以配置一个 "退出处理函数"(exit handler), 它将在程序退出的时候自动被调用.
import sys
print('---------------')
def exitfunc():
print('world')
sys.exitfunc = exitfunc
print('hello')
sys.exit(1)
print('there ')
# 在 Python 2.0 以后, 你可以使用 atexit 模块来注册多个退出处理函数.