0

Code:

def user(repo, language, page):
  # import pdb;pdb.set_trace()
  dictionary = {'language_search': language, 'page': page, 'followers': repo['followers'], 'github_repo': repo['name'],'forked?': repo['fork'], 'forks': repo['forks'], 'github_owner': repo['owner'], 'language': repo['language'], 'created_at': repo['created_at'], 'forks': repo['forks'], 'watchers': repo['watchers'], 'username': user['login'], 'type': user['type'], 'public_repos': user['public_repos'], 'followers': user['followers']}
  fields_user = ['blog', 'company', 'location', 'name']
  fields_repo = ['description']
  if user.get('email'):
    dictionary_2 = {'email': user['email']}
    for key in fields_user:
      if user.has_key(key):
        dictionary[key] = user[key]
    for key in fields_repo:
    if repo.has_key(key):
      dictionary[key] = repo[key]
    shelf[dictionary_2['email']] = dictionary
    shelf.sync()
    return dictionary_2['email']
    print dictionary_2['email']

For some reason I keep getting this error:

<ipython-input-40-98ec5463ecbd> in user(repo, language, page)
  1 def user(repo, language, page): #returns email address to pass into rapportive
  2   # import pdb;pdb.set_trace()
----> 3   dictionary = {'language_search': language, 'page': page, 'followers': repo['followers'], 'github_repo': repo['name'],'forked?': repo['fork'], 'forks': repo['forks'], 'github_owner': repo['owner'], 'language': repo['language'], 'created_at': repo['created_at'], 'forks': repo['forks'], 'followers': user['followers']}
  4   # dictionary = {, , 'watchers': repo['watchers'], 'username': user['login']} , 'type': user['type'], , 'public_repos': user['public_repos']
  5   fields_user = ['blog', 'company', 'location', 'name']

TypeError: 'function' object is not subscriptable

And I don't understand why. I just want to write more key:value pairs to dictionary. But for some reason, this keeps throwing off this error.

2
  • Pro tip: Use key in mapping instead of mapping.has_key(key). Don't use mapping.get(key) for tests where key in mapping would do either. Commented Nov 22, 2012 at 17:45
  • Counter down vote, this is a common error and people should find it when they search for it. Commented Nov 22, 2012 at 17:57

1 Answer 1

5

user is a function:

def user(repo, language, page):

but within that function you are trying to use it as a dictionary instead:

 user['followers']

on the line where you define dictionary:

dictionary = {'language_search': language, 'page': page, 'followers': repo['followers'], 'github_repo': repo['name'],'forked?': repo['fork'], 'forks': repo['forks'], 'github_owner': repo['owner'], 'language': repo['language'], 'created_at': repo['created_at'], 'forks': repo['forks'], 'followers': user['followers']}

You can split that line up over several to make it more readable and debuggable:

dictionary = {
    'language_search': language, 
    'page': page,
    'followers': repo['followers'],
    'github_repo': repo['name'],
    'forked?': repo['fork'],
    'forks': repo['forks'],
    'github_owner': repo['owner'],
    'language': repo['language'],
    'created_at': repo['created_at'],
    'forks': repo['forks'],
    'followers': user['followers']  # `user` is a function, what did you mean instead?
}

You seem to expect there to be a user dictionary in your function throughout:

  if user.get('email'):    # Will also throw the error
    dictionary_2 = {'email': user['email']}  # as will this
    for key in fields_user:
      if user.has_key(key):                  # and this
        dictionary[key] = user[key]          # and here

Perhaps you have a global named user that is a dictionary? You cannot have both a dictionary and a function with the same name in your module. Rename one or the other.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.