Difference between revisions of "Python Smart Terminal Technical"

From OpenCircuits
Jump to navigation Jump to search
(31 intermediate revisions by the same user not shown)
Line 11: Line 11:
 
The view component is called GUI ( in gui.py ). It creates all the visible components, and relays user input to the controller.
 
The view component is called GUI ( in gui.py ). It creates all the visible components, and relays user input to the controller.
  
The model component is the component that actually does the communication it is called RS232Driver ( in rs232driver.py ) and like the GUI is controlled by the controller.
+
The model component is the component that actually does the communication it is called RS232Driver ( in rs232driver.py ) and like the GUI it is controlled by the controller.
  
 
The GUI is not allowed to directly communicate with the model and vise versa. Thus you can unplug them from the application and plug in new components. Don't like the GUI? You could modify mine, or you could make a modification and choose which one to use. This is sort of like a skin for an application. You can even set up to run with no GUI at all. The RS232Driver like the GUI easy to remove and replace in the program, its use has been parameterized in to the Parameter object, so to use SPI instead of RS232 all we have to do is write an SPI object and change the values in Parameter.
 
The GUI is not allowed to directly communicate with the model and vise versa. Thus you can unplug them from the application and plug in new components. Don't like the GUI? You could modify mine, or you could make a modification and choose which one to use. This is sort of like a skin for an application. You can even set up to run with no GUI at all. The RS232Driver like the GUI easy to remove and replace in the program, its use has been parameterized in to the Parameter object, so to use SPI instead of RS232 all we have to do is write an SPI object and change the values in Parameter.
Line 17: Line 17:
 
Two other important components are called Logger ( in logger.py ) and Parameters ( in parameters.py ). The controller creates one of each, and make them available to the other components. The other components can interact with them, and uses them respectively for logging events, and getting access to parameters ( those aspects of the application that are particularly easy to change ).
 
Two other important components are called Logger ( in logger.py ) and Parameters ( in parameters.py ). The controller creates one of each, and make them available to the other components. The other components can interact with them, and uses them respectively for logging events, and getting access to parameters ( those aspects of the application that are particularly easy to change ).
  
The application has a main thread running in a Tkinter mainloop.  There is also a second thread called a "helper" running which makes some processing much easier.  To make THE main responsive to both the GUI and its own processing it uses a pseudo event loop or a polling subroutine that is implemented in SmartTerminal.polling().  This is where data is received from there comm port. The frequency which it is called is set in parameters, the relatively low rate of 100 ms between calls ( .1 sec ) seems to give a perfectly responsive application in most cases.  I have run it as fast as once every 10 ms.  Have not tried to find a limit.  The second thread is mostly intended for your custom processing, see the section Processing below.
+
The application has a main thread running in a Tkinter mainloop.  There is also a second thread called a "helper" running which makes some processing much easier.  To make gui mainloop responsive to both the GUI and its own processing it uses a pseudo event loop or a polling subroutine that is implemented in SmartTerminal.polling().  This is where data is received from there comm port ( or sometimes the responsibility is passed to the helper thread). The frequency which it is called is set in parameters, the relatively low rate of 100 ms between calls ( .1 sec ) seems to give a perfectly responsive application in most cases.  I have run it as fast as once every 10 ms.  Have not tried to find a limit.  The second thread is mostly intended for your custom processing, see the section Processing below.
  
= Components =
+
= Components and Functions =
  
*Tkinter for the GUI
+
* Tkinter for the GUI
*pySerial for serial communications
+
* pySerial for serial communications
*pyLogging for logging
+
* pyLogging for logging
......
+
* SmartTerminal a model-view-controller controller with some modifications  ( '''[https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model–view–controller - Wikipedia ]'''  or perhaps closer to '''[https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter Model–view–presenter - Wikipedia ]''' )
 +
* Second thread for processing without being blocked by/blocking the GUI
 +
* Global Singleton for app cohesion.  
  
= Coding Conventions Etc. =
+
== The Controller: SmartTerminal ==
 +
 
 +
SmartTerminal ( in smart_terminal .py )
 +
 
 +
