Configuration Files For Python

From OpenCircuits
Revision as of 12:18, 11 February 2018 by Russ hensel (talk | contribs) (→‎How)
Jump to navigation Jump to search

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

Most larger program should have configuration files:

  • Program becomes more flexible without reprogramming.
  • Users can inject their own preferences.
  • The environment changes, requiring that the program adapt.

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.

Configuration in .py Files

I have decided for my use that configuring everything in a single python, .py, file is the best solution for me ( and I think for many of you ) I will describe how I do it and then will give some of the reasons why I think the method is so very useful.

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 logging_id = system_parameter.logging_id. It is very easy.

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 )

class AppGlobal( object ):

    controller              = None
    parameters              = None
    

This AppGlobal object has only 2 variables, both undefined = None initially.

Then parameters.py looks something like this:

from app_global import AppGlobal

class Parameters( object ):
    
    def __init__(self,  ):

        AppGlobal.parameters    = self
        self.logging_id         = "MyLoggingId"
        self.timeout_sec        = 3
        ............

A class instance that needs to use a parameter uses code like:


from app_global import AppGlobal

    .............

    timeout     = AppGlobal.parameters.timeout_sec
    ............

If you are asking why Parameters is not all defined at a Class 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.

How More Advanced

Why Not

Other Links

code