castep_outputs.utilities.filewrapper#
Convenient filewrapper class.
Classes
| 
 | Data block class returned from  | 
| 
 | Convenience file wrapper to add rewind and line number capabilities. | 
- class castep_outputs.utilities.filewrapper.Block(parent)[source]#
- Bases: - object- Data block class returned from - get_block().- Emulates the properties of both a file, and sequence. - asstringio()[source]#
- Return block as a simulated file. - Returns:
- Block data as StringIO 
- Return type:
 
 - property file: NoReturn#
- Block has no internal file. - Raises:
- NotImplementedError – Block has no internal file holder. 
 
 - classmethod from_iterable(data, parent=None)[source]#
- Construct a Block from an iterable containing strings. - Parameters:
- Returns:
- Block from an iterable. 
- Return type:
- Self 
 - Examples - >>> x = Block.from_iterable(("Hello", "There")) >>> type(x).__name__ 'Block' >>> str(x) 'Hello\nThere' 
 - classmethod from_re(init_line, in_file, start, end, *, n_end=1, eof_possible=False)[source]#
- Check if line is the start of a block and return the block if it is. - Parameters:
- init_line ( - str) – Initial line which may start the block.
- in_file ( - TextIO|- FileWrapper|- Block) – File handle to read data from.
- start ( - Union[- str,- Pattern]) – RegEx matched against init_line to see if is start of block.
- end ( - Union[- str,- Pattern]) – RegEx to verify if block has ended.
- n_end ( - int) – Number of times end must match before block is returned.
- eof_possible ( - bool) – Whether it is possible block is ended by EOF.
 
- Returns:
- Recovered block of data matching prereqs. 
- Return type:
 - Notes - Advances in_file as it does so. - Raises:
- OSError – If EOF reached and - not eof_possible.
 
 - classmethod get_lines(in_file, n_lines, *, eof_possible=False)[source]#
- Read the next n_lines from in_file and return the block. - Parameters:
- in_file ( - TextIO|- FileWrapper|- Block) – File to read data from.
- n_lines ( - int) – Number of lines to read.
 
- Returns:
- Read data. 
- Return type:
- Raises:
- OSError – If EOF reached and - not eof_possible.
 - Examples - >>> from io import StringIO >>> x = StringIO('Hello\nThere\nFriend') >>> block = Block.get_lines(x, 2) >>> type(block).__name__ 'Block' >>> block.aslist() ['Hello\n', 'There\n'] 
 - property name: str#
- Name of underlying file. - Returns:
- Name if name is known otherwise “unknown”. 
- Return type:
 
 - remove_bounds(fore=1, back=2)[source]#
- Remove the bounding lines of the block. - These are often region delimiters and not relevant to the processed data. 
 - rewind()[source]#
- Rewind to previous line. - Useful for blocks not terminated by a clear statement, where we can only check if next line does NOT match. - Raises:
- ValueError – Block already rewound fully. 
- Return type:
 
 
- class castep_outputs.utilities.filewrapper.FileWrapper(file)[source]#
- Bases: - object- Convenience file wrapper to add rewind and line number capabilities. - Parameters:
- file ( - TextIO) – File to wrap and control.
 - property file: TextIO#
- File handle wrapped by the filewrapper. - Returns:
- Underlying file. 
- Return type: