From: Karol L. <kar...@kn...> - 2007-07-31 11:21:05
|
On Tuesday 31 July 2007 05:27, Noel O'Boyle wrote: > I would say avoid peeking and seeking and all that. All you need to do > is to make sure that the code for reading in the next section, follows > the code for reading in this section, and that the name of the > variable containing the "$" line is the same as the name of the > variable that is used in the big loop over all lines (typically the > variable name is "line"). Caring about the order of the sections works, and remember that the following section does not have to be directly after the first one in the code (since the conditions in between will not be fullfilled). I would recommend using a more explicit solution that is harder to break. In the first section set a unique flag if your special line is found. Then add "or <flag>" to the initial codnition of the second section. You will also need to add a condition for reading a new line (after the one with "$") in the second section, since it will have been read if the flag is set - so something like "if not <flag>: line = inputfile.next()" should be OK. I hope all that makes sense - I've also done something similar in the past. > Does this make sense? I've done this one before. Will this fit all > cases? (i.e. do you always know the order of the sections). Clearly, > it would be very helpful if you add comments indicating that the order > of a particular section is important if you use this method. If we break up the extract() method into a dictionary of functions as discussed before, there would be no problem with this, since you could just call the second method from within the first. After separating the parser and data object, i seems to me that the dictionary of functions i within grasp. > On 31/07/07, Christopher Rowley <cro...@uo...> wrote: > > Is there a preferred way to peek ahead in inputfile iterator? The problem > > I'm having is that the only way to tell if a section is over is if the > > next line is the start of the new section (ie, it starts with a $). I can > > think of one work around, which is to use tell() to get the starting > > position of the new line, and then seeking back to that point it if it is > > the end of the section. Please let me know if there a more elegant way to > > do this. -- written by Karol Langner Tue Jul 31 13:02:27 EDT 2007 |