This is the class you create to run the application see the code at the bottom of the file, just run the file.  Similar code is at the bottom of some of the other source files to make it convenient to run from those files, this is just to make it easier in development, the code in smart_terminal is the model for what should be used.  Sometimes the code at the bottom of the file may have code for testing objects in the file, it may not be maintained, may not work as intended.
 +
 
 +
The SmartTerminal.__init__ method is the initialization and "run" method for the application.  Much of the code that would normally be in it has been moved to SmartTerminal.restart which is used to restart the application when just the parameters have been changed.  See the docstring there.
 +
 
 +
== Global Values ==
 +
 
 +
Yes I know that globals are bad, but they can be useful.  For example many class instances need to access the parameter file.  This can be done using the singleton class AppGlobal.
 +
It has values at the class level ( not instance ) that are available simply by importing the class.  Most values are defaulted to None, and are set to valid values as the application initializes.
 +
 
 +
== Threads ==
  
In reading the 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 uniformWhat 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.  Here are some areas.
+
The application runs two threads, one for the GUI and one where processing can occur ( HelperThread ) with out locking up the GUIThere are 2 queues that allow the threads to communicate.
  
== Names ==
+
=== The Tkinker Thread ===
Be consistent... I have an open office doc for this should be included in download.
 
  
== Formatting ==
+
Once Tkinker is started it runs its own mainloopIn order to receive however we need to check the rs232 port from time to time.  This is done in SmartTerminal.polling()  I call this thread the GUI thread or gt.  The terminal is configured to have a second thread called the Helper Thread or ht.  In some cases the receiving of data is passed to the Helper Thread so where data is processed in addition to being posted to the GUI. See the section HelperThread for more info.
=== White Space ===
 
== Docstrings ==
 
Work towards using themNot good as of 2017 Jan
 
  
== Imports ==
+
=== HelperThread ===
*In most cases use the format "import xyz" so the name space is not poluted 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 is 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 ==
+
HelperThread in smart_terminal_helper.py  This class provides the support for a second thread of execution that does not block the main thread being run by Tinker.  I call the two threads the GUI Thread (gt) and the Helper Thread ( ht ).  It can get confusing keeping track of which method is running in which thread, I sometimes annotate them with gt and ht.  The helper thread is started by running HelperThread.run() which pretty much just runs a polling task in HelperThread.polling().  HelperThread.polling() is an infinite loop, it uses sleep to set the polling rate.  When used with the green house processing module, it may call a function there that is its own infinite loop.  There are a lot of details here, I should write some more about it.
  
Almost everything is a class.  Not much in the way of module functions, not many classes in a module.  I am trying to have all my classes descend from something be it only Object.
+
== Parameters ==
  
== Class Methods ==
+
Parameters ( in parameters.py ) this is pretty much a structure ( that is all instance variables ) that is instantiated early in the life of the application.  It passes values, strings, numbers, objects around the application to other parts of the application that need them.  Much of the appearance and behavior of the application is controlled here.
  
Look something like this:
+
The standard gui has a button to kick off editing of this file, the application may then be restarted ( another button ) with the new values.
  
    def create_class_from_strings( self, module_name, class_name):
+
There are a couple of meta parameters, the most important of which is mode which is then used in conditionals later in parameters.  Except for this sort of thing there is really not much "code" in parameters. You can change this code pretty much as much as you like, as long as you end up setting up values for the required parameters.
        """
 
        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
 
        """
 
  
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....
+
The code is extensively commented: use that for documentation.
  
I am moving toward using __ and _ as prefixes for more private methods, but have not gone too far in this direction.
+
Values are often set to None as a default, then later set to some other value.  Or the value may be set several times in a row ( this is an artifact of messing with the values ); only the last value set has any meaning.
  
= The Controller: SmartTerminal =  
+
If asked for in the command line you can also envoke a second parameter file. This is handy if you want two different instances of the terminal.
  
SmartTerminal ( in smart_terminal .py )
+
For a lot more info see:  [[Smart Terminal Parameter Examples]]
  
