在函数之间传递字典值

Python 3.8和Windows 10

大家好,我是Python的新手,敬请谅解:)

在与busAPI交互的一段代码编写的最后阶段,我遇到了问题。 (不,我不是公交车检举员)

到目前为止,我已经从用户那里获取了数据

验证服务是我们支持的一项

检索有关服务的相关数据

检索服务的路线信息,并将其传递给我打算对其进行映射的最后一部分。

不幸的是,它只画了一个点,终点

随着大量调试日志的进行,我可以看到函数交互,因此我可以看到函数之间的数据流。

    ....
    ....
    1
    DEBUG 4: Alyssum Walk , 51.89186 , 0.93168
    DEBUG 5: Alyssum Walk , 51.89186 , 0.93168
    DEBUG 5.1: map_it_dict{'stop': 'Alyssum Walk', 'latitude': 51.89186, 'longitude': 0.93168}
               stop  latitude  longitude
    0  Alyssum Walk  51.89186    0.93168
    1
    DEBUG 4: Azalea Court , 51.89278 , 0.93334
    DEBUG 5: Azalea Court , 51.89278 , 0.93334
    DEBUG 5.1: map_it_dict{'stop': 'Azalea Court', 'latitude': 51.89278, 'longitude': 0.93334}
               stop  latitude  longitude
    0  Azalea Court  51.89278    0.93334
    1
    DEBUG 4: Library , 51.89165 , 0.93706
    DEBUG 5: Library , 51.89165 , 0.93706
    DEBUG 5.1: map_it_dict{'stop': 'Library', 'latitude': 51.89165, 'longitude': 0.93706}
          stop  latitude  longitude
    0  Library  51.89165    0.93706
    1
    DEBUG 0: Returned from  bus_route(). Last Stop is Library , 51.89165 , 0.93706

从上面可以看出,我在所有函数和Pandas数据框中都获得了一致的数据。

调试0是主要功能

调试4-来自我的功能总线路由。这将查询URL,并返回路线上的半身站。该信息与公交车站的名称以及公交车站的经纬度一起提供。 busroute函数的最后一部分将lat和long的公共汽车站传递给Map_it函数

调试5-向我们展示了它正在从Bus_route函数接收相同的数据。最后,在第19行,您可以再次看到pandas数据框,其中包含相同的信息。

)是我的熊猫数据框架中的索引 1是当时字典中的项目数(总路线为53个项目)

我的map_it函数中输入的数据应在整个草地地图上绘制52个公交车站的位置。但它仅映射最后一个。

Folium map showing only one marker, should be 53

我认为发生这种情况是因为传递给map_it函数的数据是逐行传递的,我怀疑每次都重写字典。因此,0对数据报的索引。

我在bus_route函数中获取了所有数据”

    What bus do you want?: 64
    The number 64 is a bus service we support
    DEBUG 0: From validate_bus() we got 64
    DEBUG 0: From bus_service(): Bus Number 64 , Traveling from: Greenstead, Essex, Traveling to: Shrub End, Essex
    DEBUG 4: Hazell Avenue , 51.87154 , 0.86659
    DEBUG 4: Paxman Avenue , 51.87186 , 0.86827
    DEBUG 4: Alderman Blaxhill School , 51.87236 , 0.87038
    ...
    ...
    ...
    DEBUG 4: Alyssum Walk , 51.89186 , 0.93168
    DEBUG 4: Azalea Court , 51.89278 , 0.93334
    DEBUG 4: Library , 51.89165 , 0.93706
    DEBUG 0: Returned from  bus_route(). Last Stop is Library , 51.89165 , 0.93706

这是我的两个功能的代码。

    def bus_route(bus_number):
        # This function queries the transport API for route data for a specific bus service number
        # It receives input as bus_number from main()
        # it provides bus_stand, lat and long of each of the stops along the specific buses route
        # and passes them to map_it() for route mapping.

        bus = bus_number
        # Retrieve a URL via urllib3
        # This could be tidied up using data from URL but for expediancy it is coded in here
        # Use %s to pass in the Constants and Variables to make up the URL
        if bus == "64":
            url = BASE_URL + '/route/FESX/%s/inbound/1500IM2349B/2020-05-22/06:40/timetable.json?app_id=%s&app_key=%s' \
                             '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
        elif bus == "65":
            url = BASE_URL + '/route/FESX/%s/inbound/1500IM2456B/2020-05-22/19:27/timetable.json?app_id=%s&app_key=%s' \
                             '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
        elif bus == "67":
            url = BASE_URL + '/route/FESX/%s/inbound/150033038003/2020-05-22/06:55/timetable.json?app_id=%s&app_key=%s' \
                             '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
        elif bus == "70":
            url = BASE_URL + '/route/FESX/%s/inbound/1500IM77A/2020-05-22/06:51/timetable.json?app_id=%s&app_key=%s' \
                             '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
        elif bus == "74B":
            url = BASE_URL + '/route/FESX/%s/inbound/15003303800B/2020-05-22/20:10/timetable.json?app_id=%s&app_key=%s' \
                             '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
        elif bus == "88":
            url = BASE_URL + '/route/FESX/%s/inbound/1500IM77A/2020-05-22/05:50/timetable.json?app_id=%s&app_key=%s' \
                             '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
        else:
            url = BASE_URL + '/route/FESX/%s/inbound/1500IM52/2020-05-22/06:00/timetable.json?app_id=%s&app_key=%s' \
                             '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)

        http = urllib3.PoolManager()

        # Request our data, and decode the json data returned
        response = http.request('GET', url)
        bus_route_dict = json.loads(response.data.decode('utf-8'))
        x = 0
        # iterate through our dictionary giving us the bus stop names and their
        # lat and long so we can plot them on a map.
        #while x < len(bus_route_dict['stops']):
        #    print(x)
        for stop in bus_route_dict['stops']:
            bus_stand = stop['stop_name']
            lat = stop['latitude']
            long = stop['longitude']
            print("DEBUG 4: " + bus_stand + " , " + str(lat) + " , " + str(long))
            #map_it(bus_stand, lat, long)
        return bus_stand, lat, long

    def map_it(bus_stand, lat, long):
        # This function maps teh bus route on a folium map
        # It receives input as bus_stand, lat, and long from bus_route

        # Folium mapping
        stop = bus_stand
        latitude = lat
        longitude = long

        # DEBUG code to show we are receiving code from get_route()
        print("DEBUG 5: " + stop + " , " + str(latitude) + " , " + str(longitude))

        # Setup a dictionary to store the information we need to build
        # a folium map
        map_it_dict = {}
        map_it_dict['stop'] = stop
        map_it_dict['latitude'] = latitude
        map_it_dict['longitude'] = longitude
        print("DEBUG 5.1: map_it_dict" + str(map_it_dict))

        # lets get the dict into pandas
        map_it_df = pd.DataFrame([map_it_dict])
        # Check we got data - we get it. tw 22/05/2020
        #print(map_it_df.head())

        # Prep data for the map
        locations = map_it_df[['latitude', 'longitude']]
        locationlist = locations.values.tolist()
        print(len(locationlist))

        # Now build the map
        # the Location is the Lat/Long for Colchester
        route_64 = folium.Map(location=[51.8959,0.8919] , zoom_start=14)
        for point in range(0, len(locationlist)):
            folium.Marker((locationlist[point]) , popup=map_it_dict['stop']).add_to(route_64)
            route_64.save("route_maps/route_64.html ")

我看不到在哪里创建字典,因为我似乎在bus_route()函数中看到的只是单行。我意识到,如果我可以将打印语句附加到字典中,那将是我解决的问题。

评论