<div class="gmail_quote">On Mon, Mar 2, 2009 at 11:18 PM, Andre Engels <span dir="ltr">&lt;<a href="mailto:andreengels@gmail.com">andreengels@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Tue, Mar 3, 2009 at 4:54 AM, Wayne Watson<br>
&lt;<a href="mailto:sierra_mtnview@sbcglobal.net">sierra_mtnview@sbcglobal.net</a>&gt; wrote:<br>
&gt;        self.recent_events = [ event for event in self.recent_events<br>
&gt;                                if os.path.exists(event) and<br>
&gt;                                (time.time() - os.path.getmtime(event)) &lt;<br>
&gt; 3600.0 ]<br>
</blockquote><div><br>In this example, we&#39;ll step through self.recent_events - which apparently is a list of filenames - and call each item we come across &quot;event&quot;.  That&#39;s the &quot;for event in self.recent_events&quot; part.  <br>
If the filename corresponds to an actual file (&quot;if os.path.exists(event)&quot;) AND that file has been modified less than an hour ago (the difference between the current time and the file&#39;s modification time is less than 3,600 seconds), then we add &quot;event&quot; to the list we&#39;re building and move on to the next &quot;event&quot; until we&#39;re done.<br>
At the end, we call our new list self.recent_events, which replaces the list we were looping through a moment ago.  Chances are it&#39;s a bit shorter now.<br><br>List comprehensions will make your head hurt the first few dozen times you encounter them.  After that, they become easier to use than the longer for-loop structure they replace - as Andr<font style="color: rgb(0, 0, 0);" color="#888888">é</font> pointed out, they read almost like English.<br>
</div></div><br>-- <br><a href="http://www.fsrtechnologies.com">www.fsrtechnologies.com</a><br>