Last active
March 25, 2016 07:33
-
-
Save mpenkov/003ab2648ee7f003882a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Check if a file contains a set of chars | |
# | |
import argparse | |
import logging | |
import sys | |
import time | |
import collections | |
import unittest | |
import itertools | |
def read_file(path): | |
if not path: | |
return sys.stdin.read() | |
else: | |
with open(path) as fin: | |
return fin.read() | |
def chargrep_loop(chars, longstr): | |
for c in chars: | |
if longstr.find(c) != -1: | |
return True | |
return False | |
def chargrep_set(chars, longstr): | |
return len(chars.intersection(set(longstr))) != 0 | |
def chargrep_counter(chars, longstr): | |
counter = collections.Counter(longstr) | |
return len(set(chars).intersection(counter.keys())) != 0 | |
def chargrep_itertools(chars, longstr): | |
# | |
# https://www.safaribooksonline.com/library/view/python-cookbook-2nd/0596007973/ch01s09.html | |
# | |
for item in itertools.ifilter(chars.__contains__, longstr): | |
return True | |
return False | |
class CharGrepTest(unittest.TestCase): | |
def sanity_test(self): | |
self.assertTrue(chargrep_loop("abc", "the quick brown fox")) | |
self.assertFalse(chargrep_loop("yz", "the quick brown fox")) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("chars", type=str, default=None) | |
parser.add_argument("file", nargs="?", type=str, default=None) | |
parser.add_argument("-m", "--method", type=str, default="loop") | |
parser.add_argument("-l", "--loglevel", type=str, default="ERROR") | |
args = parser.parse_args() | |
logging.basicConfig(level=args.loglevel) | |
the_file = read_file(args.file) | |
the_method = globals()["chargrep_" + args.method] | |
before = time.time() | |
result = the_method(set(args.chars), the_file) | |
after = time.time() | |
print result | |
logging.info("took %.2fs" % (after - before)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment