[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:
Sebastian Wehling-Benatelli 2015-11-12 09:23:28 +01:00
parent a1fbea98be
commit 1f383579b4

View File

@ -1,7 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
#
# -*- coding: utf-8 -*-
import os import os
import subprocess import subprocess
@ -69,6 +67,30 @@ def getHash(time):
def getOwner(fn): def getOwner(fn):
return pwd.getpwuid(os.stat(fn).st_uid).pw_name 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): def prepTimeAxis(stime, trace):
nsamp = trace.stats.npts nsamp = trace.stats.npts
@ -342,3 +364,7 @@ def createAmplitude(pickID, amp, unit, category, cinfo):
amplitude.type = ope.AmplitudeCategory(category) amplitude.type = ope.AmplitudeCategory(category)
amplitude.pick_id = pickID amplitude.pick_id = pickID
return amplitude return amplitude
if __name__ == "__main__":
import doctest
doctest.testmod()