Python Style Guide and Documentation Best Practices

style n.w
1 / 13
Embed
Share

Learn about Python style conventions outlined in PEP8, the importance of docstrings for code documentation, utilizing PyDoc for generating documentation, and exploring alternative documentation tools like Sphinx. Discover how to structure your code for readability and maintainability.

  • Python Development
  • Code Style
  • Documentation Best Practices
  • Sphinx
  • PEP8

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Style Generally style is built into Python, as it demands indents. However, good Python style is set out in PEP8: https://www.python.org/dev/peps/pep-0008/

  2. Style: main elements Use 4 spaces per indent, rather than tabs. Use blank lines before function defs, and to separate logical code units. function_names; variable_names; keyword_named_variables_; ClassNames; modname; CONSTANT_NAMES Keep lines to 79 characters or less. aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaa Spaces around operators and after commas, but not just inside (){}[]: a = (1, 2) Indent comments to the level of the code referred to.

  3. Documentation Docstrings are automatically extracted to describe your code. They are triple-double quote enclosed comments after, for example, function declarations. For some peculiar reason, they are written as commands not descriptions def add(num1, num2): """Add two numbers."""

  4. Example def add (num1, num2): """ Add two numbers. Keyword arguments: num1 -- an integer or double number (no default) num2 -- an integer or double number (no default) Returns: The sum of the numbers. """ return num1 + num2 Style details: https://www.python.org/dev/peps/pep-0257/

  5. PyDoc PyDoc is the documentation system distributed with Python. Best way to invoke it is to import any code, and then type: >>> help(x) Where "x" is a function, module, dotted object method etc. If you want to see docstrings, then: print(function_name.__doc__)

  6. PyDoc To generate a webpage from documentation, at command prompt: pydoc -w filename (note without the .py) For more, see: https://docs.python.org/3/library/pydoc.html

  7. Other software There is a starter list of alternative software at: https://wiki.python.org/moin/DocumentationTools A popular one is Sphinx, which comes with Anaconda: http://www.sphinx-doc.org/en/stable/ http://www.sphinx-doc.org/en/stable/tutorial.html http://www.sphinx-doc.org/en/stable/invocation.html#invocation- apidoc

  8. DocTest DocTest runs short tests built into your documentation. """ Add two numbers together. >>> add(1,2) 3 """ def add (num1, num2): return num1 + num2

  9. DocTest To run: python -m doctest -v filename.py Or: >>> import doctest >>> import filename >>> doctest.testmod(filename, verbose=True) See: https://docs.python.org/3/library/doctest.html

  10. Unit tests Write the tests first, defining success. Then write the code that satisfies the tests. For example: #docs.py def add(num1, num2): return num1 + num2 ----------------------------------------- import docs def test_add(self): self.assertEqual(docs.add(1,2), 3) See: https://docs.python.org/3/library/unittest.html For a list of assertion functions: https://docs.python.org/3/library/unittest.html#assert-methods

  11. Write a test class # test.py import unittest import docs class TestDocs(unittest.TestCase): def test_add(self): self.assertEqual(docs.add(1,2), 3) if __name__ == '__main__': unittest.main()

  12. Write a test class Run it: python tests.py Or verbose: python -m unittest -v tests.py In the latter case you can delete the: if __name__ == '__main__': unittest.main()

  13. Test Driven Development Write the tests first, and then write the software to match the tests. Good for working in teams: if the code matches the test, it shouldn't matter how it does it. All team code uploaded is tested in a continuous integration process to make sure it works before integration. https://en.wikipedia.org/wiki/Test-driven_development https://en.wikipedia.org/wiki/Continuous_integration

More Related Content