castep_outputs.utilities.filewrapper#

Convenient filewrapper class.

Classes

Block(parent)

Data block class returned from get_block().

FileWrapper(file)

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.

aslist()[source]#

Return block as a list of lines.

Returns:

Block as list of lines.

Return type:

list[str]

asstringio()[source]#

Return block as a simulated file.

Returns:

Block data as StringIO

Return type:

StringIO

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:

data (Sequence[str]) – Data to read into block.

Return type:

TypeVar(Self, bound= Block)

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:

Block

Notes

Advances in_file as it does so.

Raises:

IOError – 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:
Returns:

Read data.

Return type:

Block

Raises:

IOError – 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 lineno: int#

Current line number.

Returns:

Current line number in file.

Return type:

int

property name: str#

Name of underlying file.

Returns:

Name if name is known otherwise “unknown”.

Return type:

str

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.

Parameters:
  • fore (int) – Whether to strip leading line from data.

  • back (int) – Whether to strip trailing line from data.

Return type:

None

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.

Return type:

None

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:

TextIO

property lineno: int#

Current line number.

Returns:

Current line number in file.

Return type:

int

property name: str#

Name of underlying file.

Returns:

Name if name is known otherwise “unknown”.

Return type:

str

rewind()[source]#

Rewind file to previous line.

If iterated by next (or for) can rewind one line.

Useful for blocks not terminated by a clear statement, where we can only check if next line does NOT match.

Return type:

None