Last active
November 20, 2015 14:24
-
-
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)
This file contains hidden or 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
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