ex 2.5
This commit is contained in:
parent
e958141123
commit
fa6038eb9f
50
mutint.py
Normal file
50
mutint.py
Normal file
@ -0,0 +1,50 @@
|
||||
from functools import total_ordering
|
||||
|
||||
|
||||
@total_ordering
|
||||
class MutInt:
|
||||
__slots__ = ['value']
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
return str(self.value)
|
||||
|
||||
def __repr__(self):
|
||||
return f'MutInt({self.value!r})'
|
||||
|
||||
def __format__(self, fmt):
|
||||
return format(self.value, fmt)
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, MutInt):
|
||||
return MutInt(self.value + other.value)
|
||||
elif isinstance(other, int):
|
||||
return MutInt(self.value + other)
|
||||
else:
|
||||
return NotImplemented
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, MutInt):
|
||||
return self.value == other.value
|
||||
elif isinstance(other, int):
|
||||
return self.value == other
|
||||
else:
|
||||
return NotImplemented
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, MutInt):
|
||||
return self.value < other.value
|
||||
elif isinstance(other, int):
|
||||
return self.value < other
|
||||
else:
|
||||
return NotImplemented
|
||||
|
||||
def __int__(self):
|
||||
return self.value
|
||||
|
||||
def __float__(self):
|
||||
return float(self.value)
|
||||
|
||||
__index__ = __int__
|
||||
69
readrides.py
69
readrides.py
@ -1,3 +1,4 @@
|
||||
import collections
|
||||
import csv
|
||||
from collections import namedtuple
|
||||
from dataclasses import dataclass
|
||||
@ -41,6 +42,74 @@ def read_rides(filename, readfunc):
|
||||
return [readfunc(row) for row in rows]
|
||||
|
||||
|
||||
def read_rides_as_tuples(filename):
|
||||
return read_rides(filename, row_to_tuple)
|
||||
|
||||
|
||||
def read_rides_as_dicts(filename):
|
||||
return read_rides(filename, row_to_dict)
|
||||
|
||||
|
||||
def read_rides_as_namedtuples(filename):
|
||||
return read_rides(filename, row_to_namedtuple)
|
||||
|
||||
|
||||
def read_rides_as_classes(filename):
|
||||
return read_rides(filename, row_to_class)
|
||||
|
||||
|
||||
def read_rides_as_slotsclasses(filename):
|
||||
return read_rides(filename, row_to_slotsclass)
|
||||
|
||||
|
||||
def read_rides_as_dataclasses(filename):
|
||||
return read_rides(filename, row_to_dataclass)
|
||||
|
||||
|
||||
class RideData(collections.abc.Sequence):
|
||||
def __init__(self):
|
||||
self.routes = []
|
||||
self.dates = []
|
||||
self.daytypes = []
|
||||
self.numrides = []
|
||||
|
||||
def __len__(self):
|
||||
return len(self.routes)
|
||||
|
||||
def __getitem__(self, index):
|
||||
if not isinstance(index, slice):
|
||||
return dict(route=self.routes[index],
|
||||
date=self.dates[index],
|
||||
daytype=self.daytypes[index],
|
||||
rides=self.numrides[index])
|
||||
value = RideData()
|
||||
value.routes = self.routes[index]
|
||||
value.dates = self.dates[index]
|
||||
value.daytypes = self.daytypes[index]
|
||||
value.numrides = self.numrides[index]
|
||||
return value
|
||||
|
||||
def append(self, d):
|
||||
self.routes.append(d['route'])
|
||||
self.dates.append(d['date'])
|
||||
self.daytypes.append(d['daytype'])
|
||||
self.numrides.append(d['rides'])
|
||||
|
||||
|
||||
def read_rides_as_columns(filename):
|
||||
"""Read the bus ride data into 4 lists, one per column."""
|
||||
data = RideData()
|
||||
with open(filename) as f:
|
||||
rows = csv.reader(f)
|
||||
next(rows)
|
||||
for row in rows:
|
||||
data.append(dict(route=row[0],
|
||||
date=row[1],
|
||||
daytype=row[2],
|
||||
rides=int(row[3])))
|
||||
return data
|
||||
|
||||
|
||||
def row_to_tuple(row):
|
||||
return (row[0], row[1], row[2], int(row[3]))
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user