Newbie Alert: Help me store constants pythonically

Ron Adam rrr at ronadam.com
Mon Nov 7 21:51:12 EST 2005



Brendan wrote:
>>How many is LOOONG? Ten? Twenty? One hundred?
> 
> 
> About 50 per Model
> 
> 
>>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

It looks to me like you may want a data file format that can be used by 
various tools if possible.

A quick search for 3d file formats finds the following list.  If any of 
these are close to what you need you may have an additional benefit of 
ready made tools for editing.

      http://astronomy.swin.edu.au/~pbourke/dataformats/


> which I'm not sure the .ini format can easily support.  I could use
> (key buzzword voice) XML, but  I fear that might send me down the
> 'overcomplicating things' path.  Your suggestion has given me some new
> places to search Google (configparser, python config files), so I'll
> look around for better ideas.
> 
>    Brendan


One approach is to just store them as Python dictionaries.  Then just 
import it and use it where it's needed.

    # models.py

    M1 = dict(
    	numBumps=1,
    	sizeOfBumps=2,
    	transversePlanes=[
         	dict(type=3, z=4),
         	dict(type=5, z=6),
         	dict(type=3, z=8) ]
         )

    M2 = ...


Then in your application...

    # makemodels.py

    import models

    class Model(object):
        def __init__( self, numBumps=None, sizOfBumps=None,
                      transversePlanes=None ):
            self.numBumps = numBumps
            self.sizeOfBumps = sizeOfBumps
            self.transversePlanes = []
            for p in tranversePlanes:
                self.transversePlanes.append(Plane(**p))
	
    mod1 = Model(**models.M1)


This may be good to use until you decide how else to do it.  You can 
easily write the dictionaries to a text file in the chosen format later 
and that will tell you what you need to do to read the file back into 
the dictionaries as it will just be reversed.

Cheers,
    Ron



More information about the Python-list mailing list