Chemical Data Processing Library Python API - Version 1.4.0
Public Member Functions | Properties | List of all members
CDPL.Base.FileIOStream Class Reference

Wrapper for C++ std::fstream exposing a Python-style file-oriented I/O interface. More...

+ Inheritance diagram for CDPL.Base.FileIOStream:

Public Member Functions

None __init__ (str file_name, str mode='r')
 Constructs the FileIOStream instance and opens the specified file in the given mode. More...
 
None __init__ (str file_name, OpenMode mode=IOStream.OpenMode.IN)
 Constructs the FileIOStream instance and opens the specified file in the given mode. More...
 
str readline (int size=-1)
 Reads one entire line. More...
 
list readlines (int size=-1)
 Reads lines until EOF and returns a list containing the lines thus read. More...
 
FileIOStream xreadlines ()
 Returns the stream instance. More...
 
str read (int size=-1)
 Reads at most size bytes. More...
 
int tell ()
 Returns the current read position. More...
 
int tellr ()
 Returns the current read position. More...
 
None seek (int offs, int whence=0)
 Sets the current read position. More...
 
None seekr (int offs, int whence=0)
 Sets the current read position. More...
 
str next ()
 Reads and returns the next line. More...
 
bool isClosed ()
 Tells whether the stream has been closed. More...
 
str getOpenModeString ()
 Returns the open mode string that was provided as argument to the constructor. More...
 
OpenMode getOpenModeFlags ()
 Returns the open mode flags that were provided as argument to the constructor. More...
 
None flush ()
 Flushes the internal buffer.
 
None write (object string)
 Outputs the specified string. More...
 
None writelines (object iterable)
 Outputs the specified sequence of strings. More...
 
int tellw ()
 Returns the current write position. More...
 
None seekw (int offs, int whence=0)
 Sets the current write position. More...
 
bool getSoftSpace ()
 Returns a boolean that indicates whether a space character needs to be printed before another value when using the print statement. More...
 
None setSoftSpace (bool value)
 Sets a boolean that indicates whether a space character needs to be printed before another value when using the print statement. More...
 
None close ()
 Closes the stream. More...
 
str getFileName ()
 Return the file path string that got passed to the constructor. More...
 
FileIOStream __iter__ ()
 Returns the stream instance. More...
 
- Public Member Functions inherited from CDPL.Base.IStream
int getObjectID ()
 Returns the numeric identifier (ID) of the wrapped C++ class instance. More...
 
- Public Member Functions inherited from CDPL.Base.OStream
int getObjectID ()
 Returns the numeric identifier (ID) of the wrapped C++ class instance. More...
 

Properties

 closed = property(isClosed)
 
 mode = property(getOpenModeString)
 
 modeFlags = property(getOpenModeFlags)
 
 softspace = property(getSoftSpace, setSoftSpace)
 
 name = property(getFileName)
 
- Properties inherited from CDPL.Base.IStream
 objectID = property(getObjectID)
 
- Properties inherited from CDPL.Base.OStream
 objectID = property(getObjectID)
 

Additional Inherited Members

- Static Public Attributes inherited from CDPL.Base.IStream
 STD_IN = _HIDDEN_VALUE_
 The standard input stream of the current process.
 
- Static Public Attributes inherited from CDPL.Base.OStream
 STD_OUT = _HIDDEN_VALUE_
 The standard output stream of the current process.
 
 STD_ERR = _HIDDEN_VALUE_
 The standard error stream of the current process.
 

Detailed Description

Wrapper for C++ std::fstream exposing a Python-style file-oriented I/O interface.

Constructor & Destructor Documentation

◆ __init__() [1/2]

None CDPL.Base.FileIOStream.__init__ ( str  file_name,
str   mode = 'r' 
)

Constructs the FileIOStream instance and opens the specified file in the given mode.

Parameters
file_nameThe path to the file to open.
modeA combination of file open mode flags (see IOStream.OpenMode) or a Python-style open mode string (e.g. 'r+').

◆ __init__() [2/2]

None CDPL.Base.FileIOStream.__init__ ( str  file_name,
OpenMode   mode = IOStream.OpenMode.IN 
)

Constructs the FileIOStream instance and opens the specified file in the given mode.

Parameters
file_nameThe path to the file to open.
modeA combination of file open mode flags (see IOStream.OpenMode) or a Python-style open mode string (e.g. 'r+').

Member Function Documentation

◆ readline()

str CDPL.Base.FileIOStream.readline ( int   size = -1)

Reads one entire line.

A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. When size is not 0, an empty string is returned only if EOF is encountered immediately.

Parameters
sizeIf present and non-negative, specifies the maximum number of bytes to read (including the trailing newline).
Returns
A string holding the read line.

◆ readlines()

list CDPL.Base.FileIOStream.readlines ( int   size = -1)

Reads lines until EOF and returns a list containing the lines thus read.

