Hello. I am python newbie, just learning. I have written this class, when I realized, that maybe there is some class built-in?
class Data:
def __init__(self):
self.table = []
self.iterator = iter(self.table)
self.current_index = 0
self.valid_bus_stop_counter = 0
def reset_iterator(self):
self.iterator = iter(self.table)
def fill(self, reader):
for row in reader:
self.table.append(row)
def __iter__(self):
self.index = 0
return self
def next(self):
if self.iterator is None:
raise StopIteration
try:
result = next(self.iterator)
return result
except StopIteration:
self.iterator = None
raise StopIteration
def set_index(self, current_index):
self.current_index = current_index
def inc(self):
self.current_index += 1
def countStop(self):
self.valid_bus_stop_counter += 1
def get_index(self):
return self.current_index
And if there is such class that I can use to store array and pointer to current row (in a for loop I usually called it i like for i,row in table) or iterator to be saved. But I would like to add my own methods to it or even members like .bus_stops , .bus_stops_counter and similar...
I use it like that:
data = Data()
differencesBetweenBusStops = Data()
with open(csv_file, 'r', encoding='utf-8') as f:
reader = csv.reader(f, delimiter='\t')
first_row = next(reader)
idt = first_row[1]
next(reader)
data.fill(reader)
reset()
for i, row in enumerate(data.table):
... find out and collect bus stops names
... and collect differences (distances) between bus stops.