ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 02 Feb 2012 18:44:22 +0100Is it possible to convert a string to mapping?https://ask.sagemath.org/question/8691/is-it-possible-to-convert-a-string-to-mapping/I am trying to develop a program which involves plotting. I am using matplotlib as a backend to plot histograms. In order to give the users more control over the plotting, I am trying to take a string from the users of the form
str = '\{\'alpha\':\'0.2\'\}'
and then passing is to a histogram using
kwargs = str
plt.hist(X,Y,**kwargs)
I get a error saying that kwargs must be a mapping not a string. Is there a way to convert a string to kwargs mapping? I am not trying to do this so that it works just for alhpa, but any option that hist accepts.
Thu, 02 Feb 2012 17:34:18 +0100https://ask.sagemath.org/question/8691/is-it-possible-to-convert-a-string-to-mapping/Answer by DSM for <p>I am trying to develop a program which involves plotting. I am using matplotlib as a backend to plot histograms. In order to give the users more control over the plotting, I am trying to take a string from the users of the form </p>
<pre><code>str = '\{\'alpha\':\'0.2\'\}'
</code></pre>
<p>and then passing is to a histogram using</p>
<pre><code>kwargs = str
plt.hist(X,Y,**kwargs)
</code></pre>
<p>I get a error saying that kwargs must be a mapping not a string. Is there a way to convert a string to kwargs mapping? I am not trying to do this so that it works just for alhpa, but any option that hist accepts.</p>
https://ask.sagemath.org/question/8691/is-it-possible-to-convert-a-string-to-mapping/?answer=13231#post-id-13231If the objects involved are built-in Python types, you can use literal_eval:
sage: import ast
sage: s = "{'alpha': 0.2}"
sage: type(s)
<type 'str'>
sage: m = ast.literal_eval(s)
sage: m
{'alpha': 0.20000000000000001}
sage: type(m)
<type 'dict'>
literal_eval is safe and doesn't allow for arbitrary code execution. eval and sage_eval both will, which is always a bit dangerous.
BTW, you should try to avoid calling variables str -- that clobbers the built-in str type.
Thu, 02 Feb 2012 18:36:40 +0100https://ask.sagemath.org/question/8691/is-it-possible-to-convert-a-string-to-mapping/?answer=13231#post-id-13231Comment by Shashank for <p>If the objects involved are built-in Python types, you can use literal_eval:</p>
<pre><code>sage: import ast
sage: s = "{'alpha': 0.2}"
sage: type(s)
<type 'str'>
sage: m = ast.literal_eval(s)
sage: m
{'alpha': 0.20000000000000001}
sage: type(m)
<type 'dict'>
</code></pre>
<p>literal_eval is safe and doesn't allow for arbitrary code execution. eval and sage_eval both will, which is always a bit dangerous.</p>
<p>BTW, you should try to avoid calling variables str -- that clobbers the built-in str type.</p>
https://ask.sagemath.org/question/8691/is-it-possible-to-convert-a-string-to-mapping/?comment=20361#post-id-20361Thanks. that works!Thu, 02 Feb 2012 18:44:22 +0100https://ask.sagemath.org/question/8691/is-it-possible-to-convert-a-string-to-mapping/?comment=20361#post-id-20361