Title: Signature.html
OK, I got the ObjectConfig and Validate downloads, and copied your Global class successfully. Don't I need to get the two files into Python as a module? How does that work?

Marc Tompkins wrote:
On Wed, Feb 18, 2009 at 7:30 AM, Wayne Watson <sierra_mtnv...@sbcglobal.net> wrote:
See Subject. I've run across a 58 page document <http://www.voidspace.org.uk/python/configobj.html>, but am uncertain of its applicability to my present needs (see my thread on "Changing the Attribute"). Usually, I end up with some 3-4 page document, so this raises an eyebrow. Am I on the right trail? In the meantime, I think I'll turn it into a more manageable pdf file before putting it to paper.

It's a very powerful module - does lots of things you don't need yet, and maybe never will - and I agree that it could benefit from a hit-the-ground-running guide...

Here's a quick-and-dirty; you can flesh out the details from the documentation you downloaded.  (By the way, you'll also want to use the validate module - another 14 pages or so...)
======================================================
    * Download configobj.py and validate.py; put them somewhere in your Python path (or in the same folder as your project, if you want)
    * Add two lines to the top of your program:
        from configobj import ConfigObj
        from validate import Validator
    * To make working with your spec easier,
        import StringIO  # allows you to work with strings as if they were files
        # You could just create a spec file, and open it normally, but this keeps everything in one place.
    * Somewhere in your program, create your spec and load it.  There are a lot of ways you can do this - for me, the simple thing is to put it in a class.  I create a class called Global - it's a crutch, I know...
        class Global(object): 
            cfgFileName = os.getcwd() + os.sep + 'Initial.sen' # current directory + "\" + filename - but you could put it anywhere and call it anything
            # create your spec as a multi-line string between triple quotes
            tmpStr = """
            mask_file_name = string(default=None)
            gray_scale = boolean(default=True)
            post_event_stack = boolean(default=False)
            post_event_format = string(default="Tiff 2")
            show_real_time = boolean(default=False)
            hourly_rate = integer(min=0, default=0)
            slowdown= integer(min=0, max=10, default=1) # I don't know what this value is/does, so this is probably wrong
            start_time = string(default="00:00:00") # or you could make the default None?
            stop_time = string(default="00:00:00")
            lat = float(min=0.0, max=90.0) # I have no idea what you'd want as a default latitude, if any...

            """
            cfgSpec = StringIO.StringIO(tmpStr) # turns the above string into a file-like object, which ConfigObj is expecting
            cfgFile = ConfigObj(cfgFileName,
                        configspec=cfgSpec, raise_errors=True, write_empty_values=True,
                        create_empty=True, indent_type='    ', list_values=True) # creates the actual ConfigObj object
            vtor = Validator() # creates a validator to match your config data against your spec
    * Invoke the above code when your program starts - I do it just before my mainloop, like so:
        def main(argv=None):
            if argv is None:
                argv = sys.argv
            test = Global.cfgFile.validate(Global.vtor, copy=True) # tries to load cfgFile; if it doesn't exist, creates it; fills in defaults for any missing values
            Global.cfgFile.write() # saves the validated file (you don't need to do this here at the start, but I usually do)
            app = MyApp(0)   
            app.MainLoop()
        if __name__ == '__main__':
            main()
    * Your ConfigObj is ready to use!  Simply access the contents like a regular dictionary.  To save back to the file, call the write() method.
        Global.cfgFile['mask_file_name'] = 'bogus.txt'
        Global.cfgFile.write()
           
            maskFile = Global.cfgFile['mask_file_name']
    * Or use the values directly without copying to intermediate variables:
        with open(Global.cfgFile['mask_file_name'], 'w+b') as maskFile:
            maskFile.write('some bogus text')
======================================================
I'm attaching that as a text file too - I suspect that my indentation won't survive the trip...

My Global class is an ugly hack.  I'm sure you'll come up with something more aesthetically pleasing...
ConfigObj has lots of very cool features - sections, repeated sections, custom input validators - that's what the 50+ pages of documentation are about.  When you need that stuff, it's available.
--
www.fsrtechnologies.com

_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

--


           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)
            

                "Nature, to be commanded, must be obeyed."
                                   -- Sir Francis Bacon 

                    Web Page: <www.speckledwithstars.net/>
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to