Reputation: 1840
I'm using argparse
to take input and pass it to a function that takes as arguments two variables and **kwargs
.
Here's my function:
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
Here's how I'm building the command line parser:
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()
When I try the following:
$ python argstest.py location_by_coordinate 40.5949799 -73.9495148
I get a correct response from the GET call I make to API endpoint with the function, with all sorts of data, that looks like this:
{"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"}]}
However, at the very end, after the data, I get this error:
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
What's going on here? Did I not correctly setup my subparsers? What should I do differently?
Upvotes: 0
Views: 55
Reputation: 6921
it seems that your parser is fine (you can just print the arguments), but your function location_by_coordinate does not return tuple, that why you get this error.
You can always split the line LAT, LNG = location_by_coordinate(**arguments)
into (for debugging):
print(arguments)
res = location_by_coordinate(**arguments)
print(res)
LAT, LNG = res
to find out what happening
Upvotes: 1