Render reproducible examples of Python code (port of R package `reprex`)
Project description
reprexpy
Render reproducible examples of Python code for posting to GitHub/Stack Overflow (port of R package
reprex
)
reprexpy
is a Python package that renders reproducible examples (also known as reprexes or minimal working examples (MWEs)) so that they are ready to be posted to GitHub or Stack Overflow. It is a port of the R package reprex.
Installation
You can get the stable version from PyPI:
pip install reprexpy
Or the development version from GitHub:
pip install git+https://github.com/crew102/reprexpy.git
A basic example
Let's say you want to know if there's a shortcut for flatting lists in Python, so you create the following MWE to post to SO (example inspired by 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]]
[item for sublist in l for item in sublist]
# but i'd like to know if there's another way. i tried this but i got an error:
import functools
functools.reduce(lambda x, y: x.extend(y), l)
You'd like to put the outputs of running the above code into the example itself, to show people what you are seeing in your terminal:
# i know that you can flatten a list in python using list comprehension:
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
[item for sublist in l for item in sublist]
#> [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:
import functools
functools.reduce(lambda x, 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 terminal 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 to include the outputs is to use reprex()
:
When you run reprex()
, your code is run inside an IPython kernel and the outputs (including errors) are captured and displayed alongside the code. Details on the IPython session are given as well in a section called "Session Info" (more on this later).
Including matplotlib
plots
reprex()
makes it easy to include matplotlib
plots 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:
import matplotlib.pyplot as plt
data = [1, 2, 3, 4]
# i'm creating a plot here
plt.plot(data);
plt.ylabel('some numbers');
plt.show()
plt.close()
# another plot
plt.plot(data);
plt.xlabel('more numbers');
plt.show()
plt.close()
You can render this reprex to a format suitable for posting to GitHub using reprex()
:
SessionInfo()
You may have noticed in the previous two examples that a section called "Session Info" is added to the end of your reprex. This section includes details about the IPython kernel that was used to render your reprex, such as the Python version that was used and the platform the code was run on, as well as the version numbers of the packages used in your example (e.g., matplotlib
). You can use the SessionInfo()
function to get this information whenever you are using an IPython kernel (e.g., when you are inside an IPython terminal or Jupyter notebook):
import pandas
import requests
import numpy
from reprexpy import SessionInfo
SessionInfo()
#> 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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file reprexpy-0.1.1.tar.gz
.
File metadata
- Download URL: reprexpy-0.1.1.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7daae3ac81822e6bdf994399c03863310bc0cfef9b75b21ae8efc33d8e794b6 |
|
MD5 | 1ba736ddf4ecc3a9ee4ad0e4612e1e4e |
|
BLAKE2b-256 | 067a6aa6f98fa99702b1c1b41927e204bc6241ade7215a428759fe58671401be |
File details
Details for the file reprexpy-0.1.1-py2.py3-none-any.whl
.
File metadata
- Download URL: reprexpy-0.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 351964ab7d8a52fd07568da0faee279b67c206cdbc31730cd4e8715c04a76d56 |
|
MD5 | 7794f62b7fb7ead82a55b29c4009a6a4 |
|
BLAKE2b-256 | 6a8d3365fe2e3f8521f697d5bd38b6eba9b9f3a229428472eb8ed8836d09e8db |