Want more features on Pastebin? Sign Up, it's FREE!
Guest

iteratee with python generator

By: a guest on Jul 29th, 2011  |  syntax: Python  |  size: 2.12 KB  |  views: 146  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def generator(fun):
  2.     def wrapper(*args, **kwargs):
  3.         gen = fun(*args, **kwargs)
  4.         gen.next()
  5.         return gen
  6.     return wrapper
  7.  
  8. @generator
  9. def identity():
  10.     chunk = None
  11.     while True:
  12.         chunk = yield chunk
  13.  
  14. @generator
  15. def lineno():
  16.     lineno = 0
  17.     chunk = yield
  18.     while True:
  19.         lineno += 1
  20.         chunk = yield lineno
  21.  
  22. @generator
  23. def lastn(n):
  24.     buf = []
  25.     chunk = yield
  26.     while True:
  27.         buf.append(chunk)
  28.         if len(buf)>n:
  29.             buf.pop(0)
  30.         chunk = yield buf
  31.  
  32. @generator
  33. def grepw(word):
  34.     line = yield
  35.     while True:
  36.         if word in line.split():
  37.             line = yield line
  38.         else:
  39.             line = yield
  40.  
  41. @generator
  42. def split(*gens):
  43.     chunk = yield
  44.     while True:
  45.         buf = []
  46.         for g in gens:
  47.             buf.append( g.send(chunk) )
  48.         if None in buf:
  49.             chunk = yield
  50.         else:
  51.             chunk = yield buf
  52.  
  53. @generator
  54. def pair(*gens):
  55.     chunks = yield
  56.     while True:
  57.         buf = []
  58.         for chunk, gen in zip(chunks, gens):
  59.             buf.append( gen.send(chunk) )
  60.         if None in buf:
  61.             chunk = yield
  62.         else:
  63.             chunks = yield buf
  64.  
  65. @generator
  66. def chain(g1, g2):
  67.     chunk = yield
  68.     while True:
  69.         chunk = g1.send(chunk)
  70.         if chunk:
  71.             chunk = yield g2.send(chunk)
  72.         else:
  73.             chunk = yield
  74.  
  75. def feed(enum, gen):
  76.     for trunk in enum:
  77.         result = gen.send(trunk)
  78.         if result!=None:
  79.             yield result
  80.  
  81. if __name__ == '__main__':
  82.     import sys
  83.     word = sys.argv[1]
  84.     count = int(sys.argv[2])
  85.     enum = open(sys.argv[3])
  86.     '''
  87.           +- lineno --------+
  88.           |                 |
  89.    line --+- grepw    ------+--- (lineno, line, context lines)
  90.           |                 |
  91.           +- context lines -+
  92.    '''
  93.     gen = split(
  94.               lineno(),
  95.               grepw(word),
  96.               lastn(count)
  97.           )
  98.  
  99.     res = feed(enum, gen)
  100.     for (lineno, line, last) in res:
  101.         print lineno
  102.         print ''.join(last)
clone this paste RAW Paste Data