#
# a python generator that returns data from a set of files
# this example uses 4 files, fold1.txt, fold2.txt, fold3.txt, fold4.txt
# each call to load() will return the next fold
# after 4 calls it wraps and starts over returning the first fold

# define a generator function
def load():
  fold=1
  while True:
    filename='fold'+str(fold)+'.txt'
    fpt=open(filename,'r')
    d=[[int(x) for x in line.split()] for line in fpt]
    fpt.close()
    yield d
    fold=fold+1
    if fold > 4:
      fold=1

# create the generator (object)
f=load()

# use the generator in a loop
for i in range(0,10):
  x=next(f)	# next() executes the generator function, returns data
  print(x[0])	# print the first sample (row) from the data file

