Newbie Alert: Help me store constants pythonically

Steven D'Aprano steve at REMOVEMEcyber.com.au
Sun Nov 6 23:52:56 EST 2005


Brendan wrote:

> Hi all
> 
> I'm new to Python (and programming in general), and I can't decide what
> is the most 'pythonic' way to approach a problem.  Your advice would be
> appreciated.
> 
> I have a bunch of 'scans', containing the data measured from one of
> several types of 'model'. Each 'model' has different values for a list
> of constants  I want to create 'analyses' which interpret the data from
> the scans, with reference the appropriate model.  So far, I have come
> up with three strategies to store this information, illustrated below
> (I've simplified the constant list to just two):
> 
> 1) Store the model constants as class instances:
> 
> class Model:
>     def __init__(self, numBumps, sizeOfBumps):
>         self.__dict__.update(locals()); del self.self
> 
> MODEL1 = Model(1, 2)
> MODEL2 = Model(3, 4)
> #etc

My vote would go with this one, as it is the closest to 
a C-style struct or a Pascal record. If you have to 
change the field list, you only need to change it in 
one place, the class.



> 2) Store the model constants as class variables:
> 
> class MODEL1:
>     numBumps = 1
>     sizeOfBumps = 2
> 
> class MODEL2:
>     numBumps = 3
>     sizeOfBumps = 4

Duplicated data... yuck. Same with method 3.

> There may be more options, but at this point I became paralyzed with
> choice.  I worry about getting stuck with an unworkable structure since
> I don't have the experience to decide the merits of each in advance.  I
> get way too frustrated about these things :)

:-)

If all the constants have the same field structure, 
stick with method 1.

In a later post, you wrote:


 > The reason I'm using that method for assigning
 > instance attributes is that the argument list for
 > __init__ is LOOONG. (There are many constants, I 
only > gave two for the examples).  I wanted to avoid 
typing > them out twice.

You could always copy-and-paste...

How many is LOOONG? Ten? Twenty? One hundred?

If it is closer to 100 than to 10, I would suggest 
putting your constants into something like an INI file:

[MODEL1]  # or something more meaningful
numBumps: 1
sizeOfBumps: 99

[MODEL2]
numBumps: 57
sizeOfBumps: 245


etc.

Then sub-class the ConfigParser to create struct-like 
objects from an ini file. Something vaguely like:

# warning: pseudo-code, seriously not tested!!!
class Config2Consts(ConfigParser):
     """Reads an INI file, and creates a struct-like
     object from each section, storing that object
     in the local scope with the name of the section.
     """

     class Struct:
         def __init__(self):
             pass

     def make_single_constant_object(self, sectionname):
         obj = Struct()
         for field in INI_FILE[sectionname]:
              obj.__dict__[field.name] = field.value
         locals().update({sectionname: obj})



If you are likely to be changing the constants (either 
names, field names, or values) an hour or three 
developing this code will save you a lot of heart-ache 
later.



-- 
Steven.




More information about the Python-list mailing list