[new] added a new utility function to find a pattern in a text file and returning the particular line as a string or None if not found
This commit is contained in:
parent
a1fbea98be
commit
1f383579b4
@ -1,7 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
@ -69,6 +67,30 @@ def getHash(time):
|
||||
def getOwner(fn):
|
||||
return pwd.getpwuid(os.stat(fn).st_uid).pw_name
|
||||
|
||||
def getPatternLine(fn, pattern):
|
||||
"""
|
||||
Takes a file name and a pattern string to search for in the file and
|
||||
returns the first line which contains the pattern string otherwise None.
|
||||
|
||||
:param fn: file name
|
||||
:type fn: str
|
||||
:param pattern: pattern string to search for
|
||||
:type pattern: str
|
||||
:return: the complete line containing pattern or None
|
||||
|
||||
>>> getPatternLine('utils.py', 'python')
|
||||
'#!/usr/bin/env python\\n'
|
||||
>>> print(getPatternLine('version.py', 'palindrome'))
|
||||
None
|
||||
"""
|
||||
fobj = open(fn, 'r')
|
||||
for line in fobj.readlines():
|
||||
if pattern in line:
|
||||
fobj.close()
|
||||
return line
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def prepTimeAxis(stime, trace):
|
||||
nsamp = trace.stats.npts
|
||||
@ -342,3 +364,7 @@ def createAmplitude(pickID, amp, unit, category, cinfo):
|
||||
amplitude.type = ope.AmplitudeCategory(category)
|
||||
amplitude.pick_id = pickID
|
||||
return amplitude
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
Loading…
Reference in New Issue
Block a user