Difference between revisions of "My Python Coding Conventions"

From OpenCircuits
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
= Coding Conventions Etc. =
 
= Coding Conventions Etc. =
  
In reading my code it may be of some use to know what conventions I have ( tried ) to follow.  The code has been developed over quite a period of time so the standards are not uniform.  What I write here are the standards that are in quite a bit of the code and the directions that I am trying to move.  In all of the coding consistency is an important standard, I have a ways to go. I am now only coding in Python 3.6 or up. Here are some types of conventions.  Ultimately if you want to understand the code, read it.  I work hard to make it readable, so please try, you can let me know of shortcoming, but note that I am aware of the fact that it still need improvement.
+
In reading my code it may be of some use to know what conventions I have ( tried ) to follow.  The code has been developed over quite a period of time so the standards are not uniform.  What I write here are the standards that are in quite a bit of the code and the directions that I am trying to move.  In all of the coding consistency is an important standard, I have a ways to go. I am now only coding in Python 3.6 or up. Here are some types of conventions.  Ultimately if you want to understand the code, read it.  I work hard to make it readable, so please try, you can let me know of shortcoming, but note that I am aware of the fact that it still need improvement.  You also might be interested in [[Russ Python Tips and Techniques]].
  
 
== Names ==
 
== Names ==
I try to be consistent: this is good but have not been very successful in standards: I keep changing my mind.    I am avoiding short names and try to make names descriptive enough that they are somewhat self documenting.  References are often copied across objects for easy access ( lots of parameters for example ); when this happens the name of the object is generally ( should be always ) the same in both objects.
+
I try to be consistent: this is good but have not been very successful in standards: I keep changing my mind.    I am avoiding short names and try to make names descriptive enough that they are somewhat self documenting.  References are often copied across objects for easy access ( lots of parameters for example ); when this happens the name of the object is generally ( should be always ) the same in both objects.  I am beginning to use leading underscores for private methods but just beginning this.  So far not doing the same for variables.
  
 
== Formatting ==
 
== Formatting ==
  
 
Nothing special here but I like white space and use a lot.  This is not standard Python.  But this is what I like.
 
Nothing special here but I like white space and use a lot.  This is not standard Python.  But this is what I like.
 +
 +
== Comments ==
 +
 +
Comments are used for:
 +
* Documentation
 +
* Notes about subtleties in the code or other possible non obvious behavior.
 +
* Ideas about code modifications for speed, enhancement .... !! marks an idea with high commitment ?? for one that is just an idea.
 +
* Dead code that I want for reference for at least a while.
 +
* Commented out code that might be useful for debugging: # will appear in column 1.  Not maintained so may or may not work.
 +
* As usual always be skeptical about comment content.
 +
 +
Comment in column 1 is often for debugging while other comments, indented, end of line, or triple quotes are more often documentation.
  
 
== Docstrings ==
 
== Docstrings ==
I am working towards using them but have not arrived at a format that I both like to read and which is quick enough to write.  Not good as of 2017 Jan
+
I am working towards using them but have not arrived at a format that I both like to read and which is quick enough to write.  Not good as of 2019 Jan
 +
* Function docstrings may say '''what it says''' if I think the function name is pretty much and explanation.  Especially if the function is short.
  
 
== Imports ==
 
== Imports ==
Line 41: Line 54:
 
I am moving toward using __ and _ as prefixes for more private methods, but have not gone too far in this direction.
 
I am moving toward using __ and _ as prefixes for more private methods, but have not gone too far in this direction.
  
== Application Structure ==
+
= Application Structure =
working on it..... so far:
 
 
 
  
 
=== MVC Structure ===
 
=== MVC Structure ===
Line 49: Line 60:
  
 
=== GUI Structure ===
 
=== GUI Structure ===
coming...
+
 
*Construct frame by frame in a subroutine returning the frame reference, the caller then places the frame.  Typically constructed and placed from top to bottom from left to right.
+
*Construct frame by frame in a subroutine for each frame returning the frame reference, the caller then places the frame.  Typically constructed and placed from top to bottom from left to right.
*Elements:
+
*Elements Typically Included:
 
