Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. # Simple grep in Python
  2.  
  3.  
  4. ```python
  5. #!/usr/bin/env python
  6. # -*- coding: utf-8 -*-
  7.  
  8. """Various text processing utilities.
  9.  
  10. Majority of these are intended for working with files but will work on any
  11. string iterables as well.
  12. """
  13.  
  14. import re
  15.  
  16.  
  17. def has_line(items, line):
  18. """Check if a line exists."""
  19. if not line.endswith('\n'):
  20. match = line + '\n'
  21. else:
  22. match = line
  23. return match in items
  24.  
  25.  
  26. def has_pattern(items, pattern, ignore_case=False):
  27. """Check if an item matching pattern exists."""
  28. regexp = re.compile(pattern, flags=ignore_case and re.I)
  29. for item in items:
  30. match = regexp.search(item)
  31. if match:
  32. return True
  33. else:
  34. return False
  35.  
  36.  
  37. def grep(items, pattern, ignore_case=False):
  38. """Get a list of all items matching regular expression."""
  39. regexp = re.compile(pattern, flags=ignore_case and re.I)
  40. matches = []
  41. for item in items:
  42. match = regexp.search(item)
  43. if match:
  44. matches.append(match.string)
  45. return matches
  46.  
  47.  
  48. def igrep(items, pattern, ignore_case=False):
  49. """Iterate over all items matching regular expression."""
  50. regexp = re.compile(pattern, flags=ignore_case and re.I)
  51. return filter(regexp.search, items)
  52.  
  53.  
  54. def cli(in_file, pattern, ignore_case=False, *args, **kwargs):
  55. """CLI entrypoint for `igrep`."""
  56. matches = igrep(in_file, pattern, ignore_case)
  57. # sort of a hack to check if iterator has at least one item
  58. try:
  59. first = next(matches)
  60. print(first, end='')
  61. except StopIteration:
  62. return 1
  63. for match in matches:
  64. print(match, end='')
  65. return 0
  66.  
  67.  
  68. if __name__ == '__main__':
  69. import argparse
  70. import sys
  71. parser = argparse.ArgumentParser()
  72. parser.add_argument(
  73. 'pattern',
  74. metavar='PATTERN',
  75. help='pattern to search for')
  76. parser.add_argument(
  77. 'in_file',
  78. nargs='?',
  79. metavar='FILE',
  80. default=sys.stdin,
  81. type=argparse.FileType('r'),
  82. help='file to search in')
  83. parser.add_argument(
  84. '-i',
  85. '--ignore-case',
  86. action='store_true',
  87. help='case insensitive match')
  88. args = parser.parse_args()
  89. sys.exit(cli(**vars(args)))
  90. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement