String To Dict Problem
Michael Spencer
mahs at telcopartners.com
Fri Apr 21 22:15:57 EDT 2006
Clodoaldo Pinto wrote:
> Michael Spencer wrote:
>
>> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469
>
> Very nice work. It will be very useful. Thanks.
>
> Only a small problem when I try to evaluate this:
>
> safe_eval('True')
>
> I get:
>
> Traceback (most recent call last):
> File "safe_eval.py", line 63, in ?
> safe_eval('True')
> File "safe_eval.py", line 59, in safe_eval
> return walker.visit(ast)
> File "safe_eval.py", line 19, in visit
> return meth(node, **kw)
> File "safe_eval.py", line 23, in default
> return self.visit(child, **kw)
> File "safe_eval.py", line 19, in visit
> return meth(node, **kw)
> File "safe_eval.py", line 47, in visitName
> node.name, node)
> __main__.Unsafe_Source_Error: Line 1. Strings must be quoted: True
>
> This is just to let you know. I can live with that. I just replace True
> for 1.
>
> Regards, Clodoaldo Pinto
>
Alternatively, you could edit visitName to allow 'True' and any other
identifiers you specify e.g. (untested):
allowed = {"True": True, "False": False}
def visitName(self,node, **kw):
try:
return self.allowed[node.name]
except KeyError:
raise Unsafe_Source_Error("Strings must be quoted",
node.name, node)
Cheers
Michael
More information about the Python-list
mailing list