On Wed, Feb 18, 2009 at 1:01 PM, Wayne Watson
<sierra_mtnv...@sbcglobal.net>wrote:

>  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?
>

Simplest: put them in the same folder as your own program.  More standard:
put them in one of the folders in your Python path - same general idea as
the DOS search path, it's a list of directories where Python will look for
modules you ask it to import.

>From the docs: (http://docs.python.org/tutorial/modules.html)
The Module Search
Path¶<http://docs.python.org/tutorial/modules.html#the-module-search-path>

When a module named spam is imported, the interpreter searches for a file
named spam.py in the current directory, and then in the list of directories
specified by the environment variable
*PYTHONPATH*<http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH>.
This has the same syntax as the shell variable *PATH*, that is, a list of
directory names. When
*PYTHONPATH*<http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH>is
not set, or when the file is not found there, the search continues in
an
installation-dependent default path; on Unix, this is usually
.:/usr/local/lib/python.

Actually, modules are searched in the list of directories given by the
variable sys.path which is initialized from the directory containing the
input script (or the current directory),
*PYTHONPATH*<http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH>and
the installation- dependent default. This allows Python programs that
know what they're doing to modify or replace the module search path. Note
that because the directory containing the script being run is on the search
path, it is important that the script not have the same name as a standard
module, or Python will attempt to load the script as a module when that
module is imported. This will generally be an error. See section *Standard
Modules* <http://docs.python.org/tutorial/modules.html#tut-standardmodules>for
more information.



>
> 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><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  -  
> tu...@python.orghttp://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/>
>
>


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

Reply via email to