[...] >> >> >>> >>> Additionally, I think the code from 470-495 and 514-522 or so could >>> be greatly simplified by using the builtin splitlines function, >>> which splits a string at newline characters and returns them in an >>> array: >>> for line in some_long_string.splitlines(): >>> log(line) >>> However, I haven't taken a deep look at this code, so if I'm >>> oversimplifying just ignore me. >> >> The problem here is that we need to keep the last line in the buffer >> if it is incomplete (not terminated with '\n') and not log it - >> I think (I might be wrong since I haven't tried) that the suggested >> change wouldn't address this aspect. > > Ah, I see now. If you wanted to, you could store the splitlines as such: > > lines = string_to_log.splitlines(True) > last_line = lines[-1] > for line in lines[:-1]: > log(line) > > That would log everything but the last line, and save the last line > for later processing. You could then process the last line as before. > The True parameter to splitlines preserves the \n for each piece, e.g.: > > "Hello\nThis is a string\n" becomes ["Hello\n", "This is a string\n"] > and > "Hello\nThis is a string too" becomes ["Hello\n", "This is a string too"] > > Again, this is just a suggestion, feel free to take it or leave it.
Hi Keith, I have tried to go with this solutions, since using splitlines() looks like better approach, but I have realized that I still need to process last line separately, since we want to keep it for later processing only if it is incomplete. Otherwise, we want to log it immediately. The final solution then was not so elegant, so if you agree, at this point I would go with the original approach. Thank you for thinking about this, Jan
