Recently, we rolled out our CLI, library, and configuration code to the Python Package Index, PyPI. One of the first things we noticed was that our perfectly sensible Github-flavored Markdown README.md file had been transformed into a barely-legible mess:
The setup.py file is a python program, and we were leveraging this to pull the README.md file and drop it in the long_description field when building our distribution files. A bit of research found that the correct format for PyPI is actually reStructured Text. I couldn't reasonably re-write all of our READMEs and ask the whole team to change to a new format; something else must be done.
Trawling the net, I found a solution in Pandoc, a universal markup translator. This has the magical property of being able to translate a Markdown formatted file into reST format, and more importantly was available in pip as a universal Python library. I figured the best solution would be to import this module into the setup.py file. To follow the existing pattern, I made a function around the convert statement, but I chose to name the function rather than using an un-named lambda function, per our PEP style guide.
We always want to code for maintainability, and since I can't predict the conditions under which this open source package would be built, it is appropriate for me to make arrangements for future users to have a safe experience. To ensure this, I handled exceptions from failure to import pypandoc by substituting a function with the same name, but the previous load-only behavior.
Once I had my diff (similar to a Pull Request) approved in our Phabricator and the next release came out, I was able to verify my work. Our team is still able to use familiar Markdown syntax to amend the README, and our output is compliant with the expectations of PyPI and looks great!