This is the class you create to run the application see the code at the bottom of the file, just run the file.  Similar code is at the bottom of some of the other source files to make it convenient to run from those files, this is just to make it easier in development, the code in smart_terminal is the model for what should be used.  Sometimes the code at the bottom of the file may have code for testing objects in the file, it may not be maintained, may not work as intended.
+
== Processing Modules==
  
The SmartTerminal.__init__ method is the initialization and "run" method for the applicationMuch of the code that would normally be in it has been moved to SmartTerminal.restart which is used to restart the application when just the parameters have been changed.  See the docstring there.
+
So called processing modules offer you the opportunity to put some custom automatic processing in your terminal. Typically this is matched up with a custom microcontroller application on the other end of the com port.  These are all implemented as descandants of the class ABCProcessing( so far ABCProcessing does not add much value ) A couple of examples of processing modules and the arduino code are included in the download package, but beware they are in various states of maintenance.  See: [[Writing You Own Extensions to SmartTerminal]]
  
= Parameters =
+
== Microcontroller and Its Protocol ==
  
Parameters ( in parameters.py ) this is pretty much a structure ( that is all instance variables ) that is instantiated early in the life of the application.  It passes values, strings, numbers, objects around the application to other parts of the application that needs themMuch of the appearance and behavior of the application is controlled here.
+
All my microcontroller projects use a simple protocol where commands are sent from the terminal and microcontroller responds, all commands and responses are in human readable encoded strings.  If we take the greeenhouse monitor code here are some of the most important commands:
  
The standard gui has a button to kick off editing of this file, the application may then be restarted ( another button ) with the new values.
+
*  v<cr>        microcontroller responds with the name and version of its software
 +
a<cr>        microcontroller aquires data and responds "ok"
 +
*  t<cr>        microcontroller responds with the aquired temperatures ex: 88.2 56.9
  
There are a couple of meta parameters, the most important of which is mode which is then used in conditionals later in parameters.  Except for this sort of thing there is really no "code" in parameters.  You can change this code pretty much as much as you like, as long as you end up setting up values for the required parameters.
+
See [[GreenHouse Monitor Program]]
  
The code is extensively commented use that for your documentation.
 
  
Values are often set to None as a default, then later set to some other value. Or the value may be set several times in a row ( this is an artifact of messing with the values ); only the last value set has any meaning.
+
GHProcessing( in gh_processing.py )
  
If asked for in the command line you can also envoke a second parameter fileThis is handy
+
== Polling ==
 +
Both threads have method that perform polling for events, events like received text, or items in their queue that may have been sent from the other threadMore info in [[Python Smart Terminal Technical Details]]
  
= Processing =   
+
== Logging ==
 +
This uses the standard Python logging class. Logging level and other logging details are set up using the parameter file.
  
So called processing modules offer you the opportunity to put some custom automatic processing in your terminal.  Typically this is matched up with a custom microcontroller application on the other end of the com port.  These are all implemented as descandants of the class  .  A couple of examples of processing modules and the arduino code are included in the download package.
+
== Other Classes ==
 +
For now the documentation, as far as it exists is in the source code.
  
== Microcontroller and Its Protocol ==
+
= Coding Conventions Etc. =
  
All my microcontroller projects use a simple protocol where commands are sent from the terminal and microcontroller responds, all commands and responses are in human readable encoded stringsIf we take the greeenhouse monitor code here are some of the most important commands:
+
In reading the 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 moveIn all of the coding consistency is an important standard.  Here are some areas.
  
v<cr>        microcontroller responds with the name and version of its software
+
== Names ==
a<cr>        microcontroller aquires data and responds "ok"
+
Be consistent: this is good but have not been very successful in standards: I keep changing my mind.  Names across classes are pretty consistent.  I am avoiding short names and try to make them 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.
t<cr>        microcontroller responds with the aquired temperatures ex:
 
  
 +
== Formatting ==
  
[[GreenHouse Monitor]]
+
Nothing special here but I like white space and use a lot.  This is not standard Python.  But this is what I like.
  
 +
