i have a function to get a weather report inside of a file 'weather.json' i dont need to open and read the file and to be honest it would be rather easy but some test cases dont have a matching dictionary key, in these cases i need to use the most recent timestamp before it and pull that one instead. any idea on how i would start this??
#π help getting started with a confusing function
8 messages Β· Page 1 of 1 (latest)
@cyan axle
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
specification
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
"""
Check if the timestamp is available. If not, create a temp variable and iterate through the .json file (which is basically a dictionary). Everytime the timestampt of the current iteration is newer than the one in the temp variable, replace it.
alright
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.