Skip to content

Instantly share code, notes, and snippets.

@sr105
Last active November 20, 2015 14:24
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 sr105/694deeca9acb55618434 to your computer and use it in GitHub Desktop.
Save sr105/694deeca9acb55618434 to your computer and use it in GitHub Desktop.
Answer for [Can I yield from an instance method](http://stackoverflow.com/q/33818422/47078)
from itertools import izip, islice
import random
def sumdiff(data):
return ((x + y, x - y) for x, y in data)
def combined_file_data(files):
for i,n in files:
# Generate some data.
x = range(n)
y = range(100,n+100)
for data in izip(x,y):
yield data
def filelist(nfiles):
for i in range(nfiles):
# Generate some data.
n = random.randint(50000, 100000)
print 'file %d n=%d' % (i, n)
yield i, n
def Nth(iterable, step):
return islice(iterable, step-1, None, step)
nskip = 12
nfiles = 10
filedata = combined_file_data(filelist(nfiles))
nth_data = Nth(filedata, nskip)
for nthsum, nthdiff in sumdiff(nth_data):
assert nthsum is not None
assert nthdiff is not None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment