0

I'm new to python and as far as i can tell i'm recieving this Tuple Error:

Traceback (most recent call last):
  File "/home/cambria/Main.py", line 10, in <module>
    main()
  File "/home/cambria/Main.py", line 5, in main
    respons3 = api.get_summoner_by_name('hi im gosan')
  File "/home/cambria/RiotAPI.py", line 31, in get_summoner_by_name
    return self._request(api_url)
  File "/home/cambria/RiotAPI.py", line 12, in _request
    for key, value in params.items():
AttributeError: 'tuple' object has no attribute 'items'

in

def _request(self, api_url, params=()):
        args = {'api_key': self.api_key}
        for key, value in params.items():
            if key not in args:
                args[key] = value
        response = requests.get(
            Consts.URL['base'].format(
                proxy=self.region,
                region=self.region,
                url=api_url
                ),
            params=args
            )
        print response.url
        return response.json()

this is the only error i have received that i really don't know much on. Is this a result of there being no .items on my params? or i left it initialized as an empty dictionary?

EDIT 1: Have tried tinkering with the Tuple and items thing but just not having any luck, my error message is as follows

Traceback (most recent call last):
  File "/home/cambria/Desktop/api/Main.py", line 10, in <module>
    main()
  File "/home/cambria/Desktop/api/Main.py", line 5, in main
    respons3 = api.get_summoner_by_name('hi im gosan')
  File "/home/cambria/Desktop/api/RiotAPI.py", line 33, in get_summoner_by_name
    return self._request(api_url)
  File "/home/cambria/Desktop/api/RiotAPI.py", line 23, in _request
    params=args
  File "/home/cambria/.local/lib/python2.7/site-packages/requests/api.py", line 69, in get
    return request('get', url, params=params, **kwargs)
  File "/home/cambria/.local/lib/python2.7/site-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/home/cambria/.local/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/cambria/.local/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/home/cambria/.local/lib/python2.7/site-packages/requests/adapters.py", line 415, in send
    raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))
>>>

as far as i can tell and have searched, this is a result of the python REQUESTS not going through completely? and my new code as follows,

  def _request(self, api_url, params=None): #edit
        if params is None:                  #edit
            params = {}                     #edit
        args = {'api_key': self.api_key}
        for key, value in params.items(): #remove?? since there is no .items()?
            if key not in args:
                args[key] = value
        response = requests.get(
            Consts.URL['base'].format(
                proxy=self.region,
                region=self.region,
                url=api_url
                ),
            params=args
            )
        print response.url
        return response.json()

2 Answers 2

1

Your code expects params to be a dict {} (or have a .items method). You've passed a tuple (). The two are not equivalent.

Set params to None by default, and pass a when needed.

def _request(self, api_url, params=None):
    params = params if params is not None else {}
    ...

Or expect a list of tuples, rather than a dict.

def _request(self, api_url, params=()):
    for key, value in params:
    ...
Sign up to request clarification or add additional context in comments.

Comments

1

A tuple () doesn't have a method named items. You probably mistake that for dictionary. Change you code as follows:

def _request(self, api_url, params=None):
    if params is None:
        params = {}
    ...

2 Comments

Hello, sorry for the late reply, posted this late last night. I have tried tinkering with your answer but i'm having no luck and am getting a giant error message that i will post in my OP edit 1
would you eliminate .items() since there is no method? but when i do, i just get this request error that i listed in the OP

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.