Skip to content

Commit b2554cb

Browse files
author
Plamen Milenkov
committed
Small enhancements in the code
1 parent 41b09ce commit b2554cb

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

days/37-39-csv-data-analsys/bad-drivers/__main__.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33
research = BadDriversResearch()
44

5-
# for state in research.sort_states_by_died_drivers()[:5]:
6-
# print("'{}' and died '{}'".format(state.state, state.drivers_fatal_collisions))
7-
#
8-
# print("#" * 20)
5+
print("States sorted by died drivers:")
6+
for state in research.sort_states_by_died_drivers()[:5]:
7+
print("'{}' and died '{}'".format(state.state, state.drivers_fatal_collisions))
98

9+
print("#" * 20)
10+
11+
print("States sorted by speeding drivers:")
1012
for state in research.sort_states_by_speed()[:10]:
11-
print("'{}' and speeding '{}'".format(state.state, state.drivers_fatal_collisions))
13+
print(f"'{state.state}' and speeding '{state.drivers_fatal_collisions}'".format(state))
14+
15+
print("#" * 20)
16+
17+
print("States sorted by drinking drivers:")
18+
for state in research.sort_states_by_alcohol()[:5]:
19+
print(f"'{state.state}' and drinking '{state.drivers_alcohol_impaired}'".format(state))
1220

13-
# print("#" * 20)
14-
#
15-
# for state in research.sort_states_by_alcohol()[:5]:
16-
# print("'{}' and drinking '{}'".format(state.state, state.drivers_fatal_collisions))
17-
#
1821
print("#" * 20)
1922

23+
print("States sorted by insurance companies losses:")
2024
for state in research.sort_states_by_insurance_company_loss()[:10]:
2125
print("'{}' and insurance losses '{}'".format(state.state, state.drivers_fatal_collisions))
2226

days/37-39-csv-data-analsys/bad-drivers/research.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
from typing import List
55

66
Record = namedtuple(
7-
'Record',
8-
'state,drivers_fatal_collisions,drivers_speeding,drivers_alcohol_impaired,'
9-
'drivers_not_distracted,drivers_first_collision,car_insurance,insurance_company_losses'
10-
)
7+
'Record',
8+
'state,drivers_fatal_collisions,drivers_speeding,drivers_alcohol_impaired,'
9+
'drivers_not_distracted,drivers_first_collision,car_insurance,insurance_company_losses'
10+
)
1111

1212
class BadDriversResearch:
1313

1414
def __init__(self):
1515
self.__data = []
16+
self.__load_file()
1617

17-
18+
def __load_file(self):
1819
base_folder = os.path.dirname(__file__)
1920
data_file = os.path.join(base_folder, 'bad-drivers.csv')
2021

@@ -37,10 +38,6 @@ def __parse_row(self, row):
3738
record = Record( **row )
3839
return record
3940

40-
def get_states(self) -> List[str]:
41-
for data in self.__data:
42-
yield data.state
43-
4441
def sort_states_by_died_drivers(self) -> List[Record]:
4542
return sorted(self.__data, key=lambda r: r.drivers_fatal_collisions, reverse=True)
4643

0 commit comments

Comments
 (0)