#
# a python generator that returns part of a single data file
# size = how many samples per return (yield)
# increments through the file from line 1...size, size+1...2*size, etc.
# each call to load() will return the next batch
# last batch at end of file may be smaller

# define a generator function
def load():
  size=501			# change as desired
  start=0
  end=start+size-1
  while True:
    data=[]
    with open('simslope.txt','r') as fpt:
      for i,line in enumerate(fpt):
        if i >= start and i <= end:
          row=[int(x) for x in line.split()]
          data.append(row)
    fpt.close()
    yield data
    if i <= end:		# check if past end of file
      start=0
      end=start+size-1
    else:
      start=start+size
      end=end+size

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

# use the generator in a loop
for i in range(0,3):
  x=next(f)	# next() executes the generator function, returns data
  print(len(x),'total rows')	# report how many data loaded
  print('first row is',x[0])	# print the first sample (row)

