[spambayes-dev] New sort+group.py

T. Alexander Popiel popiel at wolfskeep.com
Sat Dec 27 15:03:15 EST 2003


In message:  <LNBBLJKPBEHFEDALKOLCOEJDIAAB.tim.one at comcast.net>
             "Tim Peters" <tim.one at comcast.net> writes:
>
>Attached is a major rewrite of testtools/sort+group.py.

Here's a patch to make it work with python2.2.  It appears that the
'for line in fh:' syntax for filereading in 2.2 buffered a bunch of
lines which were then unavailable for use to subsequent similar loops
in the case of the first loop terminating early.  Also, enumerate()
didn't seem to exist, so I just maintained the count manually.

Enjoy.

- Alex

--- sort+group.py.noworky	Sat Dec 27 11:58:04 2003
+++ sort+group.py	Sat Dec 27 11:57:37 2003
@@ -26,20 +26,21 @@
 def get_time(fpath):
     fh = file(fpath, "rb")
     # Find first Received header.
-    for line in fh:
+    line = fh.readline()
+    while line != "\r\n" and line != "\n" and line != "":
         if line.lower().startswith("received:"):
             break
+        line = fh.readline()
     else:
         print "\nNo Received header found."
         fh.close()
         return None
     # Paste on the continuation lines.
     received = line
-    for line in fh:
-        if line[0] in ' \t':
-            received += line
-        else:
-            break
+    line = fh.readline()
+    while line[0] in ' \t':
+        received += line
+        line = fh.readline()
     fh.close()
     # RFC 2822 says the date-time field must follow a semicolon at the end.
     i = received.rfind(';')
@@ -90,7 +91,9 @@
     if loud:
         print "Renaming second pass ..."
     earliest = data[0][0]  # timestamp of earliest msg received
-    for i, (when_received, name) in enumerate(data):
+    i = 0
+    for when_received, name in data:
+        i = i + 1
         dirname = os.path.dirname(name)
         basename = os.path.basename(name)
         extension = os.path.splitext(basename)[-1]



More information about the spambayes-dev mailing list