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

Fix code formatting in docstrings for a few functions #6848

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bertiewooster
Copy link
Contributor

@bertiewooster bertiewooster commented Oct 28, 2023

Reference Issue

What does this implement/fix? Explain your changes.

Format code in docstrings as code blocks by adding >>> before lines of code

Any other comments?

I noticed this issue in the documentation for the MolsMatrixToGridImage code I submitted (scroll down to EXAMPLES), then discovered that other docstrings had similar issues, so I addressed the couple that I found.

Copy link
Member

@greglandrum greglandrum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the changes to your docs make them considerably less useful.
As is, you can copy examples from the doc string and paste them directly jnto code.
After this change you'd have to edit out the prompts.

I believe that the prompts are almost only useful when they are present as part of doctests: showing both input and output

@bertiewooster
Copy link
Contributor Author

bertiewooster commented Oct 29, 2023

In their current state, the code blocks in that documentation page are not presented as code blocks, but as run-on paragraphs:

Unformatted code in MolsMatrixToGridImage documentation

I proposed using >>> because the code blocks displayed as such, for example for GetAtomPairFingerprintAsBitVect:

Formatted code in GetAtomPairFingerprintAsBitVect documentation selected

are formatted with >>> :

  >>> from rdkit import Chem
  >>> m = Chem.MolFromSmiles('CCC')
  >>> v = [pyScorePair(m.GetAtomWithIdx(0), m.GetAtomWithIdx(1), 1),
  ...      pyScorePair(m.GetAtomWithIdx(0), m.GetAtomWithIdx(2), 2),
  ...     ]
  >>> v.sort()
  >>> fp = GetAtomPairFingerprintAsBitVect(m)
  >>> list(fp.GetOnBits()) == v
  True

And as shown in the documentation screenshot above, when you select the code with the mouse the >>> are excluded (they are not within the blue highlighting). I verified that the >>> are excluded when you then copy and paste the selected code.

If the preference is to format code blocks that are not doctest-like (showing both input and output) without using >>> so those characters are not included when copying directly from the source code files, perhaps triple backticks ``` around code blocks would work to format them as code blocks on the documentation pages? I searched for triple backticks ``` and didn't find any in the RDKit codebase that produce documentation pages, so I can't cite any example of that working, but it's the usual way to format code blocks.

P.S. In the code block above, GitHub adds a Copy button Copy code button on GitHub which we might also want to add to the RDKit documentation to facilitate copying blocks of code. I believe the sphinx-copybutton extension achieves that. I can take that on if it's something we want to add.

@greglandrum
Copy link
Member

In their current state, the code blocks in that documentation page are not presented as code blocks, but as run-on paragraphs:

The solution to this is apparently to end the line above with two colons: https://stackoverflow.com/a/64452858

I tested this with a couple of doc strings and that seems to work.

@bertiewooster
Copy link
Contributor Author

Thanks for researching that. I'll try using two colons, ideally after Documentation preview builds on CI #6849 is in place so I can check that I formatted the code blocks correctly--an easy mistake to make is to format a first code block correctly but not a second code block, for example in ExplainPairScore.
ExplainPairScore second code block formatted incorrectly

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

Successfully merging this pull request may close these issues.

None yet

2 participants