armi.utils.textProcessors module¶
-
class
armi.utils.textProcessors.SequentialReader(filePath)[source]¶ Bases:
objectFast 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 attributeslineandmatch.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.
See also
-
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.
See also
-
searchForText(text)[source]¶ Search the file for the next occurrence of
text, and set theself.lineattribute 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
patternand set theself.lineattribute 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.lineattribute 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
-
-
class
armi.utils.textProcessors.SequentialStringIOReader(stringIO)[source]¶ Bases:
armi.utils.textProcessors.SequentialReaderFast 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 attributeslineandmatch.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'])
-
-
class
armi.utils.textProcessors.TextProcessor(fname, highMem=False)[source]¶ Bases:
objectA 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+'¶
-
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.
-