Thursday, July 26, 2012

Looking ahead

As the previous post suggests, I've been thinking about Twitter bots today, and in particular how to post lengthy text in 140-character chunks. A diversion was to think about how to "read ahead" - I wanted to be able to say "if the next chunk of text will take me over the 140-character limit, don't read it in". But I can't do that without reading it in (I could of course read the whole file into an array, then use pop() and append() to treat it as a stack, but I want to avoid having to read in the whole file if possible). A solution is below:

from tempfile import mkstemp
import shutil
import os

ifpath = "a.txt"

fh, ofpath = mkstemp()
ofile = open(ofpath, "w")
ifile = open(ifpath)

lines = []
length = 0

while True:
    previous = ifile.tell() 
    line = ifile.readline()
    if length + len(line) < 140:
        length += len(line)
        print lines

for line in ifile:

shutil.move(ofpath, ifpath)
This opens a.txt for reading (as ifile), and creates a temporary file (ofile). There's then an infinite loop - each time round, I store the current file location as previous, read in a new line, and check to see if that would take it over the limit. If not, I add the new line to the stored array. If it will go over the limit, I use to go back to the file location before I read in the new line, and then print out the lines stored so far. All that remains is to write the unread lines (including the one I read in and decided not to use) to a temporary file, remove the original a.txt, and replace it with the temporary one.

No comments:

Post a Comment