forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bikeshare.py
535 lines (494 loc) · 18.4 KB
/
bikeshare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import time
#import pandas as pd
#import numpy as np
import csv
import datetime as dt
import operator
import sys
import os
'''
Definition of a user input possibilities
'''
CITY_DATA = { 'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv' }
MONTHS = {'january':1,
'february':2,
'march':3,
'april':4,
'may':5,
'june':6,
'july':7,
'august':8,
'september':9,
'october':10,
'november':11,
'december':12,
'all':0,
'no data':-1}
DAYS = {'monday':1,
'tuesday':2,
'wednesday':3,
'thursday':4,
'friday':5,
'saturday':6,
'sunday':7,
'all':0,
'no data':-1}
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
def welcome():
"""
Welcome the user and ask whether he wants to proceed or not.
Returns:
(bool) True - Proceed
(bool) False - Quit
"""
clear_screen()
print("Welcome to US bikeshare data, are you ready to go?")
exit_counter = 0
while(True):
response = input("Type yes or no: ")
response = response.lower() # make response lower case for processing
if (response == "yes" or response == "y"):
print("Okay, let's go!")
clear_screen()
return True
break
elif(response == "no" or response == "n"):
print("Okay, then it's time for me to go I guess.")
return False
break
else:
exit_counter += 1
if exit_counter > 4:
print("Okay, I don't have all day. Other customers are waiting for me. See you :)")
break
print("Sorry I couldn't understand you. Type 'yes' or 'y' to continue, 'no' or 'n' to quit.")
def get_filters():
"""
Asks user to specify a city, month, and day to analyze.
Returns:
(str) city - name of the city to analyze. (Must be specified)
(str) month - name of the month to filter by, or "all" to proceed without filter
(str) day - name of the day of week to filter by, or "all" to proceed without filter
"""
#initialize variables
city = "none"
month = "none"
day = "none"
print('Let\'s explore some US bikeshare data!')
# get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
while(city == "none"):
print('First, tell me which city you\'d like to explorer. You can choose between Chicago, New York City and Washington')
city = input("City selection: ")
city = check_filter('city', city, CITY_DATA)
# get user input for month (all, january, february, ... , june)
while(month == "none"):
print('What about the month? You can eighter type it\'s name or the corresponding number. For the whole year, type \'all\' or 0.')
month = input('Month selection: ')
month = check_filter('month', month, MONTHS)
# get user input for day of week (all, monday, tuesday, ... sunday)
while(day == "none"):
print('We\'re almost done. Just give me the weekday you would like to see. You can eighter type the name or it\'s number, starting with 1 on monday.')
print('To explorer all days just type \'all\' or 0.')
day = input("Day selection: ")
day = check_filter('day', day, DAYS)
# reverse lookup integer values
if month.isnumeric():
month = dict_reverse(month, MONTHS)
if day.isnumeric():
day = dict_reverse(day, DAYS)
print('-'*40)
summary = "Your city is {}, your month is {}, and your weekday is {}. We are almost ready to go.".format(city.title(), month, day)
print(summary)
return city, month, day
def check_filter(title, input_data, dictionary):
"""
Check if the user's input matches the possibilities given by a dictionary
Args:
(str) title - name of the input value, used for response print()
(str) input_data - user's input, checked against the given dictionary
(dict) dictionary - possible input values
Returns:
(str) intut_data - will be just returned as it is if allowed, or modified and returned if not inside the dictionary
"""
input_data = input_data.lower()
if not input_data in dictionary.keys():
if input_data.isnumeric():
dictlen = len(dictionary.keys()) -1
if int(input_data) > dictlen or int(input_data) < 0 or title == "city":
print("Sorry, I can\'t determine which " + title + " corresponds to the number you gave me. Let's retry...")
input_data = "none"
elif not input_data.isnumeric():
print("Sorry, I have no data about the given " + title + ". Did you type it's whole name correctly? Let's retry...")
input_data = "none"
return input_data
def dict_reverse(value, dictionary):
"""
Dictionary reverse lookup, finds the key for a given value inside a given dictionary
Args:
(str) value - value to find inside the dictionary
(dict) dictionary - dict to lookup for the given value
Returns:
(str) d_key - key for the given value
"""
for d_key, d_val in dictionary.items():
if d_val == int(value):
return d_key
def clear_screen():
'''
Clear the console screen to have a clean window if requiered
Note: found on Stackoverflow - https://stackoverflow.com/questions/4810537/how-to-clear-the-screen-in-python
'''
os.system('cls')
def load_data(city, month, day):
"""
Loads data for the specified city and filters by month and day if applicable.
Args:
(str) city - name of the city to analyze
(str) month - name of the month to filter by, or "all" to apply no month filter
(str) day - name of the day of week to filter by, or "all" to apply no day filter
Returns:
df - Pandas DataFrame containing city data filtered by month and day
"""
clear_screen()
print('Please wait while I apply your filters to the bikeshare data...')
starttime = dt.datetime.now()
filename = CITY_DATA.get(city)
rentals = []
with open(filename,'r') as datafile:
raw_rentals = list(csv.reader(datafile,delimiter=','))
datafile.close()
header = raw_rentals[:1]
header[0][0] = 'RentalId'
act = 1
last = len(raw_rentals) -1
month_num = MONTHS.get(month)
day_num = DAYS.get(day)
print_progress(0, last, 'Progress: ', 'Complete', 1, 50)
rentals.append(header)
while act < last:
print_progress(act+1, last, 'Progress: ', 'Complete', 1, 50)
actDate = dt.datetime.strptime(raw_rentals[act][1], DATE_FORMAT)
actMonth = actDate.month
actDay = actDate.isoweekday()
if not month_num == 0:
if not day_num == 0:
if actDay == day_num and actMonth == month_num:
rentals.append(raw_rentals[act])
else:
if actMonth == month_num:
rentals.append(raw_rentals[act])
else:
if not day_num == 0:
if actDay == day_num:
rentals.append(raw_rentals[act])
else:
rentals.append(raw_rentals[act])
act += 1
duration = dt.datetime.now() - starttime
print('\nYour filtered data is now ready to analyze. This took {:.1f} seconds.\n\n'.format(duration.total_seconds()))
print('-'*40)
return rentals
def maxDictVal(dt):
'''
Finds the key with the max integer value inside a dictionary and returns it
Note: found on Stackoverflow - https://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary
'''
try:
topKey = max(dt.items(),key=operator.itemgetter(1))[0]
except:
topKey = -1
return topKey
def findColumn(value, row):
'''
Used to find a coulumn inside the header row, just in case it could be reordered
'''
i = 0
for r in row:
for col in r:
if col.lower() == value.lower():
return i
i += 1
def time_stats(df):
"""
Displays statistics on the most frequent times of travel.
Note: time conversion found on Tutorialspoint - https://www.tutorialspoint.com/python/time_strptime.htm
"""
clear_screen()
start_time = time.time()
act = 1
last = len(df) -1
print('\nCalculating The Most Frequent Times of Travel...\n')
print_progress(0, last, 'Progress: ', 'Complete', 1, 50)
months = {}
weekdays = {}
hours = {}
timeCol = findColumn('start time', df[0])
# get the top times
while act < last:
print_progress(act + 1, last, 'Progress: ', 'Complete', 1, 50)
actDT = dt.datetime.strptime(df[act][timeCol],DATE_FORMAT)
actMonth = actDT.month
actWeekday = actDT.isoweekday()
actHour = actDT.hour
if actMonth in months:
months[actMonth] += 1
else:
months[actMonth] = 1
if actWeekday in weekdays:
weekdays[actWeekday] += 1
else:
weekdays[actWeekday] = 1
if actHour in hours:
hours[actHour] += 1
else:
hours[actHour] = 1
act += 1
topMonth = maxDictVal(months)
strTopMonth = dict_reverse(topMonth, MONTHS)
topWeekday = maxDictVal(weekdays)
strTopWeekday = dict_reverse(topWeekday, DAYS)
topHour = maxDictVal(hours)
print('Ready. Here are the Most Frequent Times:')
# display the most common month
print('Most common month: {}'.format(strTopMonth.title()))
# display the most common day of weekday
print('Most common weekday: {}'.format(strTopWeekday.title()))
# display the most common start hour
print('Most common hour: {:2d}:00'.format(topHour))
print("\nThis took {:.2f} seconds.".format((time.time() - start_time)))
print('-'*40)
def combine_stations(startStation, endStation):
'''
Combines the given start and end station to one single string
'''
tempList = []
tempList.append(startStation)
tempList.append(endStation)
tempString = ''
for Station in sorted(tempList):
tempString = tempString + Station + ' / '
tempLen = len(tempString) - 3
tempString = tempString[:tempLen]
return tempString
def station_stats(df):
"""Displays statistics on the most popular stations and trip."""
# clear_screen()
print('\nCalculating The Most Popular Stations and Trip...\n')
start_time = time.time()
act = 1
last = len(df) -1
print_progress(0, last, 'Progress: ', 'Complete', 1, 50)
startStations = {}
endStations = {}
combined = {}
iStartStation = findColumn('Start Station',df[0])
iEndStation = findColumn('End Station', df[0])
while act < last:
print_progress(act+1, last, 'Progress: ', 'Complete', 1, 50)
actStart = df[act][iStartStation]
actEnd = df[act][iEndStation]
if actStart in startStations:
startStations[actStart] += 1
else:
startStations[actStart] = 1
if actEnd in endStations:
endStations[actEnd] += 1
else:
endStations[actEnd] = 1
combStation = combine_stations(actStart, actEnd)
if combStation in combined:
combined[combStation] += 1
else:
combined[combStation] = 1
act += 1
topStart = maxDictVal(startStations)
topEnd = maxDictVal(endStations)
topCombined= maxDictVal(combined)
print('The station statistics are ready now. Here are the most commonly used stations:')
# display most commonly used start station
print('Start Station: {}'.format(topStart))
# display most commonly used end station
print('End Station: {}'.format(topEnd))
# display most frequent combination of start station and end station trip
print('Station Combination: {}'.format(topCombined))
print("\nThis took {:.2f} seconds.".format((time.time() - start_time)))
print('-'*40)
def trip_duration_stats(df):
"""Displays statistics on the total and average trip duration."""
#clear_screen()
print('\nCalculating Trip Duration...\n')
start_time = time.time()
act = 1
last = len(df) -1
totalSecs = 0
iDuration = findColumn('Trip Duration', df[0])
print_progress(0, last, 'Progress: ', 'Complete', 1, 50)
while act < last:
print_progress(act+1, last, 'Progress: ', 'Complete', 1, 50)
actSecs = df[act][iDuration]
totalSecs += float(actSecs)
act += 1
meanSecs = 0
if last > 0:
meanSecs = totalSecs / last
tSec = dt.timedelta(seconds=totalSecs)
mSec = dt.timedelta(seconds=meanSecs)
print('Time statistics are ready now. Here are some travel times:')
# display total travel time
print('Total travel time: {}'.format(tSec))
# display mean travel time
print('Mean travel time: {}'.format(mSec))
print("\nThis took {:.2f} seconds.".format((time.time() - start_time)))
print('-'*40)
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
bar_length - Optional : character length of bar (Int)
"""
str_format = "{0:." + str(decimals) + "f}"
percents = str_format.format(100 * (iteration / float(total)))
filled_length = int(round(bar_length * iteration / float(total)))
bar = '█' * filled_length + '-' * (bar_length - filled_length)
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()
def user_stats(df):
"""
Displays statistics on bikeshare users.
Note: Nonetype check found on Stackoverflow - https://stackoverflow.com/questions/23086383/how-to-test-nonetype-in-python
"""
print('\nCalculating User Stats...\n')
start_time = time.time()
act = 1
last = len(df) -1
print_progress(0, last, 'Progress: ', 'Complete', 1, 50)
Users = {}
Genders = {}
BirthYears = {}
iUser = findColumn('User Type', df[0])
iGender = findColumn('Gender', df[0])
iBirth = findColumn('Birth Year', df[0])
#Check for Nonetype added to avoid errors in washington where some data is missing
CalcUser = True
CalcGender = True
CalcBirth = True
if iUser is None:
CalcUser = False
if iGender is None:
CalcGender = False
if iBirth is None:
CalcBirth = False
while act < last:
print_progress(act+1, last, 'Progress: ', 'Complete', 1, 50)
#Calc Userstats
if CalcUser:
actUserType = df[act][iUser]
if actUserType in Users:
Users[actUserType] += 1
else:
Users[actUserType] = 1
#Calc Genderstats
if CalcGender:
actGender = df[act][iGender]
if actGender in Genders:
Genders[actGender] += 1
else:
Genders[actGender] = 1
#Calc Year of Birth stats
if CalcBirth:
actBirth = df[act][iBirth]
if actBirth in BirthYears:
BirthYears[actBirth] += 1
else:
BirthYears[actBirth] = 1
#count up for next line
act += 1
if CalcBirth:
topBirth = maxDictVal(BirthYears)
if topBirth == '':
topBirth = 'Unknown'
else:
topBirth = int(topBirth)
mostRecent = 0.0
earliest = 999999.0
try:
for Year in BirthYears:
if not Year == '':
if float(Year) < earliest:
earliest = float(Year)
if float(Year) > mostRecent:
mostRecent = float(Year)
except:
print('What happened? Year: {}'.format(Year))
print('User statistics are ready now. Here some user related information:')
# Display counts of user types
if CalcUser:
print('Count of different user types: {}'.format(len(Users)))
for User in Users:
print('Type {} has {} rental entrys.'.format(User, Users[User]))
# Display counts of gender
if CalcGender:
print('Count of genders: {}'.format(len(Genders)))
for Gender in Genders:
print('There were {} rentals from {} users.'.format(Genders[Gender], Gender))
# Display earliest, most recent, and most common year of birth
if CalcBirth:
print('And a few age statistics...')
print('Earliest year of birth: {}'.format(int(earliest)))
print('Most recent year of birth: {}'.format(int(mostRecent)))
print('Most common year of birth: {}'.format(topBirth))
print("\nThis took {:.2f} seconds.".format((time.time() - start_time)))
print('-'*40)
def printRaw(df):
'''
Print five raw data lines as long as the user retypes 'yes'
'''
showraw = input('\nWould you like to see some raw data? Enter yes or no.\n')
actRaw = 1
noStop = False
if showraw.lower() == 'yes':
noStop = True
while noStop:
stopRaw = actRaw + 5
if stopRaw > len(df) -1:
stopRaw = len(df) -1
while actRaw < stopRaw:
print(df[actRaw])
actRaw += 1
showraw = input('\nDo you want more? Enter yes or no.\n')
noStop = False
if showraw.lower() == 'yes':
noStop = True
def main():
'''
Main function, calls all othe functions and loops until the user decides to quit
'''
while True:
#Check if the user wants to continue before jumping in
if not welcome():
break
#Let's go, first set filters by user inputs
city, month, day = get_filters()
#Now let's load the data filtered by the userinputs before
df = load_data(city, month, day)
#Calculate some statistics
time_stats(df)
station_stats(df)
trip_duration_stats(df)
user_stats(df)
#Print raw data
printRaw(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main() # call main, start loop