Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2016 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d0cc2fdec90363ead171c48abdd03510 to your computer and use it in GitHub Desktop.
Save anonymous/d0cc2fdec90363ead171c48abdd03510 to your computer and use it in GitHub Desktop.
# Wneh we can pass FileObject to write
class SampleStorletWithWriter(object):
def __init__(self, logger):
self.logger = logger
def __call__(self, in_md, in_files, out_md_files, params):
for i in xrange(2):
out_md_files[i].dumps(in_md[i])
while True:
data0 = in_files[0].read(1)
data1 = in_files[1].read(1)
if not data0 == '' or data1 == '':
break
out_files[0].write(max(data0, data1))
out_files[0].write(min(data0, data1))
# When we get iterator as its return value
from collections import deque
class SampleStorlet(object):
def __init__(self, logger):
self.logger = logger
self.max_queue = deque()
self.min_queue = deque()
def read_next(self, in_files):
d0 = in_files[0].read()
d1 = in_files[1].read()
if d0 == '' or d1 == '':
raise StopIteration()
else:
self.max_queue.append(max(d0, d1))
self.min_queue.append(min(d0, d1))
def get_iter(self, queue):
while True:
if not queue:
self.read_next()
continue
yield self.queue.popleft()
def __call__(self, in_md, in_files, params):
return in_md, [self.get_iter(self.max_queue), self.get_iter(self.min_queue)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment