PDielec.IO
#
Simple IO interface
This aims to reproduce the python readline() io method, with limited functionality but much faster.
Module Contents#
Classes#
A class for reading lines from a file and providing an interface similar to file objects. |
- class PDielec.IO.pdielec_io(filename, mode)[source]#
A class for reading lines from a file and providing an interface similar to file objects.
The class opens a file upon initialization, reads its lines into memory, and allows sequential reading of these lines through the readline method. It also provides a close method to reset the reading process. This speeds up the reading of a file using the usual python readline() method.
Parameters#
- filenamestr
The path to the file to be read.
- modestr
The mode in which the file should be opened (e.g., ‘r’ for read).
Attributes#
- filenamestr
The path to the file that was opened.
- lineslist of str
The lines read from the file.
- indexint
The current position (line) in the file being read, initialized at 0.
- endint
The total number of lines in the file.
Methods#
- readline()
Reads the next line from the file. If the end of the file has been reached, returns an empty string.
- close()
Resets the reading process by resetting the current line index and deleting all lines read from the file from memory.
- close()[source]#
Closes the current object session.
Resets the index to 0 and deletes the lines attribute from the object.
Parameters#
None
Returns#
None
Notes#
This method is designed to perform cleanup tasks, such as resetting object state and freeing resources by deleting attributes. Calling this method on an object may render it unusable for certain operations that depend on the deleted attributes.
- readline()[source]#
Read and return the next line from the stored lines.
This method reads a line from the current position within an internal list of lines and then advances the position so that subsequent calls will return subsequent lines. If the end of the list is reached, it returns an empty string.
Parameters#
None
Returns#
- str
The next line in the internal list of lines, or an empty string if the end of the list is reached.