<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: What I learned about Python Today &#8211; eval()</title>
	<atom:link href="http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/</link>
	<description>Made with only the finest 1's and 0's</description>
	<lastBuildDate>Thu, 26 Jan 2012 21:20:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: nis</title>
		<link>http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/comment-page-1/#comment-185</link>
		<dc:creator>nis</dc:creator>
		<pubDate>Thu, 20 Mar 2008 17:37:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/#comment-185</guid>
		<description>I am interested to know what the speed difference is using your 250MB file between eval and getattr for the simple cases that work with either. Just curious.</description>
		<content:encoded><![CDATA[<p>I am interested to know what the speed difference is using your 250MB file between eval and getattr for the simple cases that work with either. Just curious.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kumar McMillan</title>
		<link>http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/comment-page-1/#comment-191</link>
		<dc:creator>Kumar McMillan</dc:creator>
		<pubDate>Tue, 11 Mar 2008 18:49:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/#comment-191</guid>
		<description># comment form swallowed my regex, replace _gt_ and _lt_ with the symbols:
expr = re.compile(r&#039;\s*(=&#124;_gt_&#124;_lt_)\s*&#039;)</description>
		<content:encoded><![CDATA[<p># comment form swallowed my regex, replace _gt_ and _lt_ with the symbols:<br />
expr = re.compile(r&#8217;\s*(=|_gt_|_lt_)\s*&#8217;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kumar McMillan</title>
		<link>http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/comment-page-1/#comment-190</link>
		<dc:creator>Kumar McMillan</dc:creator>
		<pubDate>Tue, 11 Mar 2008 18:45:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/#comment-190</guid>
		<description>90% of the time when I see people using eval(), I see a better way to go instead.  Here&#039;s why:

1. eval is unsafe.  no matter how hard you try, you will most likely leave some gaping security hole somewhere.  Especially if you are parsing a web log!  Try grepping for &quot;woot&quot; in your log.  This is the first entry I see in my log:

GET /path/to/mysite/woots.txt HTTP/1.1&quot; 404 366 &quot;-&quot; &quot;Java/1.6.0_01

the Internet is a crazy place.

2. eval makes for unreadable code.  If I were to see this written in someone else&#039;s code: eval(rule.attr, line.__dict__) I would have to stop and figure out what the hell is going on.  What are all possible values of rule.attr?  even if those are in comments of the docstring I am going to be annoyed that I had to read lengthy documentation where some more readable code would suffice.

How about this instead?

&gt;&gt;&gt; import cgi
&gt;&gt;&gt; import re
&gt;&gt;&gt; expr = re.compile(r&#039;\s*(=&#124;&gt;&#124;&gt;&gt; input_expr = expr.split(&#039;ip=192.168.1.2&#039;)
&gt;&gt;&gt; input_expr
[&#039;ip&#039;, &#039;=&#039;, &#039;192.168.1.2&#039;]
&gt;&gt;&gt; attr, op, val = input_expr
&gt;&gt;&gt; line_parsed = cgi.parse_qs(&#039;ip=192.168.1.2&amp;ip=192.168.1.240&#039;)
&gt;&gt;&gt; line_parsed
{&#039;ip&#039;: [&#039;192.168.1.2&#039;, &#039;192.168.1.240&#039;]}
&gt;&gt;&gt; line_matched = False
&gt;&gt;&gt; for parsed_val in line_parsed.get(attr, []):
...     if op == &#039;=&#039;:
...             if parsed_val == val:
...                     line_matched = True
...                     break
...     elif op == &#039;&gt;&#039;:
...             if parsed_val &gt; val:
...                     line_matched = True
...                     break
...     elif op == &#039;&lt;&#039;:
...             if parsed_val &gt;&gt; line_matched
True
&gt;&gt;&gt;

much more readable, IMHO, and tailored better to what you are trying to do (that is, given this description of what you&#039;re trying to do:))</description>
		<content:encoded><![CDATA[<p>90% of the time when I see people using eval(), I see a better way to go instead.  Here&#8217;s why:</p>
<p>1. eval is unsafe.  no matter how hard you try, you will most likely leave some gaping security hole somewhere.  Especially if you are parsing a web log!  Try grepping for &#8220;woot&#8221; in your log.  This is the first entry I see in my log:</p>
<p>GET /path/to/mysite/woots.txt HTTP/1.1&#8243; 404 366 &#8220;-&#8221; &#8220;Java/1.6.0_01</p>
<p>the Internet is a crazy place.</p>
<p>2. eval makes for unreadable code.  If I were to see this written in someone else&#8217;s code: eval(rule.attr, line.__dict__) I would have to stop and figure out what the hell is going on.  What are all possible values of rule.attr?  even if those are in comments of the docstring I am going to be annoyed that I had to read lengthy documentation where some more readable code would suffice.</p>
<p>How about this instead?</p>
<p>&gt;&gt;&gt; import cgi<br />
&gt;&gt;&gt; import re<br />
&gt;&gt;&gt; expr = re.compile(r&#8217;\s*(=|&gt;|&gt;&gt; input_expr = expr.split(&#8216;ip=192.168.1.2&#8242;)<br />
&gt;&gt;&gt; input_expr<br />
['ip', '=', '192.168.1.2']<br />
&gt;&gt;&gt; attr, op, val = input_expr<br />
&gt;&gt;&gt; line_parsed = cgi.parse_qs(&#8216;ip=192.168.1.2&amp;ip=192.168.1.240&#8242;)<br />
&gt;&gt;&gt; line_parsed<br />
{&#8216;ip&#8217;: ['192.168.1.2', '192.168.1.240']}<br />
&gt;&gt;&gt; line_matched = False<br />
&gt;&gt;&gt; for parsed_val in line_parsed.get(attr, []):<br />
&#8230;     if op == &#8216;=&#8217;:<br />
&#8230;             if parsed_val == val:<br />
&#8230;                     line_matched = True<br />
&#8230;                     break<br />
&#8230;     elif op == &#8216;&gt;&#8217;:<br />
&#8230;             if parsed_val &gt; val:<br />
&#8230;                     line_matched = True<br />
&#8230;                     break<br />
&#8230;     elif op == &#8216;&lt;&#8217;:<br />
&#8230;             if parsed_val &gt;&gt; line_matched<br />
True<br />
&gt;&gt;&gt;</p>
<p>much more readable, IMHO, and tailored better to what you are trying to do (that is, given this description of what you&#8217;re trying to do:))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: m0j0</title>
		<link>http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/comment-page-1/#comment-189</link>
		<dc:creator>m0j0</dc:creator>
		<pubDate>Tue, 11 Mar 2008 17:36:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/#comment-189</guid>
		<description>I understand the religion of not using things like eval(), but please at least read the documentation about how that function works, and take the time to test your hypotheses before you dismiss out of hand my use of it.

In addition, it&#039;s not like I&#039;m extolling the virtues of eval as a general safety net. It works in this instance, and I&#039;m not opposed to entertaining other ideas about what would work. However, I&#039;m not going to rewrite this code just because eval *can* be bad *IF USED INAPPROPRIATELY*. I&#039;ll remind everyone that just because something has the potential to be used unwisely does not make any use of that thing unwise. You can apply that to a great many things, technical or otherwise. Like a chainsaw, for example.

I can&#039;t profess to have read the code that implements eval(). I can&#039;t profess to know that it can&#039;t be broken as I&#039;ve used it. However, I&#039;m *confident* that it&#039;s safe the way I&#039;ve used it, and furthermore, the context of this application is that it is run by a user against *their own logs*, so we should also consider (in *addition* to the rest) what the cost of failure is in this case.</description>
		<content:encoded><![CDATA[<p>I understand the religion of not using things like eval(), but please at least read the documentation about how that function works, and take the time to test your hypotheses before you dismiss out of hand my use of it.</p>
<p>In addition, it&#8217;s not like I&#8217;m extolling the virtues of eval as a general safety net. It works in this instance, and I&#8217;m not opposed to entertaining other ideas about what would work. However, I&#8217;m not going to rewrite this code just because eval *can* be bad *IF USED INAPPROPRIATELY*. I&#8217;ll remind everyone that just because something has the potential to be used unwisely does not make any use of that thing unwise. You can apply that to a great many things, technical or otherwise. Like a chainsaw, for example.</p>
<p>I can&#8217;t profess to have read the code that implements eval(). I can&#8217;t profess to know that it can&#8217;t be broken as I&#8217;ve used it. However, I&#8217;m *confident* that it&#8217;s safe the way I&#8217;ve used it, and furthermore, the context of this application is that it is run by a user against *their own logs*, so we should also consider (in *addition* to the rest) what the cost of failure is in this case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: inopua</title>
		<link>http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/comment-page-1/#comment-188</link>
		<dc:creator>inopua</dc:creator>
		<pubDate>Tue, 11 Mar 2008 17:06:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.protocolostomy.com/2008/03/11/what-i-learned-about-python-today-eval/#comment-188</guid>
		<description>Then what about

?import sys;sys.exit();=whatever

?</description>
		<content:encoded><![CDATA[<p>Then what about</p>
<p>?import sys;sys.exit();=whatever</p>
<p>?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

