PyCon 比print()还要方便的q()
关于PyCon大会,虽然知道 PyCon 的人不多,媒体报道少,但是,它依然是球最盛大也是最高级别的关于 Python 语言的会议了,全世界最牛的 Python 开发者都聚集在这里:PyCon 2018了解更多
q项目主页:https://github.com/zestyping/q
What is q ???
Quick and dirty debugging output for tired programmers.
为疲惫的程序员们创造的一种迅速而又随性的debug输出工具。
1. 安装q :
pip install -U q
![](https://img.haomeiwen.com/i6102062/a579f7c6be1c9ed7.png)
![](https://img.haomeiwen.com/i6102062/f62f7d6e39489e24.png)
![](https://img.haomeiwen.com/i6102062/c4e7aca7a521d375.png)
2. 使用q模块:
2.1)All output goes to /tmp/q, which you can watch with this shell command:
tail -f /tmp/q
在windows的 命令行中尝试了该命令:-----虽然该命令不能查看,还可以通过其他方法查看。
![](https://img.haomeiwen.com/i6102062/ca8ee80626b8efe6.png)
2.2)直接在写Python程序:
使用 import q; q.q( )函数就能替代用print()、logging()语句。
![](https://img.haomeiwen.com/i6102062/bcd68da5083ddcde.png)
2.3)找q模块的输出文件在哪里???
q.py源码:
![](https://img.haomeiwen.com/i6102062/f76d06981579c1f2.png)
这段代码的意思是:debugging日志会放入如下文件中,文件路径是去电脑的系统环境变量中查找 ‘TMPDIR’ or ‘TEMP’ or '/tmp',从前往后,先找到谁,就用谁。
![](https://img.haomeiwen.com/i6102062/5fff0dd3468697f3.png)
通过上图,去相应的目录下查找,可以查到输出文件q
![](https://img.haomeiwen.com/i6102062/233c6c44baac4689.png)
![](https://img.haomeiwen.com/i6102062/2c1c22373bd11478.png)
2.4)不想让debugging log输出文件放在我的C盘
方法很多:比如可以创建TMPDIR环境变量,固定q文件输出路径。
可以自己尝试。
3)q模块的其他用法:
To print the value of something in the middle of an expression, insert "q()", "q/", or "q|". For example, give this statement:
file.write(prefix + (sep or ' ').join(items))
...you can print out various values without using any temporary variables:
file.write(prefix + q(sep or '').join(items)) # prints (sep or '')
file.write(q/prefix + (sep or '').join(items)) # prints prefix
file.write(q|prefix + (sep or '').join(items)) # prints the arg to write
To trace a function's arguments and return value, insert this this above the def:
import q
@q
To start an interactive console at any point in your code, call q.d():
import q; q.d()
The following Lightning Talk shows how powerful using q can be.