If the optional size argument is present and non-negative, instead of reading up to EOF, whole lines totalling approximately size bytes are read.

Parameters
sizeIf present and non-negative, specifies the maximum number of bytes to read.
Returns
A list containing the read lines.

◆ xreadlines()

FileIOStream CDPL.Base.FileIOStream.xreadlines ( )

Returns the stream instance.

Returns
self.

◆ read()

str CDPL.Base.FileIOStream.read ( int   size = -1)

Reads at most size bytes.

If the size argument is negative or omitted, all data until EOF is reached are read. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately.

Parameters
sizeIf present and non-negative, specifies the maximum number of bytes to read.
Returns
A string containing the read data.

◆ tell()

int CDPL.Base.FileIOStream.tell ( )

Returns the current read position.

Returns
The current read position.

◆ tellr()

int CDPL.Base.FileIOStream.tellr ( )

Returns the current read position.

Returns
The current read position.

◆ seek()

None CDPL.Base.FileIOStream.seek ( int  offs,
int   whence = 0 
)

Sets the current read position.

The whence argument is optional and defaults to 0 (absolute positioning). Other supported values are 1 to seek relative to the current position and 2 for seeking relative to the end of input.

Parameters
offsThe offset to use for the seek operation.
whenceValue specifying how to calculate the final read position.

◆ seekr()

None CDPL.Base.FileIOStream.seekr ( int  offs,
int   whence = 0 
)

Sets the current read position.

The whence argument is optional and defaults to 0 (absolute positioning). Other supported values are 1 to seek relative to the current position and 2 for seeking relative to the end of input.

Parameters
offsThe offset to use for the seek operation.
whenceValue specifying how to calculate the final read position.

◆ next()

str CDPL.Base.FileIOStream.next ( )

Reads and returns the next line.

A stream object is its own iterator, for example iter(s) returns s. When a stream is used as an iterator, typically in a for loop (for example, for line in s: print line.strip()), the next() method is called repeatedly. This method returns the next input line, or raises StopIteration when EOF is hit when the stream is open for reading (when the stream has been opened for writing only then Base.IOError is raised).

Returns
A string holding the read line.

◆ isClosed()

bool CDPL.Base.FileIOStream.isClosed ( )

Tells whether the stream has been closed.

See also
close()

◆ getOpenModeString()

str CDPL.Base.FileIOStream.getOpenModeString ( )

Returns the open mode string that was provided as argument to the constructor.

Returns
The open mode string (e.g. 'r+') that was provided as argument to the constructor.

◆ getOpenModeFlags()

OpenMode CDPL.Base.FileIOStream.getOpenModeFlags ( )

Returns the open mode flags that were provided as argument to the constructor.

Returns
The open mode flags that were provided as argument to the constructor (see Stream.OpenMode).

◆ write()

None CDPL.Base.FileIOStream.write ( object  string)

Outputs the specified string.

Parameters
stringThe string to output.
Note
Due to buffering, the string may not actually show up in the file until the flush() or close() method is called.

◆ writelines()

None CDPL.Base.FileIOStream.writelines ( object  iterable)

Outputs the specified sequence of strings.

Parameters
iterableThe string sequence to output (can be any iterable object producing strings, typically a list of strings).

◆ tellw()

int CDPL.Base.FileIOStream.tellw ( )

Returns the current write position.

Returns
The current write position.

◆ seekw()

None CDPL.Base.FileIOStream.seekw ( int  offs,
int   whence = 0 
)

Sets the current write position.

The whence argument is optional and defaults to 0 (absolute positioning). Other supported values are 1 to seek relative to the current position and 2 for seeking relative to the end of input.

Parameters
offsThe offset to use for the seek operation.
whenceValue specifying how to calculate the final write position.

◆ getSoftSpace()

bool CDPL.Base.FileIOStream.getSoftSpace ( )

Returns a boolean that indicates whether a space character needs to be printed before another value when using the print statement.

Returns
True if a space character needs to be printed before another value when using the print statement, and False otherwise.

◆ setSoftSpace()

None CDPL.Base.FileIOStream.setSoftSpace ( bool  value)

Sets a boolean that indicates whether a space character needs to be printed before another value when using the print statement.

Parameters
valueTrue if a space character shall be printed before another value when using the print statement, and False otherwise.

◆ close()

None CDPL.Base.FileIOStream.close ( )

Closes the stream.

A closed stream cannot be read from or written to anymore. Any operation which requires the stream to be open will raise a Base.ValueError after the stream has been closed. Calling close() more than once is allowed.

◆ getFileName()

str CDPL.Base.FileIOStream.getFileName ( )

Return the file path string that got passed to the constructor.

Returns
The file path string that got passed to the constructor.

◆ __iter__()

FileIOStream CDPL.Base.FileIOStream.__iter__ ( )

Returns the stream instance.

Returns
self.