** Buttons for application management: restart the app, edit the parameters, edit the log, help.
 
** Buttons for application management: restart the app, edit the parameters, edit the log, help.
** Buttons.... for the application.
+
** Buttons.... for the application, depending on the application.
** A text control for application messages, sort of a mini console for the application.
+
** Data Entry and Display widgets as required.
 +
** A scrolling text panel control for application messages, sort of a mini console for the application.
  
 
=== Application Parameters ===
 
=== Application Parameters ===
Line 62: Line 74:
 
=== Typical Application Functionality ===
 
=== Typical Application Functionality ===
 
Some functions seem helpful in most applications:
 
Some functions seem helpful in most applications:
 +
*Application Title: Program Name, Version, and Mode.
 +
*GUI color: often set in parameters.py so multiple instances are distinguished at a glance.
 
*Help
 
*Help
 
*Restart
 
*Restart
Line 68: Line 82:
  
 
=== Global Variables and Application State ===
 
=== Global Variables and Application State ===
Globals are often.....
+
Globals are often disparaged, in part because of strong coupling between components. Nevertheless they are very useful in applications. My approach is to use a class, never instantiated which is a singleton and which holds the global variables. This is in a file called app_global.py, check out the ( very simple ) code there.  Objects that need to be globally available typically have an __init__
 +
which contains a line much like  AppGlobal.a_global_reference = self.  It can then be accessed anywhere in the code ( after from app_global import AppGlobal ) as: a_useful_ref  = AppGlobal.a_global_reference.  Other application state information may be shared in an object of the class AppState.  Not quite sure how these differ form AppGlobals, maybe they doe not ( although I usually use an instance of this not the class itself ).  All this works fine, so I am using it this way now, but I need to think more about the proper way to code this stuff.
  
 
=== MultiThreading ===
 
=== MultiThreading ===
Line 80: Line 95:
 
I usually implement the standard python logging utility, messages into a file configured by parameters.py, the gui typically has a button to view this file.
 
I usually implement the standard python logging utility, messages into a file configured by parameters.py, the gui typically has a button to view this file.
  
== Links ==
+
= Links =
 
=== Projects Using these Conventions ===
 
=== Projects Using these Conventions ===
 
*[[Python Control of Smart Plugs]]
 
*[[Python Control of Smart Plugs]]
Line 88: Line 103:
 
*Use wiki feature "What Links Here".
 
*Use wiki feature "What Links Here".
 
*[[Configuration Files For Python]]
 
*[[Configuration Files For Python]]
 +
*Look at the categories below.
 
   
 
   
 
  
 
      
 
      
   [[Category:Python SmartPlug]][[Category:Python]]
+
   [[Category:Python]][[Category:Python SmartPlug]] [[Category:Python Scheduling Application]][[Category:SmartTerminal]]

Latest revision as of 08:09, 12 June 2020

Coding Conventions Etc.[edit]

In reading my code it may be of some use to know what conventions I have ( tried ) to follow. The code has been developed over quite a period of time so the standards are not uniform. What I write here are the standards that are in quite a bit of the code and the directions that I am trying to move. In all of the coding consistency is an important standard, I have a ways to go. I am now only coding in Python 3.6 or up. Here are some types of conventions. Ultimately if you want to understand the code, read it. I work hard to make it readable, so please try, you can let me know of shortcoming, but note that I am aware of the fact that it still need improvement. You also might be interested in Russ Python Tips and Techniques.

Names[edit]

I try to be consistent: this is good but have not been very successful in standards: I keep changing my mind. I am avoiding short names and try to make names descriptive enough that they are somewhat self documenting. References are often copied across objects for easy access ( lots of parameters for example ); when this happens the name of the object is generally ( should be always ) the same in both objects. I am beginning to use leading underscores for private methods but just beginning this. So far not doing the same for variables.

Formatting[edit]

Nothing special here but I like white space and use a lot. This is not standard Python. But this is what I like.

Comments[edit]

