Skip to content

Instantly share code, notes, and snippets.

@mgrechukh
Created February 27, 2018 12:51
Show Gist options
  • Save mgrechukh/ed6ab59995b42f9faafc65d51bca4d55 to your computer and use it in GitHub Desktop.
Save mgrechukh/ed6ab59995b42f9faafc65d51bca4d55 to your computer and use it in GitHub Desktop.
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