armi.utils.textProcessors module

class armi.utils.textProcessors.SequentialReader(filePath)[source]

Bases: object

Fast sequential reader that must be used within a with statement.

line

value of the current line

Type

str

match

value of the current match

Type

re.match

Notes

This reader will sequentially search a file for a regular expression pattern or string depending on the method used. When the pattern/string is matched/found, the reader will stop, return True, and set the attributes line and match.

This pattern makes it easy to cycle through repetitive output in a very fast manner. For example, if you had a text file with consistent chuncks of information that always started with the same text followed by information, you could do something like this:

>>> with SequentialReader('somefile') as sr:
...     data = []
...     while sr.searchForText('start of data chunk'):
...         # this needs to repeat for as many chunks as there are.
...         if sr.searchForPatternOnNextLine('some-(?P<data>\w+)-pattern'):
...             data.append(sr.match['data'])
issueWarningOnFindingText(text, warning)[source]

Add a text search for every line of the file, if the text is found the specified warning will be issued.

This is important for determining if issues occurred while searching for text.

Parameters
  • text (str) – text to find within the file

  • warning (str) – An warning message to issue.

raiseErrorOnFindingText(text, error)[source]

Add a text search for every line of the file, if the text is found the specified error will be raised.

This is important for determining if errors occurred while searching for text.

Parameters
  • text (str) – text to find within the file

  • error (Exception) – An exception to raise.

raiseErrorOnFindingPattern(pattern, error)[source]

Add a pattern search for every line of the file, if the pattern is found the specified error will be raised.

This is important for determining if errors occurred while searching for text.

Parameters
  • pattern (str) – regular expression pattern

  • error (Exception) – An exception to raise.

searchForText(text)[source]

Search the file for the next occurrence of text, and set the self.line attribute to that line’s value if it matched.

Notes

This will search the file line by line until it finds the text. This sets the attribute self.line. If the previous _searchFor* method did not match, the last line it did not match will be searched first.

Returns

matched – Boolean inidcating whether or not the pattern matched

Return type

bool

searchForPattern(pattern)[source]

Search the file for the next occurece of pattern and set the self.line attribute to that line’s value if it matched.

Notes

This will search the file line by line until it finds the pattern. This sets the attribute self.line. If the previous _searchFor* method did not match, the last line it did not match will be searched first.

Returns

matched – Boolean inidcating whether or not the pattern matched

Return type

bool

searchForPatternOnNextLine(pattern)[source]

Search the next line for a given pattern, and set the self.line attribute to that line’s value if it matched.

Notes

This sets the attribute self.line. If the previous _searchFor* method did not match, the last line it did not match will be searched first.

Returns

matched – Boolean inidcating whether or not the pattern matched

Return type

bool

consumeLine()[source]

Consumes the line.

This is necessary when searching for the same pattern repetitively, because otherwise searchForPatternOnNextLine would not work.

class armi.utils.textProcessors.SequentialStringIOReader(stringIO)[source]

Bases: armi.utils.textProcessors.SequentialReader

Fast sequential reader that must be used within a with statement.

line

value of the current line

Type

str

match

value of the current match

Type

re.match

Notes

This reader will sequentially search a file for a regular expression pattern or string depending on the method used. When the pattern/string is matched/found, the reader will stop, return True, and set the attributes line and match.

This pattern makes it easy to cycle through repetitive output in a very fast manner. For example, if you had a text file with consistent chuncks of information that always started with the same text followed by information, you could do something like this:

>>> with SequentialReader('somefile') as sr:
...     data = []
...     while sr.searchForText('start of data chunk'):
...         # this needs to repeat for as many chunks as there are.
...         if sr.searchForPatternOnNextLine('some-(?P<data>\w+)-pattern'):
...             data.append(sr.match['data'])
__enter__()[source]

Override to prevent trying to open/reopen a StringIO object.

We don’t need to override __exit__, because it doesn’t care if closing the object fails.

class armi.utils.textProcessors.TextProcessor(fname, highMem=False)[source]

Bases: object

A general text processing object that extends python’s abilities to scan through huge files.

Use this instead of a raw file object to read data out of output files, etc.

scipat = '[+-]?\\d*\\.\\d+[eEdD][+-]\\d+'
number = '[+-]?\\d+\\.*\\d*'
decimal = '[+-]?\\d*\\.\\d+'
reset()[source]

rewinds the file so you can search through it again

errorChecking(checkForErrors)[source]
checkErrors(line)[source]
fsearch(pattern, msg=None, killOn=None, textFlag=False)[source]

Searches file f for pattern and displays msg when found. Returns line in which pattern is found or FALSE if no pattern is found. Stops searching if finds killOn first

If you specify textFlag=True, the search won’t use a regular expression (and can’t). The basic result is you get less powerful matching capabilities at a huge speedup (10x or so probably, but that’s just a guess.) pattern and killOn must be pure text if you do this.

class armi.utils.textProcessors.SmartList(f)[source]

Bases: object

A list that does stuff like files do i.e. remembers where it was, can seek, etc. Actually this is pretty slow. so much for being smart. nice idea though.

next()[source]
seek(line)[source]
close()[source]