/usr/share/doc/phatch/html/lib.safe.html is in phatch-doc 0.2.7.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>safe — Phatch v0.2 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Phatch v0.2 documentation" href="index.html" />
<link rel="up" title="lib" href="lib.html" />
<link rel="next" title="system" href="lib.system.html" />
<link rel="prev" title="reverse_translation" href="lib.reverse_translation.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="lib.system.html" title="system"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="lib.reverse_translation.html" title="reverse_translation"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Phatch v0.2 documentation</a> »</li>
<li><a href="lib.html" accesskey="U">lib</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-lib.safe">
<span id="safe"></span><h1>safe<a class="headerlink" href="#module-lib.safe" title="Permalink to this headline">¶</a></h1>
<dl class="exception">
<dt id="lib.safe.UnsafeError">
<em class="property">exception </em><tt class="descclassname">lib.safe.</tt><tt class="descname">UnsafeError</tt><a class="headerlink" href="#lib.safe.UnsafeError" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">exceptions.Exception</span></tt></p>
</dd></dl>
<dl class="function">
<dt id="lib.safe.assert_safe">
<tt class="descclassname">lib.safe.</tt><tt class="descname">assert_safe</tt><big>(</big><em>expr</em>, <em>_globals=None</em>, <em>_locals=None</em>, <em>validate=None</em><big>)</big><a class="headerlink" href="#lib.safe.assert_safe" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="lib.safe.assert_safe_expr">
<tt class="descclassname">lib.safe.</tt><tt class="descname">assert_safe_expr</tt><big>(</big><em>meta_expr</em>, <em>_globals=None</em>, <em>_locals=None</em>, <em>validate=None</em>, <em>preprocess=<function <lambda> at 0x8bf1c34></em><big>)</big><a class="headerlink" href="#lib.safe.assert_safe_expr" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="lib.safe.compile_expr">
<tt class="descclassname">lib.safe.</tt><tt class="descname">compile_expr</tt><big>(</big><em>meta_expr</em>, <em>_globals=None</em>, <em>_locals=None</em>, <em>validate=None</em>, <em>preprocess=<function <lambda> at 0x8bf1bc4></em>, <em>safe=True</em><big>)</big><a class="headerlink" href="#lib.safe.compile_expr" title="Permalink to this definition">¶</a></dt>
<dd><p>If safe is a list, a restricted evaluation will be executed.
Otherwise if safe is None, a unrestriced eval will be executed.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>meta_expr</strong> (<em>string</em>) – meta-expression with <subexpressions></li>
<li><strong>_globals</strong> (<em>dict</em>) – globals</li>
<li><strong>_locals</strong> (<em>dict</em>) – locals</li>
<li><strong>safe</strong> (<em>list or None</em>) – safe names which will be accepted by the compiler</li>
<li><strong>preprocess</strong> (<em>callable</em>) – preprocess expression (e.g. for ## formatting)</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">compile_expr</span><span class="p">(</span><span class="s">'<1+1>_<abs(2-3)>'</span><span class="p">,</span> <span class="n">safe</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="go">u'2_1'</span>
<span class="gp">>>> </span><span class="n">compile_expr</span><span class="p">(</span><span class="s">'<###(index+1)>'</span><span class="p">,</span> <span class="n">_locals</span><span class="o">=</span><span class="p">{</span><span class="s">'index'</span><span class="p">:</span><span class="mi">1</span><span class="p">},</span>
<span class="gp">... </span> <span class="n">preprocess</span><span class="o">=</span><span class="n">format_expr</span><span class="p">,</span> <span class="n">safe</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="go">u'002'</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="lib.safe.eval_restricted">
<tt class="descclassname">lib.safe.</tt><tt class="descname">eval_restricted</tt><big>(</big><em>s</em>, <em>_globals=None</em>, <em>_locals=None</em>, <em>allowed=</em><span class="optional">[</span>, <em>'abs'</em>, <em>'int'</em>, <em>'min'</em>, <em>'max'</em>, <em>'pow'</em>, <em>'sum'</em>, <em>'True'</em>, <em>'False'</em>, <em>'denominator'</em>, <em>'numerator'</em>, <em>'chr'</em>, <em>'lower'</em>, <em>'str'</em>, <em>'title'</em>, <em>'upper'</em>, <em>'day'</em>, <em>'hour'</em>, <em>'microsecond'</em>, <em>'minute'</em>, <em>'month'</em>, <em>'monthname'</em>, <em>'second'</em>, <em>'weekday'</em>, <em>'weekdayname'</em>, <em>'year'</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#lib.safe.eval_restricted" title="Permalink to this definition">¶</a></dt>
<dd><p>Evaluate an expression while allowing a restricted set of names.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>allowed</strong> (<em>list of string</em>) – allowed names</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">result</p>
</td>
</tr>
</tbody>
</table>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">eval_restricted</span><span class="p">(</span><span class="s">'max(a, a+b)'</span><span class="p">,</span> <span class="n">_globals</span><span class="o">=</span><span class="p">{</span><span class="s">'a'</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span> <span class="s">'b'</span><span class="p">:</span><span class="mi">2</span><span class="p">},</span>
<span class="gp">... </span><span class="n">_locals</span><span class="o">=</span><span class="p">{</span><span class="s">'a'</span><span class="p">:</span><span class="mi">1</span><span class="p">},</span> <span class="n">allowed</span><span class="o">=</span><span class="p">[</span><span class="s">'max'</span><span class="p">])</span>
<span class="go">3</span>
<span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">eval_restricted</span><span class="p">(</span><span class="s">'a+b+c'</span><span class="p">,</span> <span class="n">_globals</span><span class="o">=</span><span class="p">{</span><span class="s">'a'</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span> <span class="s">'b'</span><span class="p">:</span><span class="mi">2</span><span class="p">},</span> <span class="n">_locals</span><span class="o">=</span><span class="p">{</span><span class="s">'a'</span><span class="p">:</span><span class="mi">1</span><span class="p">})</span>
<span class="gp">... </span><span class="k">except</span> <span class="n">UnsafeError</span><span class="p">,</span> <span class="n">error</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
<span class="go">The following name(s) are invalid: c</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="lib.safe.eval_safe">
<tt class="descclassname">lib.safe.</tt><tt class="descname">eval_safe</tt><big>(</big><em>expr</em>, <em>_globals=None</em>, <em>_locals=None</em>, <em>validate=None</em><big>)</big><a class="headerlink" href="#lib.safe.eval_safe" title="Permalink to this definition">¶</a></dt>
<dd><p>Safely evaluate an expression. It will raise a <tt class="docutils literal"><span class="pre">ValueError</span></tt> if
non validated names are used.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>expr</strong> (<em>string</em>) – expression</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">result</p>
</td>
</tr>
</tbody>
</table>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">eval_safe</span><span class="p">(</span><span class="s">'1+1'</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">eval_safe</span><span class="p">(</span><span class="s">'"lowercase".upper()'</span><span class="p">)</span>
<span class="gp">... </span><span class="k">except</span> <span class="n">UnsafeError</span><span class="p">,</span> <span class="n">error</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
<span class="go">The following name(s) are invalid: upper</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="lib.safe.extend_vars">
<tt class="descclassname">lib.safe.</tt><tt class="descname">extend_vars</tt><big>(</big><em>vars</em>, <em>s</em><big>)</big><a class="headerlink" href="#lib.safe.extend_vars" title="Permalink to this definition">¶</a></dt>
<dd><p>Extend <tt class="docutils literal"><span class="pre">vars</span></tt> with new unique variables from <tt class="docutils literal"><span class="pre">s</span></tt>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>vars</strong> (<em>list of string</em>) – collection of previous variables</li>
<li><strong>s</strong> (<em>string</em>) – multiple expressions</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">vars</span> <span class="o">=</span> <span class="p">[</span><span class="s">'a1'</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">extend_vars</span><span class="p">(</span><span class="nb">vars</span><span class="p">,</span> <span class="s">'<a1>_<foo>_<world>_<###index>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">vars</span>
<span class="go">['a1', 'foo', 'world', 'index']</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="lib.safe.format_expr">
<tt class="descclassname">lib.safe.</tt><tt class="descname">format_expr</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#lib.safe.format_expr" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an expression with <tt class="docutils literal"><span class="pre">####</span></tt> in a pure python expression
which can be evaluated.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>s</strong> (<em>expression</em>) – expression</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">format_expr</span><span class="p">(</span><span class="s">'###(5+1)'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f</span>
<span class="go">'"%03d"%(5+1)'</span>
<span class="gp">>>> </span><span class="nb">eval</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">'006'</span>
</pre></div>
</div>
</dd></dl>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="lib.reverse_translation.html"
title="previous chapter">reverse_translation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="lib.system.html"
title="next chapter">system</a></p>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="lib.system.html" title="system"
>next</a> |</li>
<li class="right" >
<a href="lib.reverse_translation.html" title="reverse_translation"
>previous</a> |</li>
<li><a href="index.html">Phatch v0.2 documentation</a> »</li>
<li><a href="lib.html" >lib</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2009, www.stani.be.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
</div>
</body>
</html>
|