Skip to content

Instantly share code, notes, and snippets.

@ayharano
Created January 3, 2018 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayharano/069acd658eb93512d552672bc4d47aef to your computer and use it in GitHub Desktop.
Save ayharano/069acd658eb93512d552672bc4d47aef to your computer and use it in GitHub Desktop.
A grep-like copycat to match before and after matched lines, if requested.
#!/usr/bin/env python3
"""A `grep`-like copycat to match before and after matched lines.
"""
import re
def grep(pattern, string, before=0, after=0):
"""A grep-like copycat to match before and after matched lines,
if requested.
Matches a pattern in string, retrieving before and after lines.
Args:
pattern: a valid regular expression to match against.
string: str to find pattern.
before: a non negative int number of lines to retrieve
before each matching line.
after: a non negative int number of lines to retrieve
after each matching line.
Return:
A list composed by instances of str covering
a single match and lines before and after the match itself.
Raises:
TypeError: invalid parameter type.
ValueError: invalid parameter value.
"""
try:
re.compile(pattern)
except re.error:
raise ValueError('pattern is not a valid regular expression. '
'Found value: {0}'
.format(pattern))
if not isinstance(string, str):
raise TypeError('string should be str. '
'Found type: {0}'
.format(type(string)))
if not isinstance(before, int):
raise TypeError('before should be int. '
'Found type: {0}'
.format(type(before)))
if not isinstance(after, int):
raise TypeError('after should be int. '
'Found type: {0}'
.format(type(after)))
if before < 0:
raise ValueError('before should be a non negative int. '
'Found value: {0}'
.format(before))
if after < 0:
raise ValueError('after should be a non negative int. '
'Found value: {0}'
.format(after))
split_string = string.split('\n')
return ['\n'.join(split_string[index-before:index+after+1])
for index, value in enumerate(split_string)
if re.findall(pattern, value)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment