





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















Daniel Perrefort – University of Pittsburgh
Best practices are any procedure that is accepted as being the most effective either by consensus or by prescription. Practices can range from stylistic to in-depth design methodologies. "A universal convention supplies all of maintainability, clarity, consistency, and a foundation for good programming habits too." —Tim Peters on comp.lang.python, 2001- 06 - 16
“A PEP is a design document providing information to the Python community, or describing a new feature for Python or its processes or environment.” (PEP 1)
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! >>> import this
Using 4 spaces per indentation level (not tabs!) Putting two blank lines before functions and classes Limiting line lengths to: 79 characters for code 72 characters for long blocks of text It is okay to increase the line length limit up to 99 characters Your probably already familiar with…
_# !/usr/bin/env python3.
"""This is a description of the module."""_ import json import os from astropy.table import Table, vstack from my_code import utils version = 0. def my_function(): ...
Use is when comparing singletons Use is not instead of not ... is # Wrong if foo == None : do_something() # Also wrong if not foo is None : do_something() # Correct if foo is not None : do_something()
Always use a def statement instead of an assignment statement for anonymous (lambda) expressions # Wrong f = lambda x: 2 * x # Correct def double(x): return 2 * x
Booleans are already Booleans – they don’t need comparisons For sequences, (e.g., a lists), use the fact that empty sequences are false # Wrong: if my_boolean == True : do_something() # Worse: if my_boolean is True : do_something() # Still bad: if len(my_list) != 0 : do_something() # Correct for sequences and booleans if some_variable: do_something()
PEP8 inspection is built into many Integrated Development Environments (IDEs) PyCharm : https://www.jetbrains.com/pycharm/ Atom-pep8 : https://atom.io/packages/pep Command line tools for PEP 8 are also available Pylint : http://pylint.pycqa.org/ Flake8 : https://flake8.pycqa.org/ Jupyter Plugins: Python Black : https://github.com/drillan/jupyter-black If You Take Away One Thing...
Explain what each function / module / package does or is responsible for Be understandable to you when you revisit the code in 6 months Be understandable by someone new to the project (but not necessarily new to the subject matter) Be specific and to the point
Triple quotes are always used The closing quotes are on the same line as the opening quotes. The docstring is a phrase ending in a period. It prescribes the function’s effect as a command ("Do this", "Return that"), not as a description; e.g. don't write "Returns the pathname. def kos_root(): """Return the pathname of the KOS root directory.""" ...
http://google.github.io/styleguide/pyguide.html Based on the principle that docs in the code should be human readable def connect_to_next_port(minimum): """Connects to the next available port. Args: minimum: A port value greater or equal to 1024. Returns: The new minimum port. Raises: ConnectionError: If no available port is found. """
Extensive project documentation isn’t always necessary and should scale to meet your project requirements. Include a README file at minimum Describe the project goals and general approach Does not need to be super in depth For larger projects, you might document: Design choices or style guides Project notes (e.g. from papers you read) A development plan / roadmap