类似shell的工具,使开发python代码更容易
devshell的Python项目详细描述
devshell公司
devshell是一个使python开发更容易的工具。在
主要特点
- 允许输入绑定到特定python对象的docstring的交互式会话。输入和响应被自动记录并作为doctest插入docstring。在
- 类似Shell的工具,用于导航、检查、测试和为项目中定义的python对象生成doctest
什么是医生,我为什么要在乎?在
doctest是类似于Python交互模式会话的文本片段。 doctest可以嵌入到代码中的docstring中,以达到两个目的:
- 在
为用户提供可执行的示例,以便他们能够更好地理解如何使用您的代码
在 - 在
通过运行这些生产线并确认产生预期输出来支持自动化测试
在
docstring是代码中的一块内联文本,位于模块、类或函数的开头,用于记录函数。当对对象调用builtin help()函数时,将显示该对象的类和方法的docString。此外,还有许多工具,如sphinx或pdoc,它们通过扫描项目中的docstring生成精美的文档文件。在
如何使用devshell
首先打开shell或命令行窗口并导航到包含感兴趣的包和/或模块的文件夹。 然后运行:
```
$ python -m devshell
Starting devshell command line interface...
Welcome to the devshell shell. Type help or ? to list commands.
(devshell)$
```
然后您将进入devshell shell,它的设计外观和感觉都非常类似于unixshell。 最大的区别是,devshell shell不是在实际的文件/目录中导航,而是在python包、模块、类和函数中导航。支持制表符补全。在
在shell中,可以键入help以列出所有命令。在
^{pr2}$也可以键入help后跟命令来获取有关该特定命令的信息:
```
(devshell)$ help ls
Help: (devshell)$ ls
This will show all items contained within the currently targeted item.
e.g. for a package, this would list the modules
e.g. for a module, this would list the functions and classes
etc
Note that using this command may result in importing the module containing the currently targeted item.
Note that setup.py files will be purposefully excluded because importing/inspecting them without providing commands results in terminating python.k
```
使用pwd、cd和ls命令浏览不同的项目:
```
devshell)$ ls
devshell package directory
test_pkg package directory
tests package directory
(devshell)$ cd test_pkg
(devshell)$ cd test_subpkg.test_mod.f
(devshell)$ pwd
/test_pkg.test_subpkg.test_mod.f
```
导航到感兴趣的项后,运行devshell命令以输入录制的交互式python会话。将自动导入目标项的包含模块中的所有项。实际上,您只需输入doctest输入,交互会话将对它们求值并显示输出。完成后,按Ctrl+D退出交互会话。此时,devshell将把记录的操作写入目标对象的docstring中。然后,它将对该对象运行doctest以确保没有问题。如果遇到任何问题,原始文件将被还原,有问题的文件将以特殊后缀保存在同一文件夹中。在
```
(devshell)$ devshell
Testing doctest execution of original file
...done: Fail count = 0, Total count = 0
Entering interactive console
Doctest insertion targeting object test_pkg.test_subpkg.test_mod.f within /home/mtm/interspace/devshell/test_pkg/test_subpkg/test_mod.py
Press Ctrl+D to stop writing code and incorporate session into the docstring of the targeted object
To abort this session without writing anything into the targeted file, call the exit() function
>>> from test_pkg.test_subpkg.test_mod import * # automatic import by devshell
>>> f(20)
20
>>>
Writing doctest lines to file
Testing doctest execution of new file
...done: Fail count = 0 (old=0), Total count = 1 (old=0)
File successfully updated
```
您可以使用doc或source命令验证doctest是用以下方式编写的:
```
(devshell)$ doc
>>> f(20)
20
(devshell)$ source
File: /home/mtm/interspace/devshell/test_pkg/test_subpkg/test_mod.py
def f(x):
"""
>>> f(20)
20
"""
return x
```
您还可以更改devshell shell正在扫描的模块和包的当前工作目录。 您可以使用chdir、listdir和getcwd导航filestystem,这些方法与具有相同名称的标准python os module方法执行相同的操作。 chdir和listdir支持制表符补全。在
要退出doctestshell,只需按Ctrl+D或键入quit命令。在
- 项目
标签: