This commit is contained in:
Mike Bloy 2023-10-02 16:28:35 -05:00
parent 8b2d2fb1dd
commit 76e3381760
2 changed files with 80 additions and 13 deletions

View File

@ -1,24 +1,86 @@
import csv
from collections import namedtuple
from dataclasses import dataclass
def read_rides_as_tuples(filename):
"""Read the bus ride data as a list of tuples"""
records = []
@dataclass
class DataClassRow:
route: str
date: str
daytype: str
rides: int
class BasicRow:
def __init__(self, route, date, daytype, rides):
self.route = route
self.date = date
self.daytype = daytype
self.rides = rides
class SlotsRow:
__slots__ = ['route', 'date', 'daytype', 'rides']
def __init__(self, route, date, daytype, rides):
self.route = route
self.date = date
self.daytype = daytype
self.rides = rides
TupleRow = namedtuple('Row', ['route', 'date', 'daytype', 'rides'])
def read_rides(filename, readfunc):
"""Read the bus ride data using a conversion function."""
with open(filename) as f:
rows = csv.reader(f)
next(rows)
for row in rows:
route = row[0]
date = row[1]
daytype = row[2]
rides = int(row[3])
record = (route, date, daytype, rides)
records.append(record)
return records
return [readfunc(row) for row in rows]
def row_to_tuple(row):
return (row[0], row[1], row[2], int(row[3]))
def row_to_dict(row):
return dict(route=row[0], date=row[1], daytype=row[2], rides=int(row[3]))
def row_to_namedtuple(row):
return TupleRow(row[0], row[1], row[2], int(row[3]))
def row_to_class(row):
return BasicRow(row[0], row[1], row[2], int(row[3]))
def row_to_slotsclass(row):
return SlotsRow(row[0], row[1], row[2], int(row[3]))
def row_to_dataclass(row):
return DataClassRow(route=row[0], date=row[1],
daytype=row[2], rides=int(row[3]))
if __name__ == '__main__':
import sys
import tracemalloc
method = sys.argv[1]
methods = {
'tuple': row_to_tuple,
'dict': row_to_dict,
'namedtuple': row_to_namedtuple,
'class': row_to_class,
'slots': row_to_slotsclass,
'dataclass': row_to_dataclass,
}
if method not in methods:
print("unknown method")
sys.exit(-1)
tracemalloc.start()
rows = read_rides_as_tuples('Data/ctabus.csv')
print('Memory Use: Current %d, Peak %d' % tracemalloc.get_traced_memory())
rows = read_rides('Data/ctabus.csv', methods[method])
print('Memory Use: Current %d, Peak %d;' % tracemalloc.get_traced_memory(),
'Method:', method)

5
readrides_data.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
for method in tuple dict namedtuple class slots dataclass; do
python readrides.py $method
done