Difference between revisions of "Programming guidelines"

From Gramps
Jump to: navigation, search
Line 17: Line 17:
 
* Docstrings. Python provides a docstrings to document classes and functions. If the class is a class used by others (such as the RelLib classes), the docstrings should follow the [http://epydoc.sourceforge.net/manual-epytext.html epydoc] format. This allows us to extract [http://gramps-project.org/api3 API] documentation. Classes that are not core reusable classes do not have to follow this format, but should be documented using docstrings.
 
* Docstrings. Python provides a docstrings to document classes and functions. If the class is a class used by others (such as the RelLib classes), the docstrings should follow the [http://epydoc.sourceforge.net/manual-epytext.html epydoc] format. This allows us to extract [http://gramps-project.org/api3 API] documentation. Classes that are not core reusable classes do not have to follow this format, but should be documented using docstrings.
 
<code>
 
<code>
  class MyClass:
+
  class MyClass:
      """
+
      """
      MyClass is a sample class.
+
      MyClass is a sample class.
      """
+
      """
 
      
 
      
      def my_function(self):
+
      def my_function(self):
          """
+
          """
          The my_function task serves no purpose whatsoever.
+
          The my_function task serves no purpose whatsoever.
          """
+
          """
          pass
+
          pass
 
</code>
 
</code>
 
* Private class functions (functions that cannot be called outside the class) should be preceded with two underscores. Protected functions (functions that can only be called by the class or derived classes) should be preceded with one underscore.
 
* Private class functions (functions that cannot be called outside the class) should be preceded with two underscores. Protected functions (functions that can only be called by the class or derived classes) should be preceded with one underscore.
 
<code>
 
<code>
  def __private_function(self):
+
  def __private_function(self):
      pass
+
      pass
 
    
 
    
  def _protected_function(self):
+
  def _protected_function(self):
      pass
+
      pass
 
</code>
 
</code>
 
* Run <code>pylint</code> on your code before checking in, and use the output to reasonably clean up the code. Note that you must run <code>pylint</code> in the <code>src</code> directory.
 
* Run <code>pylint</code> on your code before checking in, and use the output to reasonably clean up the code. Note that you must run <code>pylint</code> in the <code>src</code> directory.

Revision as of 14:49, 18 September 2007


As more and more people start editing the code, we need to establish a few guidelines to make sure the code remains maintainable.

  • Write PEP 8 compatible code. This is important to have a consistent, readable codebase.
  • Do not use tabs. Use whitespace. If you use an editor with tabs, go into the preferences, and change the tab behavior to a number of spaces. In GRAMPS we use 4 spaces for indentation.
  • Class headers. Each class should have a simple header to help mark it in the file. This is not used for documentation - it is used to help find the class when multiple classes exist in the same file.

 #------------------------------------------------------------
 #
 # MyClass
 #
 #------------------------------------------------------------

  • Docstrings. Python provides a docstrings to document classes and functions. If the class is a class used by others (such as the RelLib classes), the docstrings should follow the epydoc format. This allows us to extract API documentation. Classes that are not core reusable classes do not have to follow this format, but should be documented using docstrings.

 class MyClass:
     """
     MyClass is a sample class.
     """
    
     def my_function(self):
         """
         The my_function task serves no purpose whatsoever.
         """
         pass

  • Private class functions (functions that cannot be called outside the class) should be preceded with two underscores. Protected functions (functions that can only be called by the class or derived classes) should be preceded with one underscore.

 def __private_function(self):
     pass
  
 def _protected_function(self):
     pass

  • Run pylint on your code before checking in, and use the output to reasonably clean up the code. Note that you must run pylint in the src directory.