== Docstrings ==
 +
Work towards using them.  Not good as of 2017 Jan
  
GHProcessing( in gh_processing.py )
+
== Imports ==
 +
*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 is 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".
  
= HelperThread =
+
== Object Orientation ==
  
 +
Almost everything is a class.  Not much in the way of module functions, not many classes in a module.  I am trying to have all my classes descend from something be it only Object.  And now that I am in Python 3.6 this is how it always works.
  
HelperThread in smart_terminal_helper.py
+
== Documentation for Class Instance Methods ==
  
 +
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
 +
        """
  
= Other Classes =
+
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.
  
  
 
* RS232Driver
 
* RS232Driver
  
 +
= See Also =
  
 +
*[[Python Smart Terminal Technical Details]] More detailed technical issues.
 +
*[[Smart Terminal GUI]] how the gui works and interfaces the user.
 +
*[[Smart Terminal Convert from 2.7 to 3.6]] converting a moderate size application.
 +
* and the categories below may be useful ( click on them ).
  
[[Category:SmartTerminal]] [[Category:Arduino/RaspberryPi]]
+
[[Category:SmartTerminal]] [[Category:Arduino/RaspberryPi]][[category:Python]]

Revision as of 08:52, 24 September 2018

Python Smart Terminal Technical

Overview

These notes are here so you can more easily modify the code. Contact me Russ Hensel if you need additional help.

Before modifying the code it is best to understand how it works. Here is an overview of the general plan, details can be filled out by reading the code.

The architecture is called the model view controller or MVC. The class SmartTerminal ( in smart_terminal.py ) could be viewed as the main class. To run the program run its file ( see code at end of file) SmartTerminal is the controller in MVC it is responsible for all overall control, it directly or indirectly creates all other program objects.

The view component is called GUI ( in gui.py ). It creates all the visible components, and relays user input to the controller.

The model component is the component that actually does the communication it is called RS232Driver ( in rs232driver.py ) and like the GUI it is controlled by the controller.

The GUI is not allowed to directly communicate with the model and vise versa. Thus you can unplug them from the application and plug in new components. Don't like the GUI? You could modify mine, or you could make a modification and choose which one to use. This is sort of like a skin for an application. You can even set up to run with no GUI at all. The RS232Driver like the GUI easy to remove and replace in the program, its use has been parameterized in to the Parameter object, so to use SPI instead of RS232 all we have to do is write an SPI object and change the values in Parameter.

Two other important components are called Logger ( in logger.py ) and Parameters ( in parameters.py ). The controller creates one of each, and make them available to the other components. The other components can interact with them, and uses them respectively for logging events, and getting access to parameters ( those aspects of the application that are particularly easy to change ).

The application has a main thread running in a Tkinter mainloop. There is also a second thread called a "helper" running which makes some processing much easier. To make gui mainloop responsive to both the GUI and its own processing it uses a pseudo event loop or a polling subroutine that is implemented in SmartTerminal.polling(). This is where data is received from there comm port ( or sometimes the responsibility is passed to the helper thread). The frequency which it is called is set in parameters, the relatively low rate of 100 ms between calls ( .1 sec ) seems to give a perfectly responsive application in most cases. I have run it as fast as once every 10 ms. Have not tried to find a limit. The second thread is mostly intended for your custom processing, see the section Processing below.

Components and Functions

The Controller: SmartTerminal

SmartTerminal ( in smart_terminal .py )

This is the class you create to run the application see the code at the bottom of the file, just run the file. Similar code is at the bottom of some of the other source files to make it convenient to run from those files, this is just to make it easier in development, the code in smart_terminal is the model for what should be used. Sometimes the code at the bottom of the file may have code for testing objects in the file, it may not be maintained, may not work as intended.

The SmartTerminal.__init__ method is the initialization and "run" method for the application. Much of the code that would normally be in it has been moved to SmartTerminal.restart which is used to restart the application when just the parameters have been changed. See the docstring there.

Global Values

Yes I know that globals are bad, but they can be useful. For example many class instances need to access the parameter file. This can be done using the singleton class AppGlobal. It has values at the class level ( not instance ) that are available simply by importing the class. Most values are defaulted to None, and are set to valid values as the application initializes.

Threads

The application runs two threads, one for the GUI and one where processing can occur ( HelperThread ) with out locking up the GUI. There are 2 queues that allow the threads to communicate.

The Tkinker Thread

Once Tkinker is started it runs its own mainloop. In order to receive however we need to check the rs232 port from time to time. This is done in SmartTerminal.polling() I call this thread the GUI thread or gt. The terminal is configured to have a second thread called the Helper Thread or ht. In some cases the receiving of data is passed to the Helper Thread so where data is processed in addition to being posted to the GUI. See the section HelperThread for more info.

HelperThread

HelperThread in smart_terminal_helper.py This class provides the support for a second thread of execution that does not block the main thread being run by Tinker. I call the two threads the GUI Thread (gt) and the Helper Thread ( ht ). It can get confusing keeping track of which method is running in which thread, I sometimes annotate them with gt and ht. The helper thread is started by running HelperThread.run() which pretty much just runs a polling task in HelperThread.polling(). HelperThread.polling() is an infinite loop, it uses sleep to set the polling rate. When used with the green house processing module, it may call a function there that is its own infinite loop. There are a lot of details here, I should write some more about it.

Parameters

Parameters ( in parameters.py ) this is pretty much a structure ( that is all instance variables ) that is instantiated early in the life of the application. It passes values, strings, numbers, objects around the application to other parts of the application that need them. Much of the appearance and behavior of the application is controlled here.

The standard gui has a button to kick off editing of this file, the application may then be restarted ( another button ) with the new values.

There are a couple of meta parameters, the most important of which is mode which is then used in conditionals later in parameters. Except for this sort of thing there is really not much "code" in parameters. You can change this code pretty much as much as you like, as long as you end up setting up values for the required parameters.

The code is extensively commented: use that for documentation.

Values are often set to None as a default, then later set to some other value. Or the value may be set several times in a row ( this is an artifact of messing with the values ); only the last value set has any meaning.

If asked for in the command line you can also envoke a second parameter file. This is handy if you want two different instances of the terminal.

For a lot more info see: Smart Terminal Parameter Examples

Processing Modules

So called processing modules offer you the opportunity to put some custom automatic processing in your terminal. Typically this is matched up with a custom microcontroller application on the other end of the com port. These are all implemented as descandants of the class ABCProcessing. ( so far ABCProcessing does not add much value ) A couple of examples of processing modules and the arduino code are included in the download package, but beware they are in various states of maintenance. See: Writing You Own Extensions to SmartTerminal

Microcontroller and Its Protocol

All my microcontroller projects use a simple protocol where commands are sent from the terminal and microcontroller responds, all commands and responses are in human readable encoded strings. If we take the greeenhouse monitor code here are some of the most important commands:

  • v<cr> microcontroller responds with the name and version of its software
  • a<cr> microcontroller aquires data and responds "ok"
  • t<cr> microcontroller responds with the aquired temperatures ex: 88.2 56.9

See GreenHouse Monitor Program


GHProcessing( in gh_processing.py )

Polling

Both threads have method that perform polling for events, events like received text, or items in their queue that may have been sent from the other thread. More info in Python Smart Terminal Technical Details

Logging

This uses the standard Python logging class. Logging level and other logging details are set up using the parameter file.

Other Classes

For now the documentation, as far as it exists is in the source code.

Coding Conventions Etc.

In reading the 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. Here are some areas.

Names

Be consistent: this is good but have not been very successful in standards: I keep changing my mind. Names across classes are pretty consistent. I am avoiding short names and try to make them 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.

Formatting

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

Docstrings

Work towards using them. Not good as of 2017 Jan

Imports

  • 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 is 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

Almost everything is a class. Not much in the way of module functions, not many classes in a module. I am trying to have all my classes descend from something be it only Object. And now that I am in Python 3.6 this is how it always works.

Documentation for Class Instance Methods

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.


  • RS232Driver

See Also