Comments are used for:

  • Documentation
  • Notes about subtleties in the code or other possible non obvious behavior.
  • Ideas about code modifications for speed, enhancement .... !! marks an idea with high commitment ?? for one that is just an idea.
  • Dead code that I want for reference for at least a while.
  • Commented out code that might be useful for debugging: # will appear in column 1. Not maintained so may or may not work.
  • As usual always be skeptical about comment content.

Comment in column 1 is often for debugging while other comments, indented, end of line, or triple quotes are more often documentation.

Docstrings[edit]

I am working towards using them but have not arrived at a format that I both like to read and which is quick enough to write. Not good as of 2019 Jan

  • Function docstrings may say what it says if I think the function name is pretty much and explanation. Especially if the function is short.

Imports[edit]

  • In most cases use the format "import xyz" so the name space is not polluted and so it is easy to identify just what an imported class is.
  • In in objects that are almost all GUI then using "from Tkinter import *" is ok but better is: "import Tkinter as Tk"
  • I normally have only one or a few classes in a file so there are a lot of files and a lot of what I call "local imports".
  • Almost all imports are at the top of a file, std library imports first then "local imports".

Object Orientation[edit]

Almost everything is a class. Not much in the way of module functions, not many classes in a module. I think my Java experience has led me to overuse classes and under use functions.... at the module level.

Documentation for Class Instance Methods[edit]

Look something like this:

   def create_class_from_strings( self, module_name, class_name):
       """
       This will load a class from string names
       It makes it easier to specify classes in the parameter file.
       I believe it is used for both the comm drive and the "processor"
       args:  strings
       ret:   instance of the class
       Side effects Class created 
       """

The comment should give the intent of the method, some hint as to the args ( which hopefully have good names ), and some info. on the return value. zip means nothing, void....

I am moving toward using __ and _ as prefixes for more private methods, but have not gone too far in this direction.

Application Structure[edit]

MVC Structure[edit]

I try to separate the Model, View, and Controller into separate modules and classes. The View is the class GUI. The controller is normally the main routine where the app is started.

GUI Structure[edit]

  • Construct frame by frame in a subroutine for each frame returning the frame reference, the caller then places the frame. Typically constructed and placed from top to bottom from left to right.
  • Elements Typically Included:
    • Buttons for application management: restart the app, edit the parameters, edit the log, help.
    • Buttons.... for the application, depending on the application.
    • Data Entry and Display widgets as required.
    • A scrolling text panel control for application messages, sort of a mini console for the application.

Application Parameters[edit]

see: Configuration Files For Python

Typical Application Functionality[edit]

Some functions seem helpful in most applications:

  • Application Title: Program Name, Version, and Mode.
  • GUI color: often set in parameters.py so multiple instances are distinguished at a glance.
  • Help
  • Restart
  • Edit Parameters
  • Edit Log File

Global Variables and Application State[edit]

Globals are often disparaged, in part because of strong coupling between components. Nevertheless they are very useful in applications. My approach is to use a class, never instantiated which is a singleton and which holds the global variables. This is in a file called app_global.py, check out the ( very simple ) code there. Objects that need to be globally available typically have an __init__ which contains a line much like AppGlobal.a_global_reference = self. It can then be accessed anywhere in the code ( after from app_global import AppGlobal ) as: a_useful_ref = AppGlobal.a_global_reference. Other application state information may be shared in an object of the class AppState. Not quite sure how these differ form AppGlobals, maybe they doe not ( although I usually use an instance of this not the class itself ). All this works fine, so I am using it this way now, but I need to think more about the proper way to code this stuff.

MultiThreading[edit]

In any GUI code using Tkinter if the GUI kicks off a long running piece of code the GUI becomes unresponsive while the code runs. To avoid this you can use a second thread. This is frequently what I do. I call the two threads the gui thread, and the helper thread. To coordinate the two I have two queues, one from gui thread --> helper thread and from helper thread --> gui thread. I try to isolate the code for each thread into its own modules and classes, and comment the code as to the required thread in cases where I think there might be doubt.

Python Logging[edit]

I usually implement the standard python logging utility, messages into a file configured by parameters.py, the gui typically has a button to view this file.

Links[edit]

Projects Using these Conventions[edit]

Other Links[edit]