Invoking Python from Python
Mike Meyer
mwm at mired.org
Wed Nov 9 14:54:24 EST 2005
claird at lairds.us (Cameron Laird) writes:
> I'll rein myself in and suggest an even easier introduction
> to this subject: configuration files. RARELY is the correct
> answer to create a new syntax, although many development
> organizations give the impression that's their first choice.
> ".ini"-speak is a safe-enough choice. Most interesting,
> though, is to interpret Python or some subset as a configu-
> ration specification, so that one has immediately not just
> HOME = "/some/folder"
> STEP_LIMIT = 16
> but
> pool_size = cpu_count * 30
> and even
> if today == "Sunday":
> total_process_maximum = 8
> available in the configuration language. Neat, eh?
I once carried this a step further, and used methodless classes as a
configuration mechanism:
class PlainFoo:
# Values for a plain foo
class FancyFoo(PlainFoo):
# Overrides for just the things that are different
The program that used this created needed lots of objects, in a
variety of different flavers that were literally specified as "Just
like PlainFoo, but with ...". Doing it this way made configuring
things trivial.
At the time, I attached "configuration variables" to instances. If I
were going to do it today, I'd look into making the parent classes of
the class that implements Foo dynanmic.
plwm (an X11 window manager - sort of - built in top of python-xlib)
carries this another step further. You configure your window manager
by creating a subclass of the WindowManager (or other) class that
mixes in the features you want, and sets the attributes to control
specific features.
It's very flexible - but at this point, the "configuration file" is a
Python program, and not really suitable to use by non-programmers.
> But if configuration is *that* powerful, then it can also
> do great damage. How does one make Python interpretation safe?
> That's a subject for another day.
We're all waiting for this, somewhat impatiently :-).
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list