Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have included instructions for my assignment below an I have included the code I have written at the bottom. I would appreciate it if

I have included instructions for my assignment below an I have included the code I have written at the bottom. I would appreciate it if someone would review for adherence to the directions and provide a suggestion for the easiest way to fix my timestamp format to look like the example in the directions, and provide a detailed explanation of how to do so.(I have really struggled with this.)

  1. Create a basic class that can hold all relevant Bike data for an individual bike station (see above).
    • As with a database, you don't normally want to have calculated values stored in your objects because it could cause data integrity issues if, for example - you update the available_bikes when a bike is returned but forget to update percent_full.
    • As such, you BikeStation class should only include the data values to uniquely specify the current state (number bikes available, number slots in the station).
  2. Appropriately create a constructor to set all data values .
  3. Use the @property decorator to make at least one property in your BikeStation class private.
  4. Use the @???.setter method to validate the private property in some way (e.g. check if its numeric, change it's data type, change it's length) before setting its value.
  5. Create one or more regular class methods to CALCULATE other relevant bike station values from the data attributes you are storing as a part of the class.
  6. Override the __str__ method to print a string representation of a BikeStation that looks like the String below (note that the format of the date string is different than it is in the data file): Paulina St & Howard St had 10 bikes on Mon Nov 16 11:55:55 2020

Step 3: Write a second class called Point, Position or Time to store one or more pieces of BikeStation data.

  • The class could store latitude & longitude OR timestamp
  • You should use this class in the BikeStation class instead of storing the vales as primitive data types

You may use the Author and Article classes presented in lecture as a starting point for these classes.

Step 4: Testing you classes using data from the data file bdata.txt

  1. Prompt for a file name (bdata.txt):
  2. Open that file and read through the file
  3. Display a custom error message if the file does not exist
  4. Use an appropriate String method find lines for the BikeStations you were assigned - e.g. lines that contain: "id": "###" (where (###) is the number for one of the Bike Stations you were assigned).
  5. Once you have found lines of, you can pull the BikeStation data out from the the line by splitting the line on a ", " and then splitting the string a second time using a colon.
  6. Create a new BikeStation object and add them to a list.
  7. Print the BikeStation Object
  8. You code should be efficient (you should not have to open the file multiple times NOR should you have to loop through the data more than once).
  9. Print out the total number of Bikes available for each of the five Bike Stations you were are gathering data for after the file has been completely read.
  10. Print out the total number of empty bike docks available for each of the five Bike Stations you were are gathering data for after the file has been completely read.

Your output should look something like this:

Paulina St & Howard St had 10 bikes on Mon Nov 16 11:55:55 2020

The following is my code:

fname = input('Enter the file name: ') try: fhand = open(fname) except: print('Error: ',fname, 'cannot be opened.') quit()

for line in fhand: if '"id": "123"'in line: data = line.split(",") statName1 = data[2].split(":") stationName1 = statName1[1].strip('" "') bikes1 = data[6].split(":") bikesAvailable1 = bikes1[1].strip('" "') time = line.split() timestamp = time[3].strip('"",') available1 = data[5].split(":") availableDocks1 = available1[1].strip('" "') num1 = data[0].split(":") statNum1 = num1[1].strip('" "') total1 = data[3].split(":") totalDocks1 = total1[1].strip('" "') ntimestamp = timestamp.split("T")

elif '"id": "258"' in line: data = line.split(",") statName2 = data[2].split(":") stationName2 = statName2[1].strip('" "') bikes2 = data[6].split(":") bikesAvailable2 = bikes2[1].strip('" "') time = line.split() timestamp = time[3].strip('"",') available2 = data[5].split(":") availableDocks2 = available2[1].strip('" "') num2 = data[0].split(":") statNum2 = num2[1].strip('" "') total2 = data[3].split(":") totalDocks2 = total2[1].strip('" "') elif '"id": "290"' in line: data = line.split(",") statName3 = data[2].split(":") stationName3 = statName3[1].strip('" "') bikes3 = data[6].split(":") bikesAvailable3 = bikes3[1].strip('" "') time = line.split() timestamp = time[3].strip('"",') available3 = data[5].split(":") availableDocks3 = available3[1].strip('" "') num3 = data[0].split(":") statNum3 = num3[1].strip('" "') total3 = data[3].split(":") totalDocks3 = total3[1].strip('" "') elif '"id": "501"' in line: data = line.split(",") statName4 = data[2].split(":") stationName4 = statName4[1].strip('" "') bikes4 = data[6].split(":") bikesAvailable4 = bikes4[1].strip('" "') time = line.split() timestamp = time[3].strip('"",') available4 = data[5].split(":") availableDocks4 = available4[1].strip('" "') num4 = data[0].split(":") statNum4 = num4[1].strip('" "') total4 = data[3].split(":") totalDocks4 = total4[1].strip('" "') elif '"id": "502"' in line: data = line.split(",") statName5 = data[2].split(":") stationName5 = statName5[1].strip('" "') bikes5 = data[6].split(":") bikesAvailable5 = bikes5[1].strip('" "') time = line.split() timestamp = time[3].strip('"",') available5 = data[5].split(":") availableDocks5 = available5[1].strip('" "') num5 = data[0].split(":") statNum5 = num5[1].strip('" "') total5 = data[3].split(":") totalDocks5 = total5[1].strip('" "') else: continue fhand.close()

class bikeStation: def __init__(self, statNum, timestamp, stationName, totalDocks, availableDocks, bikesAvailable): self.ID=statNum self.timestamp=timestamp self.StationName=stationName self.TotalDocks=totalDocks self.availableDocks=availableDocks self.availableBikes=bikesAvailable print(stationName, " had", bikesAvailable, " bikes available on", timestamp) @property def StationName(self): return self.__StationName @StationName.setter def StationName(self, x): x = str(x) self.__StationName = x[0:20] def availableDocks(self): self.availableDocks = availableDocks def availableBikes(self): self.availableBikes = availableBikes

bs1 = bikeStation(statNum1, timestamp, stationName1, totalDocks1, availableDocks1, bikesAvailable1) bs2 = bikeStation(statNum2, timestamp, stationName2, totalDocks2, availableDocks2, bikesAvailable2) bs3 = bikeStation(statNum3, timestamp, stationName3, totalDocks3, availableDocks3, bikesAvailable3) bs4 = bikeStation(statNum4, timestamp, stationName4, totalDocks4, availableDocks4, bikesAvailable4) bs5 = bikeStation(statNum5, timestamp, stationName5, totalDocks5, availableDocks5, bikesAvailable5)

statNums = (statNum1, statNum2, statNum3, statNum4, statNum5)

availableDocks = (int(availableDocks1) + int(availableDocks2) + int(availableDocks3) + int(availableDocks4) + int(availableDocks5)) availableBikes = (int(bikesAvailable1) + int(bikesAvailable2) + int(bikesAvailable3) + int(bikesAvailable4) + int(bikesAvailable5))

print("Stations:", statNums, "had", availableDocks, "available docks and", availableBikes, "bikes available.")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago