Re: Repost: Suggestion - builtin module 'settings' and usage conventions

Guido.van.Rossum@cwi.nl
Wed, 27 Jul 1994 20:47:02 +0200

> I think it would be good for python to institute some sort of package-
> default-values mechanism, for use by system, optional, and custom
> packages.

Yes, I think the time has come to think about such a mechanism. I
would start however by an implementation that doesn't need a built-in
module -- a standard library module written in Python should suffice,
and makes it easier to experiment.

I'd say the design needs to pay attention to two different interfaces:
(1) how does the user specify values, and (2) how does a package
extract values.

The basic interface could be something like this. A module "custom"
exists which exports two functions:

custom.defvar(packagename, varname, value)
value = custom.getvar(packagename, varname [, defaultvalue])

The first call to defvar sets the value for the variable, further
calls are ignored. The default value would default to None (this is
only used in case the package doesn't actually ever call defvar).

By convention, a module should use its own module name (preferably
using __name__ instead of spelling it out) for the package name, but
it may be useful at times to have a single package name shared by a
group of cooperating modules, or to simply peek in another package's
variables.

This doesn't solve all of (1) -- for a customization mechanism to be
useful, it must be easy for an end user to customize a package,
e.g. by editing a standard startup file. For interactive Python
sessions, there is already the $PYTHONSTARTUP mechanism -- a user
could add something to the startup file like this:

from custom import defvar
defvar('pdb', 'verbose_stacktrace', 1)
defvar('tempfile', 'tmpdir', '/tmp')
...

But suppose we want the customizations to affect scripts as well.
Then the user could place the defvar calls in a file
"$HOME/.custom.py" and the custom module could read this when it is
imported, e.g. through execfile (as a convenience, it could place the
defvar function in the globals dictionary so the .custom.py file
needn't import defvar).

An alternative would be to have a separate customization file per
package -- the custom module should then read this file the first time
a particular module is mentioned. Since there are potentially many
packages, it's best to collect the files in a directory, so the files
might be $HOME/.python/<packagename>.py. We can now do away with the
defvar business and let the user write

(in .python/pdb.py)
verbose_stacktrace = 1

(in .python/tempfile.py)
tmpdir = '/tmp'

A disadvantage of this scheme is that it requires the user to create
many small files, each containing maybe one or a few lines. We could
combine the schemes...

That's all I can think of for now... Somebody else please shoot holes
in it or provide an implementation!

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
<URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>