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 = | ||
− | Most larger | + | Most larger program should have configuration files: |
* Program becomes more flexible without reprogramming. | * Program becomes more flexible without reprogramming. | ||
* Users can inject their own preferences. | * Users can inject their own preferences. | ||
* The environment changes, requiring that the program adapt. | * The environment changes, requiring that the program adapt. | ||
− | There are a large number of formats for configuration files, some are accessed only through | + | There are a large number of formats for configuration files, some are accessed only through wizzards to they can 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 | + | My SmartTerminal program now has over 50 different parameters that control its use in a variety of different applications. |
= Configuration in .py Files = | = Configuration in .py 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 | + | 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 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: More Advanced == | == How: More Advanced == | ||
Line 97: | Line 87: | ||
</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 137: | ||
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 153: | ||
=== 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 === |
* Text file based, easy to edit, works in all OS's. | * Text file based, easy to edit, works in all OS's. | ||
− | |||
* Can use programmatic features as it is in executable code. ( if overdone can be confusing ) | * Can use programmatic features as it is in executable code. ( if overdone can be confusing ) | ||
* Easy to create parameters of any type ( *.ini files, for example, typically create strings that may need conversion to be useful ) | * Easy to create parameters of any type ( *.ini files, for example, typically create strings that may need conversion to be useful ) | ||
Line 213: | Line 173: | ||
* and so many more...... really? | * and so many more...... really? | ||
− | + | = Why Not = | |
− | + | Right now I will not discuss these, they may show up in the links. I think there are answers/solutions to many, will not go into them at this time. Email me if you would like to discuss. | |
− | |||
* 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 = | ||
− | |||
− | |||
− | |||
− | |||
− | |||
*'''[[Smart Terminal Parameter Examples]]''' documents how I used this approach in the '''[[Python Smart Terminal]]''' You can also link to the full code. | *'''[[Smart Terminal Parameter Examples]]''' documents how I used this approach in the '''[[Python Smart Terminal]]''' You can also link to the full code. | ||
Line 269: | Line 203: | ||
this cannot bee seen | this cannot bee seen | ||
-------------> | -------------> | ||
− | [[Category:Arduino/RaspberryPi]][[Category:Python]][[Category:SmartTerminal | + | [[Category:Arduino/RaspberryPi]][[Category:Python]][[Category:SmartTerminal]] |
− |