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 21: Line 21:
 
== 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>

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)