呈现可复制的python代码示例(r包'reprex'的端口)

reprexp的Python项目详细描述


重复性

Render reproducible examples of Python code for posting to GitHub/Stack Overflow (port of R package reprex)

Linux Build StatusPyPI versionPython versions

reprexpy是一个python包,它将reproducibleexamples(也称为reprexesminimal working examples (MWEs))呈现为适合发布到github或堆栈溢出的格式。它是r包reprex的端口。

安装

您可以从pypi获得稳定版本:

pip install reprexpy

或者github的开发版本:

pip install git+https://github.com/crew102/reprexpy.git

基本示例

假设您想知道python中是否有扁平化列表的快捷方式,那么您可以创建下面的mwe来发布到so(mwe的灵感来自this SO question):

# i know that you can flatten a list in python using list comprehension:l=[[1,2,3],[4,5,6],[7],[8,9]][itemforsublistinlforiteminsublist]# but i'd like to know if there's another way. i tried this but i got an error:importfunctoolsfunctools.reduce(lambdax,y:x.extend(y),l)

您希望在示例中包含运行上述代码的输出,以便向人们显示您在终端中看到的内容:

# i know that you can flatten a list in python using list comprehension:l=[[1,2,3],[4,5,6],[7],[8,9]][itemforsublistinlforiteminsublist]#> [1, 2, 3, 4, 5, 6, 7, 8, 9]# but i'd like to know if there's another way. i tried this but i got an error:importfunctoolsfunctools.reduce(lambdax,y:x.extend(y),l)#> Traceback (most recent call last):#>  File "<stdin>", line 1, in <module>#>  File "<stdin>", line 1, in <lambda>#> AttributeError: 'NoneType' object has no attribute 'extend'

您可以在终端中运行代码,并将输出复制/粘贴到示例中。不过,这可能是一种痛苦,尤其是如果有很多输出要复制。更简单的方法是使用reprex()

运行reprex()时,mwe在ipython内核中运行。运行代码的输出(包括错误)将被捕获并与代码本身一起显示。关于ipython会话的详细信息也在示例的末尾通过调用SessionInfo()(稍后将详细介绍)。

包括matplotlib

reprex()使在reprexes中包含matplotlib图变得容易。它通过将图上载到imgur并在示例中包含指向它们的内联链接来实现这一点。例如,假设您有以下要发布到github的mwe:

importmatplotlib.pyplotaspltdata=[1,2,3,4]# i'm creating a plot hereplt.plot(data);plt.ylabel('some numbers');plt.show()plt.close()# another plotplt.plot(data);plt.xlabel('more numbers');plt.show()plt.close()

您可以使用reprex()

渲染sphinx代码示例

reprex()创建代码示例以插入sphinx文档是轻而易举的事。例如,假设您希望包含以下函数的示例:

defare_dogs_awesome():r"""Are dogs awesome?    Examples    --------    """return'Yep'

只要reprex()您的示例并将结果粘贴到docstring:

SessionInfo()

在前面的两个例子中,您可能已经注意到默认情况下reprex的末尾会添加一个名为“session info”的部分(注意,在0.2.0及更高版本中不再是这种情况)。本节使用SessionInfo()函数来包含有关用于运行reprex的ipython内核的详细信息,以及任何相关第三方包的版本号。请注意,只要您使用的是ipython内核(例如,在ipython终端或jupyter笔记本中),就可以在reprexes之外调用SessionInfo()

importpandasimportrequestsimportnumpyfromreprexpyimportSessionInfoSessionInfo()#> Session info --------------------------------------------------------------------#> Date: 2018-08-27#> Platform: Darwin-17.7.0-x86_64-i386-64bit (64-bit)#> Python: 3.5#> Packages ------------------------------------------------------------------------#> numpy==1.15.0#> pandas==0.23.4#> reprexpy==0.1.0#> requests==2.19.1

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
提交的java引用不明确:ExecutorService中的提交(可调用)和ExecutorService中的方法提交(可运行)匹配   java Get()和Set()在多个@Test、Selenium WebDriver之间   java更改同一会话中的entitymanager数据源   java为不同的日志文件配置不同的JDK日志格式   JavaSpring从JSON响应到网页   java IN子句在PreparedStatement上仅返回1行   java在使用数据提供程序时使用Testng优先级   如何在java中实现ketama算法?   java弹出窗口宽度和高度=0   java JLayeredPane未使用JFrame调整大小   将iCalendar DateTime转换为Java日期对象   java Fibonacci堆问题   java如何在spring数据jpa中映射友元请求实体   java Instanceof for generic with<?>或者没有<?>   java Hibernate回调用于许多联接表操作?