Editing Configuration Files For Python

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
This is an article started by Russ Hensel, see "http://www.opencircuits.com/index.php?title=Russ_hensel#About My Articles" About My Articles for a bit of info.
+
This is an article started by Russ Hensel, see "http://www.opencircuits.com/index.php?title=Russ_hensel#About My Articles" About My Articles for a bit of info. The page is only partly finished.
 
 
This page discusses both the general virtues of using a parameter file written in Python, and the particular implementation, including some details I use in my applications.
 
 
 
  
 
= Why Configuration Files =
 
= Why Configuration Files =
Line 21: Line 18:
 
== How: The Basics ==
 
== How: The Basics ==
  
No matter what the application I put the configuration in a file called parameters.py and use it to define/create a class called Parameters.  It is full of instance variables like self.logging_id = "MyLoggingId".  Any part of my system that wants to know a parameter value takes the instance of Parameters created at startup and accesses its instance value this:  '''logging_id = system_parameter.logging_id'''.  It is very easy.
+
No matter what the application I put the configuration in a file called parameters.py and use it to define/create a class called Parameters.  It is full of instance variables like self.logging_id = "MyLoggingId".  Any part of my system that wants to know a parameter value takes the instance of Parameters created at startup and accesses its instance value   logging_id = system_parameter.logging_id.  It is very easy.
  
You may ask how does some part of the system get the instance of of parameters?  The best way is probably through a global singleton.  It is more or less what I do.  There seem to be a host of methods of implementing singletons.  I use a little recommended one but one that I find more than adequate:  I define a class and make the global variables be variables of the class, not of the instance.  You can get access to the class just by importing it, creating an instance servers no particular purpose and I never do it.  So the global class, AppGlobal, is defined something like this ( in a file app_global.py )
+
You may ask how does that part of the system get the instance of of parameters?  The best way is probably through a global singleton.  It is more or less what I do.  There seem to be a host of methods of implementing singletons.  I use a little recommended one but one that I find more than adequate:  I define a class and make the global variables be variables of the class not the instance.  You can get access to the class just by importing it, creating an instance servers no particular purpose.  So the global class, AppGlobal, is defined something like this ( in a file app_global.py )
  
 
<pre>
 
<pre>
Line 64: Line 61:
  
 
If you are asking why Parameters is not all defined at a Class level instead of instance level it is because I did not think of it then, I am now but have not changed the code so far ( requires more thought ).
 
If you are asking why Parameters is not all defined at a Class level instead of instance level it is because I did not think of it then, I am now but have not changed the code so far ( requires more thought ).
 
== How: New Users Guide ==
 
 
This section of this page is for a new user to the parameters as used in my applications.  Later you can read the documentation below for more advanced users.  For new users there are two parts of parameters.py that you should probably read.  The method parameters.new_user_mode() is normally where you should put your personal settings.  This alone is not sufficient, because you need to know what settings to include and how to choose values.  For this refer to the method parameters.default_mode, where a default is set for all values ( I hope ) and comments document the settings.  Also refer to the written documentation for each application.
 
 
Remember that the parameter.py file has to be legal Python.
 
 
  
 
== How: More Advanced ==
 
== How: More Advanced ==
Line 149: Line 139:
  
 
At some later point I may not remember well which grouping I used so I often give them names as in self.connect = "Remote" in the example.
 
At some later point I may not remember well which grouping I used so I often give them names as in self.connect = "Remote" in the example.
 
 
=== Modes ===
 
A feature that has emerged over time is that of "Modes".  Modes are simply a grouping of parameter values for some use of the application.  The smart terminal, for example, has a mode for working with a particular arduino program.  Say the arduino program is for monitoring a green house.  Then I might create a mode called "Green House".  This has become important enough that I display the mode in the title bar of the application. 
 
 
 
==== Default Mode ====
 
There is one mode that is always set/called.  This is the default mode.  Typically it sets all parameters and has a comment to document the parameter.  After it is called, you normally call the method for a mode of your own where you change the mode name and change any values from the default mode that you need to.  If you just run with the default mode the program should basically run, but some function may misbehave, and you may not find the configuration much to your liking.  If you eliminate the default mode the undefined values will probably cause an exception and crash the program.
 
 
You are encouraged to create new mode method, following the pattern of the ones shipped, and use them rather than changing mode values from the value as shipped.
 
 
 
A special mode like method is plus_test_mode.  It is designed for you to make short term additions to another mode for testing, instead of changing the mode name it simply adds " + test" to the mode, so "Green House" would become "Green House + test".  Again this shows in the application title bar.
 
 
==== Choose Mode ====
 
Modes have become so important for me that the first method in parameters is called choose_mode.  It then lists all the mode methods ( except default which is called earlier than choose_mode ) and the plus_test_mode with all but the mode you are using commented out.  Then just un-comment the mode you want and make sure the unwanted modes are commented out.
 
Un-comment '''plus_test_mode''' if you want to run a test.
 
 
==== Running On ====
 
Since the program may call the OS for operations such as text editing you may want to tweak settings based on the OS.  To do this there is a method '''running_on_tweaks''',  it is called after the default mode and tweaks the values for the environment ( including the computer name or tcpip address ).  Information about what you are running on is collected by parameters.running_on, so the value parameters.running_on.os_is_win will be True if you are running on Windows.  To see what values are set see running_on.py.  Change running_on_tweaks() to suit your situation.  In it you may find some tweaks for the computers I use ( by name ), you can leave them in or delete them, since it is unlikely that you will have computers of the same name.
 
  
 
=== Conditionally Assign Values ===
 
=== Conditionally Assign Values ===
Line 192: Line 162:
 
=== My Overall Structure ===
 
=== My Overall Structure ===
  
Check the __init__ method to see
+
* Meet the syntactic requirements for class creation.
 
 
* Meet the syntactic requirements for class instance creation.
 
 
* Assign instance to AppGlobal
 
* Assign instance to AppGlobal
* Call a subroutine that defaults all value as best it can for typical situations.
+
* Call a subroutine that defaults all value as best it can ( including getting OS and computer name )
* Call a subroutine that tweaks values according to the environment the program is running on.
+
* Call a subroutine that tweaks values according to OS
* Call a subroutine at give a value to mode and sets the mode of operation that has the name self.mode.  Typically called from choose_mode()
+
* Call a subroutine that tweaks values according to computer name
 +
* Call a subroutine at give a value to mode and sets the mode of operation that has the name self.mode
 
* Done  
 
* Done  
  
Code discipline is such that other code never modifies these values again ( except for some cute little monkey patching that I currently do not use and do not want to explain ).
+
Code discipline is such that other code never touches these values again ( except for some cute little monkey patching that I currently do not use and do not want to explain ).
 +
 
  
 
== Why Advantages/Features ==
 
== Why Advantages/Features ==
Line 226: Line 196:
  
 
= Editing and Editors =
 
= Editing and Editors =
 
This documentation is old, you can now give a list of editors for the system to try.  More revisions coming.
 
 
 
You need a text editor suitable for .py files to manage the parameter file ( parameters.py )
 
You need a text editor suitable for .py files to manage the parameter file ( parameters.py )
 
This includes most text editors.  I particularity like:
 
This includes most text editors.  I particularity like:
Line 244: Line 211:
 
* Python cares about capitalization, use the capitalization indicated in the default files and the example code.
 
* Python cares about capitalization, use the capitalization indicated in the default files and the example code.
 
* Python also cares a lot about how lines are indented.  Do not change the indentation from the sample files, and always indent using spaces ( not tabs. most text editors will use spaces automatically for .py files, even if you use the tab key )
 
* Python also cares a lot about how lines are indented.  Do not change the indentation from the sample files, and always indent using spaces ( not tabs. most text editors will use spaces automatically for .py files, even if you use the tab key )
 +
 +
== Modes ==
 +
 +
Modes are a set of parameters you may want to use for a specialization of the application, this may be to adapt the application to a particular environment or user.  Because of this the first method in parameters.py is normally choose_mode.  It has calls to the modes in your parameter file ( except the default mode that is called prior to choose mode ).  Normally all but the mode you want are commented out.  There is also a mode at the end, plus_test_mode that is used to temporarily  modify another mode.  So once you have you system set up you will most often just switch modes using choose_mode.
  
 
= Other Links =  
 
= Other Links =  
*'''Check out link to left "What links here" This will, in part, link to projects using this type of configuration'''
+
'''*Check out link to left "What links here"'''
 
 
*'''[https://learning-python.com/cgi/showcode.py?name=pyedit-products/unzipped/textConfig.py File: pyedit-products/unzipped/textConfig.py ]''' Here is another application that uses a .py to configure.
 
  
 
*'''[https://param.pyviz.org/ Param — Param 1.9.0 documentation ]'''
 
*'''[https://param.pyviz.org/ Param — Param 1.9.0 documentation ]'''
Line 270: Line 239:
 
------------->
 
------------->
 
[[Category:Arduino/RaspberryPi]][[Category:Python]][[Category:SmartTerminal]][[Category:Python SmartPlug]] [[Category:Python Easy DB]] [[Category:Python Projects]]
 
[[Category:Arduino/RaspberryPi]][[Category:Python]][[Category:SmartTerminal]][[Category:Python SmartPlug]] [[Category:Python Easy DB]] [[Category:Python Projects]]
[[Category:ClipBoard]] [[Category:Twitter Analysis DB]]
 

Please note that all contributions to OpenCircuits may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see OpenCircuits:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)