Argparse返回预期数据,但也返回ValueError

时间:2015-11-14 07:32:21

标签: python python-2.7 argparse

我正在使用argparse来获取输入并将其传递给一个函数,该函数将两个变量和**kwargs作为参数。

这是我的功能:

def location_by_coordinate(LAT, LNG, **kwargs):
    if not kwargs:
        coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&lng=%s&access_token=%s" % (LAT, LNG, current_token)
        r = requests.get(coordinate_url).text
    else:
        coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&lng=%s&access_token=%s" % (LAT, LNG, current_token)
        for key, value in kwargs.iteritems():
            if 'DISTANCE' in kwargs:
                distance = kwargs.get('DISTANCE')
                if distance > 5000:
                    print distance
                    print "max distance is 5000m, value is reassigned to default of 1000m"
                    distance = 1000
                    coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&lng=%s&access_token=%s" % (LAT, LNG, current_token)
                    r = requests.get(coordinate_url).text
                else:
                    pass
                    coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&lng=%s&access_token=%s" % (LAT, LNG, current_token)
                    r = requests.get(coordinate_url).text
            if 'FACEBOOK_PLACES_ID' in kwargs:
                fb_places_id = kwargs.get('FACEBOOK_PLACES_ID')
                payload = {'FACEBOOK_PLACES_ID': '%s' % (fb_places_id), 'DISTANCE': '%s' % (DISTANCE)}
                r = requests.get(coordinate_url, params=payload).text
            if 'FOURSQUARE_ID' in kwargs:
                foursquare_id = kwargs.get('FOURSQUARE_ID')
                payload = {'FOURSQUARE_ID': '%s' % (foursquare_id), 'DISTANCE': '%s' % (DISTANCE)}
                r = requests.get(coordinate_url, params=payload).text
            if 'FOURSQUARE_V2_ID' in kwargs:
                foursquare_v2_id = kwargs.get('FOURSQUARE_V2_ID')
                payload = {'FOURSQUARE_V2_ID': '%s' % (foursquare_v2_id), 'DISTANCE': '%s' % (DISTANCE)}
                r = requests.get(coordinate_url, params=payload).text
    #print r
    return r

以下是我构建命令行解析器的方法:

def main():
    parser = argparse.ArgumentParser(description="API Endpoints tester")
    subparsers = parser.add_subparsers(dest="command", help="Available commands")

    location_by_parser = subparsers.add_parser("location_by_coordinate", help="location function")
    location_by_parser.add_argument("LAT", help="latitude")
    location_by_parser.add_argument("LNG", help="longitude")
    arguments = parser.parse_args(sys.argv[1:])
    arguments = vars(arguments)
    command = arguments.pop("command")
    if command == "location_by_coordinate":
        LAT, LNG = location_by_coordinate(**arguments)
    else:
        print "No command provided..."

if __name__ == "__main__":
    main()

当我尝试以下操作时:

$ python argstest.py location_by_coordinate 40.5949799 -73.9495148

我从使用函数的API端点向GET调用得到了正确的响应,其中包含各种类型的数据,如下所示:

{"meta":{"code":200},"data":[{"latitude":40.595094814,"id":"40169670","longitude":-73.94971014,"name":"2203 Avenue X"},{"latitude":40.595371721,"id":"540102881","longitude":-73.949681161,"name":"America's Keswick"},{"latitude":40.5951,"id":"853669456","longitude":-73.94877,"name":"Little Neck, Long Island"},{"latitude":40.594903,"id":"122281671","longitude":-73.950292,"name":"L A Liquor"},{"latitude":40.594988731,"id":"1015076504","longitude":-73.950396851,"name":"Ballys Gym"},.........
{"latitude":40.594335241,"id":"570423173","longitude":-73.950273475,"name":"Art House Restaurant"}]}

然而,最后,在数据之后,我收到了这个错误:

Traceback (most recent call last):
  File "argstest.py", line 117, in <module>
    main()
  File "argstest.py", line 108, in main
    LAT, LNG = location_by_coordinate(**arguments)
ValueError: too many values to unpack

这里发生了什么?我没有正确设置我的子分析器吗?我该怎么办?

1 个答案:

答案 0 :(得分:1)

似乎您的解析器很好(您可以只打印参数),但您的函数location_by_coordinate不会返回元组,这就是您收到此错误的原因。 您始终可以将行LAT, LNG = location_by_coordinate(**arguments)拆分为(用于调试):

print(arguments)
res =  location_by_coordinate(**arguments) 
print(res)
LAT, LNG = res

找出发生的事情