Created
February 27, 2018 12:51
-
-
Save mgrechukh/ed6ab59995b42f9faafc65d51bca4d55 to your computer and use it in GitHub Desktop.
This file contains 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
import re | |
class Grep: | |
def __init__(self, pattern): | |
self.pattern = re.compile(pattern) | |
def __ror__(self, iterable): | |
for x in iterable: | |
if self.pattern.match(x): | |
yield x | |
class Wc: | |
def __init__(self, t): | |
self.count = 0 | |
if t == '-l': | |
self.type = 'l' | |
elif t == '-c': | |
self.type = 'c' | |
else: | |
raise ValueError() | |
def __ror__(self, iterable): | |
for x in iterable: | |
if self.type == 'l': | |
self.count += 1 | |
elif self.type == 'c': | |
self.count += len(x) | |
return self.count | |
open('/home/mykola/test.txt') | Grep('abc') | Wc('-c') | |
open('/home/mykola/test.txt') | Grep('abc') | Wc('-l') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment