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. |
− | |||
− | |||
− | |||
= Why Configuration Files = | = Why Configuration Files = | ||
Line 13: | Line 10: | ||
There are a large number of formats for configuration files, some are accessed only through wizards and they may have a "secret" format. Some configuration files are not even really files but instead are entries in a data base. But most are stored in some sort of human readable text format and can be edited with a straight ahead text editor. | There are a large number of formats for configuration files, some are accessed only through wizards and they may have a "secret" format. Some configuration files are not even really files but instead are entries in a data base. But most are stored in some sort of human readable text format and can be edited with a straight ahead text editor. | ||
− | My SmartTerminal program now has over 50 different parameters that control its use in a variety of different applications. I have used a parameter file for: [[Python Control of Smart Plugs]], [[Python Smart Terminal]], [[Python Smart Terminal | + | My SmartTerminal program now has over 50 different parameters that control its use in a variety of different applications. I have used a parameter file for: [[Python Control of Smart Plugs]], [[Python Smart Terminal Graph]], and |
+ | [[Python Smart Terminal]] | ||
= Configuration in .py Files = | = Configuration in .py Files = | ||
Line 21: | Line 19: | ||
== 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 | + | 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 | + | 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 62: | ||
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: More Advanced == | == How: More Advanced == | ||
Line 97: | Line 88: | ||
</pre> | </pre> | ||
− | === Set Values | + | === Set Values from Your Computer === |
<pre> | <pre> | ||
self.computername = ( str( os.getenv( "COMPUTERNAME" ) ) ).lower() | self.computername = ( str( os.getenv( "COMPUTERNAME" ) ) ).lower() | ||
</pre> | </pre> | ||
− | |||
− | |||
=== Override Values === | === Override Values === | ||
Line 149: | Line 138: | ||
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. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== Conditionally Assign Values === | === Conditionally Assign Values === | ||
− | + | This way I can even move the parameters.py file between OS's without tweaking them: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<pre> | <pre> | ||
Line 192: | Line 154: | ||
=== My Overall Structure === | === My Overall Structure === | ||
− | + | * Meet the syntactic requirements for class creation. | |
− | |||
− | * Meet the syntactic requirements for class | ||
* Assign instance to AppGlobal | * Assign instance to AppGlobal | ||
− | * Call a subroutine that defaults all value as best it can | + | * 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 | + | * 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 | + | * 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 | + | 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 217: | Line 179: | ||
Worth some consideration, may be important to you ( but not for me in my situation ) | Worth some consideration, may be important to you ( but not for me in my situation ) | ||
− | |||
* User may find too complex, mess up. | * User may find too complex, mess up. | ||
* Since it is programmatic you can be tempted to get too clever. | * Since it is programmatic you can be tempted to get too clever. | ||
− | * May be less than secure | + | * May be less than secure |
* May not be what users are used to. | * May not be what users are used to. | ||
* Not your shops standard. | * Not your shops standard. | ||
− | * Not XML. | + | * Not XML. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
= Other Links = | = Other Links = | ||
− | |||
− | |||
− | |||
*'''[https://param.pyviz.org/ Param — Param 1.9.0 documentation ]''' | *'''[https://param.pyviz.org/ Param — Param 1.9.0 documentation ]''' | ||
Line 269: | Line 207: | ||
this cannot bee seen | this cannot bee seen | ||
-------------> | -------------> | ||
− | [[Category:Arduino/RaspberryPi]][[Category:Python]][[Category:SmartTerminal]][[Category:Python SmartPlug | + | [[Category:Arduino/RaspberryPi]][[Category:Python]][[Category:SmartTerminal]][[Category:Python SmartPlug]] |
− |