Package wsatools :: Module Utilities :: Class ParsedFile
[hide private]

Class ParsedFile

source code


Behaves like a file object, except that when iterating over file lines only non-blank, non-comment lines are returned and any EOL characters are removed together with trailing white-space. Use this class just like the file class when reading files:

   from wsatools.Utilities import ParsedFile
   for line in ParsedFile("myfile.txt"):
       print(line)
Instance Methods [hide private]
str
next(self)
Returns: The next line without EOL characters and trailing whitespace that isn't a blank and isn't a comment.
source code
str
readline(self)
Returns: The next line without EOL characters and trailing whitespace that isn't a blank and isn't a comment.
source code
list(str)
readlines(self)
Returns: A list of all lines in the file without EOL characters and trailing whitespace that aren't blank and aren't a comment.
source code

Inherited from file: __delattr__, __enter__, __exit__, __getattribute__, __init__, __iter__, __new__, __repr__, __setattr__, close, fileno, flush, isatty, read, readinto, seek, tell, truncate, write, writelines, xreadlines

Inherited from object: __format__, __hash__, __reduce__, __reduce_ex__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  commentMarker = '#'
Comment marker string.
  stripEOLComments = False
Strip comments from the end of lines too?
Properties [hide private]

Inherited from file: closed, encoding, errors, mode, name, newlines, softspace

Inherited from object: __class__

Method Details [hide private]

next(self)

source code 
Returns: str
The next line without EOL characters and trailing whitespace that isn't a blank and isn't a comment.
Overrides: file.next

Note: Unfortunately, I have reimplemented the base-class design for next() because the base-class does not call this readline() method. Could have chosen to call the base-class next() method and then parsed in the exact same way as readline() is parsed (save for the EOF return condition that is shouldn't be in the next() implementation). However, this way should be more efficient and maintainable.

readline(self)

source code 

Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line may be returned then). Return an empty string at EOF.

Returns: str
The next line without EOL characters and trailing whitespace that isn't a blank and isn't a comment. At EOF a blank string is returned.
Overrides: file.readline

readlines(self)

source code 

Call readline() repeatedly and return a list of the lines so read. The optional size argument, if given, is an approximate bound on the total number of bytes in the lines returned.

Returns: list(str)
A list of all lines in the file without EOL characters and trailing whitespace that aren't blank and aren't a comment.
Overrides: file.readlines

Note: Unfortunately, I have reimplemented the base-class design for readlines() because the base-class does not call this readline() method. Could have chosen to call the base-class readlines() method and then parsed in the same way as readline(), but this way should be more efficient and maintainable.