Package: reprexpy
reprexpyRenderreproducibleexamples of Python code for posting to GitHub/Stack Overflow (port of R packagereprex)reprexpyis a Python package that rendersreproducibleexamples (also known asreprexesorminimal working examples (MWEs)) to a format suitable for posting to GitHub or Stack Overflow. It's a port of the R packagereprex.InstallationYou can get the stable version from PyPI:pip install reprexpyOr the development version from GitHub:pip install git+https://github.com/crew102/reprexpy.gitA basic exampleLet's say you want to know if there's a way to flatten lists in Python without using list comprehension, so you create the following MWE to post to SO (MWE inspired bythis 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)You'd like to include the outputs of running the above code into the example itself, to show people what you're seeing in your console:# 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'You could run the code in your console and copy/paste the outputs into your example. That can be a pain, though, especially if you have a lot of outputs to copy. An easier way is to usereprex():When you runreprex(), your MWE is run inside an IPython kernel. The outputs from running your code (including errors) are captured and displayed alongside the code itself. Details on the IPython session are also given at the end of your example by callingSessionInfo()(more on this later).Includingmatplotlibplotsreprex()makes it easy to includematplotlibplots in your reprexes. It does this by uploading the plots to imgur and including inline links to them in your example. For example, let's say you have the following MWE that you want to post to GitHub: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()You can prepare this reprex for posting to GitHub usingreprex():SessionInfo()You may have noticed in the previous two examples that a section called "Session info" is added to the end of your reprex by default (note, this is no longer the case in version 0.2.0 and above). This section uses theSessionInfo()function to include details about the IPython kernel that was used to run your reprex, as well as the version numbers of relevant third-party packages used in your example. Note that you can callSessionInfo()outside of reprexes, so long as you're using an IPython kernel (e.g., when inside an IPython terminal or Jupyter notebook):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.1Render code examples for docstringsCreating code examples to insert into docstrings is a breeze withreprex(). For example, let's say you want to include an example for the following function:defare_dogs_awesome():r"""Are dogs awesome?Examples--------"""return'Yep'Justreprex()your example and paste the result into your docstring: