Why a class when there will only be one instance?

Christian Tismer tismer at stackless.com
Thu May 27 21:37:13 EDT 2004


Vineet Jain wrote:

>>Just keep track of the state you want to preserve and
>>you want to re-initialise, and then call the different parts
>>from outside. Easy.
> 
> 
> If I went with just functions and not classes then I could pass 
> a variable (class instance) which can be used by users to store some 
> state information between calls. I would also use the same variable to 
> expose global variables.

Even simpler. Please see the compile.__doc__

You compile the code, and then you execute it with the exec statement.

Simplest apporach:

You have a file, and you use execfile (see its doc)

then you can execfile(filename, globals, locals),

so you just pass the dict which is used as the globals.
Before executing, you put a special version of __builtins__
into the globals dict, and you select only builtin functions
that you want to allow, or customized versions.
This is rather safe. At least, a novice programmer
cannot break through, easily.

> How would you write a custom evaluator? 
> Is the evaluator that comes with python written in c?
> How difficult would it be?

Absolutely simple. Just use what Python has, and remove some
builtins :-)

> How can I restrict the code that the user can run so that he can write
> only to his sub directory and that he does not crash the system somehow?

See above, start with execfile. Later, you can refine, after
you got more used to this.
You build a customized __builtins__ module. You insert that
into a fresh dict for the globals. Then you execfile the user's
code in that, and pick the results he left in the globals.

> Thanks for your help.

You're welcome. I think you are set -- chris

-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  mobile +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/





More information about the Python-list mailing list