Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating doctest output #37

Open
CagtayFabry opened this issue Sep 20, 2021 · 10 comments
Open

creating doctest output #37

CagtayFabry opened this issue Sep 20, 2021 · 10 comments
Labels
enhancement New feature or request

Comments

@CagtayFabry
Copy link

thank you for creating this handy tool (and the jupyter cell magic!) 💯

it would be very handy to be able to create doctest compatible output from reprexlite (I see there is already an issue for reading doctest format inputs)

so basically going from

from itertools import product
grid = list(product([1, 2, 3], [8, 16]))
grid
#> [(1, 8), (1, 16), (2, 8), (2, 16), (3, 8), (3, 16)]
list(zip(*grid))
#> [(1, 1, 2, 2, 3, 3), (8, 16, 8, 16, 8, 16)]

to

>>> from itertools import product
>>> grid = list(product([1, 2, 3], [8, 16]))
>>> grid
[(1, 8), (1, 16), (2, 8), (2, 16), (3, 8), (3, 16)]
>>> list(zip(*grid))
[(1, 1, 2, 2, 3, 3), (8, 16, 8, 16, 8, 16)]

I tried to get there by using the options but run into a few issues:

  • how can I pass 'nothing' to the --comment option?
  • is there an option for code prefixes? (adding >>> to the code lines)

(ultimately having --venue=doctest available would really sweet obviously)

@jayqi
Copy link
Owner

jayqi commented Sep 20, 2021

Hi @CagtayFabry, thanks for your interest in reprexlite!

Can you explain a little bit more about why you want to use reprexlite for doctest outputs? One of the core design ideas behind reprexlite is to be an alternative to doctest. Even if this weren't the case, it's also not clear to me that something like reprexlite is the right tool for this. You could simply open a Python REPL from the command line and copy-paste from there.

@CagtayFabry
Copy link
Author

thanks for getting back here @jayqi

basically I currently use reprexlite for a convenient way to produce examples/bug reports etc. for GH. I usually generate those from a jupyter environment. Some of my projects rely on doctest for docstrings and testing.
The main benefits I could see in using reprexlite for creating doctest-like output over running code in an iPython shell are:

  • better handling of multiline scripts with multiple outputs at the correct line (without adding print etc.)
  • ability to black-format the input
  • stick to a notebook environment to generate both (GH format + doctest)

I do agree on your assessment of the drawbacks when using doctest format but I need the sphinx/pytest integration etc.

@jayqi
Copy link
Owner

jayqi commented Sep 20, 2021

I guess that makes sense.

I think rather than a --venue=doctest, it probably makes more sense to implement it through different parameters, since you might want doctest-style output in any of the venues. Discussion of two possible approaches to implementation:

  1. Have some kind of switch --style=reprex or --style=doctest.
    • This might not be the right abstraction since the --comment option doesn't make as much sense in this context.
  2. Add prompt parameter(s) that you can set to >>> (and maybe also ... for multiline). Figure out how to set --comment to empty string.
    • This seems like it would be the most general approach, and would be able to support doctest as well as whatever other styles that someone cooks up.

Another angle to your use case is maybe open issue #16. I'm not sure how sphinx or pytest handle doctests so I don't know much on top of that it would take to support sphinx or pytest.

@jayqi jayqi added the enhancement New feature or request label Sep 20, 2021
@jayqi
Copy link
Owner

jayqi commented Oct 8, 2021

I did poke around at this and made some progress. It does involve a bunch of refactoring as it doesn't quite line up with some of my abstractions. 😅

One annoying thing still to solve is that libcst, which I use to parse the code, attaches whitespace lines to code statements following them, which ends up misformatting the code after empty lines. That will involve some finagling.

>>> from itertools import product
>>> 
... grid = list(product([1, 2, 3], [8, 16]))
>>> grid
[(1, 8), (1, 16), (2, 8), (2, 16), (3, 8), (3, 16)]
>>> for var in list(zip(*grid)):
...     print(var)
(1, 1, 2, 2, 3, 3)
(8, 16, 8, 16, 8, 16)

@jayqi
Copy link
Owner

jayqi commented Feb 23, 2023

Hi @CagtayFabry,

I know it's been a while, but I did a major overhaul of reprexlite's internals and this has been merged to the main branch. It's not released yet, but if you want to start using it, you can install from source.

reprex --prompt=">>>" --continuation="..." --comment=""
>>> from itertools import product
>>> 
>>> grid = list(product([1, 2, 3], [8, 16]))
>>> grid
[(1, 8), (1, 16), (2, 8), (2, 16), (3, 8), (3, 16)]
>>> list(zip(*grid))
[(1, 1, 2, 2, 3, 3), (8, 16, 8, 16, 8, 16)]

Created at 2023-02-22 19:55:25 EST by reprexlite v1.0.0

@CagtayFabry
Copy link
Author

awesome! 🚀
these configuration options look great! thank you for the ping @jayqi

@richieadler
Copy link

@jayqi Any chance of a release in the near future?

@jayqi
Copy link
Owner

jayqi commented Dec 10, 2024

Hi @richieadler. To be transparent, this project has been a bit of a victim of a big, ambitious overhaul that never fully got finished. With the holidays coming up, I'll try to find some free time to revisit this. I haven't worked on this in nearly two years, so realistically that's a pretty big hurdle to cross.

@richieadler
Copy link

I haven't worked on this in nearly two years, so realistically that's a pretty big hurdle to cross.

@jayqi Maybe not even the doctest generation, but the current main works with the parameters you indicated; maybe that alone may warrant a minor bump to the version and a release in PyPI?

@jayqi
Copy link
Owner

jayqi commented Feb 12, 2025

Hi @richieadler,

I have ended up working on this a fair amount over the last month and have made good progress.

For now, I've released the version that's been on main as v1.0.0a1, since the eventual final 1.0.0 will be quite different. This means you can install it from PyPI now (and later) if you specify that version, e.g., pip install reprexlite==1.0.0a1.

There are quite a few further improvements coming in #79 (configuration files!) which will be the final 1.0.0 release.

Thanks for your patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants