0

I'm trying to pass an arbitrary number of arguments to a function, and I keep getting errors, and not quite sure where I'm going wrong. This is the first time I've attempted to use **kwargs.

More specifically, I'm trying to use the Facepy library to get data from Facebook's Graph API. According to the documentation (https://facepy.readthedocs.org/en/latest/usage/graph-api.html), the get method should accept optional parameters like "since", "until", etc. Since I only want to pass some of these parameters on any given query, this seems like an ideal time to use **kwargs.

First I create a function that wraps the Facepy library:

def graph_retriever(object_id, metric_url, **kwargs): 
    #optional args that I want to pass include since, until, and summary
    graph = facepy.GraphAPI(access_token)
    retries = 3 # An integer describing how many times the request may be retried.
    object_data = graph.get('%s/%s' % (object_id, metric_url), False, retries, **kwargs)
    return object_data

Here's two examples where I call the function with different arguments:

for since, until in day_segmenter(start, end): # loop through the date range
    raw_post_data = graph_retriever(str(page), 'posts', {'since': since, 'until': until})

post_dict['comment_count'] = graph_retriever(post_id, 'comments', {'summary':1})['summary']['total_count']

However, when I try to run this, I get the following error:

Traceback (most recent call last): raw_post_data = graph_retriever(str(page), 'posts', {'since': since, 'until': until}) TypeError: graph_retriever() takes exactly 2 arguments (3 given)

What am I doing wrong?

Jeff Widman
  • 22,014
  • 12
  • 72
  • 88

1 Answers1

0

You can pass a dictionary as kwargs by unpacking it using **

graph_retriever(str(page), 'posts', **{'since': since, 'until': until})

EDIT 1:

kwargs must actually be passed as key word args ie the function should be called as

graph_retriever(str(page), 'posts', since= since, until= until)

see here for the docs Python Kwargs

Pruthvikar
  • 430
  • 4
  • 15
  • calling it in this format worked for me: `graph_retriever(str(page), 'posts', **{'since': since, 'until': until})` <--the part I was missing was the ** when calling the function Afraid I don't understand your edit though. – Jeff Widman Nov 07 '13 at 06:34