Question
I am struggling to implement the following function: def list_weather_violations(directory): Returns the (annotated) list of flight reservations that violate weather minimums. This function reads
I am struggling to implement the following function:
def list_weather_violations(directory):
"""
Returns the (annotated) list of flight reservations that violate weather minimums.
This function reads the data files in the given directory (the data files are all
identified by the constants defined above in this module). It loops through the
list of flight lessons (in lessons.csv), identifying those takeoffs for which
get_weather_violation() is not the empty string.
This function returns a list that contains a copy of each violating lesson, together
with the violation appended to the lesson.
Example: Suppose that the lessons
S00687 548QR I061 2017-01-08T14:00:00-05:00 2017-01-08T16:00:00-05:00 VFR Pattern
S00758 548QR I072 2017-01-08T09:00:00-05:00 2017-01-08T11:00:00-05:00 VFR Pattern
S00971 426JQ I072 2017-01-12T13:00:00-05:00 2017-01-12T15:00:00-05:00 VFR Pattern
violate for reasons of 'Winds', 'Visibility', and 'Ceiling', respectively (and are the
only violations). Then this function will return the 2d list
[[S00687, 548QR, I061, 2017-01-08T14:00:00-05:00, 2017-01-08T16:00:00-05:00, VFR, Pattern, Winds],
[S00758, 548QR, I072, 2017-01-08T09:00:00-05:00, 2017-01-08T11:00:00-05:00, VFR, Pattern, Visibility],
[S00971, 426JQ, I072, 2017-01-12T13:00:00-05:00, 2017-01-12T15:00:00-05:00, VFR, Pattern, Ceiling]]
REMEMBER: VFR flights are subject to minimums with VMC in the row while IFR flights
are subject to minimums with IMC in the row. The examples above are all VFR flights.
If we changed the second lesson to
S00758, 548QR, I072, 2017-01-08T09:00:00-05:00, 2017-01-08T11:00:00-05:00, IFR, Pattern
then it is possible it is no longer a visibility violation because it is subject to
a different set of minimums.
Parameter directory: The directory of files to audit
Precondition: directory is the name of a directory containing the files 'daycycle.json',
'weather.json', 'minimums.csv', 'students.csv', and 'lessons.csv'
""" def list_weather_violations(directory):
"""
Returns the (annotated) list of flight reservations that violate weather minimums.
This function reads the data files in the given directory (the data files are all
identified by the constants defined above in this module). It loops through the
list of flight lessons (in lessons.csv), identifying those takeoffs for which
get_weather_violation() is not the empty string.
This function returns a list that contains a copy of each violating lesson, together
with the violation appended to the lesson.
Example: Suppose that the lessons
S00687 548QR I061 2017-01-08T14:00:00-05:00 2017-01-08T16:00:00-05:00 VFR Pattern
S00758 548QR I072 2017-01-08T09:00:00-05:00 2017-01-08T11:00:00-05:00 VFR Pattern
S00971 426JQ I072 2017-01-12T13:00:00-05:00 2017-01-12T15:00:00-05:00 VFR Pattern
violate for reasons of 'Winds', 'Visibility', and 'Ceiling', respectively (and are the
only violations). Then this function will return the 2d list
[[S00687, 548QR, I061, 2017-01-08T14:00:00-05:00, 2017-01-08T16:00:00-05:00, VFR, Pattern, Winds],
[S00758, 548QR, I072, 2017-01-08T09:00:00-05:00, 2017-01-08T11:00:00-05:00, VFR, Pattern, Visibility],
[S00971, 426JQ, I072, 2017-01-12T13:00:00-05:00, 2017-01-12T15:00:00-05:00, VFR, Pattern, Ceiling]]
REMEMBER: VFR flights are subject to minimums with VMC in the row while IFR flights
are subject to minimums with IMC in the row. The examples above are all VFR flights.
If we changed the second lesson to
S00758, 548QR, I072, 2017-01-08T09:00:00-05:00, 2017-01-08T11:00:00-05:00, IFR, Pattern
then it is possible it is no longer a visibility violation because it is subject to
a different set of minimums.
Parameter directory: The directory of files to audit
Precondition: directory is the name of a directory containing the files 'daycycle.json',
'weather.json', 'minimums.csv', 'students.csv', and 'lessons.csv'
"""
I am trying to call this helper function: def get_weather_report(takeoff,weather):
"""
Returns the most recent weather report at or before take-off.
The weather is a dictionary whose keys are ISO formatted timestamps and whose values
are weather reports. For example, here is an example of a (small portion of) a
weather dictionary:
{
"2017-04-21T08:00:00-04:00": {
"visibility": {
"prevailing": 10.0,
"units": "SM"
},
"wind": {
"speed": 13.0,
"crosswind": 2.0,
"units": "KT"
},
"temperature": {
"value": 13.9,
"units": "C"
},
"sky": [
{
"cover": "clouds",
"type": "broken",
"height": 700.0,
"units": "FT"
}
],
"code": "201704211056Z"
},
"2017-04-21T07:00:00-04:00": {
"visibility": {
"prevailing": 10.0,
"units": "SM"
},
"wind": {
"speed": 13.0,
"crosswind": 2.0,
"units": "KT"
},
"temperature": {
"value": 13.9,
"units": "C"
},
"sky": [
{
"type": "overcast",
"height": 700.0,
"units": "FT"
}
],
"code": "201704210956Z"
}
...
},
If there is a report whose timestamp matches the ISO representation of takeoff,
this function uses that report. Otherwise it searches the dictionary for the most
recent report before (but not equal to) takeoff. If there is no such report, it
returns None.
Example: If takeoff was as 8 am on April 21, 2017 (Eastern), this function returns
the value for key '2017-04-21T08:00:00-04:00'. If there is no additional report at
9 am, a 9 am takeoff would use this value as well.
Parameter takeoff: The takeoff time
Precondition: takeoff is a datetime object
Paramater weather: The weather report dictionary
Precondition: weather is a dictionary formatted as described above
"""
# HINT: Looping through the dictionary is VERY slow because it is so large
# You should convert the takeoff time to an ISO string and search for that first.
# Only loop through the dictionary as a back-up if that fails.
# Search for time in dictionary
# As fall back, find the closest time before takeoff
takeoff_iso = takeoff.isoformat()
if takeoff_iso in weather:
return weather[takeoff_iso]
for time in weather.keys():
if time < takeoff_iso:
return weather[time]
return None and receiving the error i've posted in the picture
Collapse 4. The Weather Violations Functions 31. Implement list_weather_vio... is not friendly to flying). Check the Function You may run this test multiple times (but run the test script first). This will take a while! Check It! LAST RUN on 4/10/2023, 7:33:41 PM The call list_weather_violations ('/home/codio/workspace/.guides/tests/1 crashed. Traceback (most recent call last): File "/home/codio/workspace/.guides/tests/step3/verifier.py", line 753, in grade_func6 results = func(tdir) File "/home/codio/workspace/auditor/violations.py", line 493, in list_weather_violations weather_cond = get_weather_report(takeoff, weather) "/home/codio/workspace/auditor/violations.py", File line 317, in get_weather_report takeoff_iso = takeoff.isoformat() AttributeError: 'str' object has no attribute 'isoformat' 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 # For each of the lessons pilot_min= {} for row in students: pilot_min[row [0]] = row [2:] min_cond = {"VMC": [], "IMC": []} for row in minimums: if row [0] == "VMC": min_cond ["VMC"] = row [1:] min_cond ["IMC"] = row [1:] else: violations = [] upp.py for i in lessons: # Get the takeoff time takeoff=i[3] # Get the pilot credentials student_id = i[1] # Get the weather conditions weather_cond = get_weather_report (takeoff, weather) # Get the pilot minimums pilot_min_id = pilot_min[student_id] weather_min = min_cond [weather_cond] # Check for a violation and add to result if so violation = get_weather_violation (pilot_min_id, weather_min) if violation: violations.append(i + [violation]) return violations
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started