[mod_python] using compile_code objects

Colin Bean ccbean at gmail.com
Thu Jun 1 02:28:03 EDT 2006


Hi David,

I think you can accomplish what you're trying to do without using
eval; correct me if I don't understand your problem.

In order to have a function that you can address from a dictionary,
you could do just that and use a function pointer; so something like:


_states = ['AZ', 'AL', 'MO']

def testState(name):
         return name in _states

_state = {'name': '',
         'max-length': 2,
         'validator':testState
         }
Then to validate a state name, you could use:
if _state['validator'](req.form['state']):
....

If you don't want to define a separate function for doing the
validation, you could change the above example to:

I think you can accomplish what you're trying to do without using
eval; correct me if I don't understand your problem.

In order to have a function that you can address from a dictionary,
you could do just that and use a function pointer; so something like:


_states = ['AZ', 'AL', 'MO']
_state = {'name': '',
         'max-length': 2,
         'validator':lambda n: n in _states
         }

Again, sorry if I'm missing something here, but I think this would do
what you're after.  I'd definitely avoid passing user input into an
eval statement.

HTH,
Colin

On 5/31/06, David Bear <David.Bear at asu.edu> wrote:
> I am thinking of writing a validation code in the definition of a
> field that I receive from a web form. For example, if I have a field
> named 'state', I would define a dictionary like this:
>
> _states = ['AZ', 'AL', 'MO']
> _state = {'name': '',
>           'max-length': 2,
>           'validator': 'in n in _states return true'
>           }
> then when I read my req.form['state'] I can say something like
>
> if eval(_state['validator']) == true:
>   ok
>
> I am wondering a coupling things:
>
> 1) has anyone else done something like this?
> 2) what kind of performance penalty is there for compile and eval
> builtins
> 3) other issues unique to modpython that would make this undesireable
> 4) how to parameterize a python string that I would compile (so I
> could pass in req.form['state'] to the _state['validator'] function.
>
> Hopefully this makes sense.
>
> --
> David Bear
> phone:  480-965-8257
> fax:    480-965-9189
> College of Public Programs/ASU
> Wilson Hall 232
> Tempe, AZ 85287-0803
>  "Beware the IP portfolio, everyone will be suspect of trespassing"
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list