<div class="gmail_quote">On Mon, Mar 2, 2009 at 11:18 PM, Andre Engels <span dir="ltr"><<a href="mailto:andreengels@gmail.com">andreengels@gmail.com</a>></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>
<<a href="mailto:sierra_mtnview@sbcglobal.net">sierra_mtnview@sbcglobal.net</a>> wrote:<br>
> self.recent_events = [ event for event in self.recent_events<br>
> if os.path.exists(event) and<br>
> (time.time() - os.path.getmtime(event)) <<br>
> 3600.0 ]<br>
</blockquote><div><br>In this example, we'll step through self.recent_events - which apparently is a list of filenames - and call each item we come across "event". That's the "for event in self.recent_events" part. <br>
If the filename corresponds to an actual file ("if os.path.exists(event)") AND that file has been modified less than an hour ago (the difference between the current time and the file's modification time is less than 3,600 seconds), then we add "event" to the list we're building and move on to the next "event" until we'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'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>