Dict object has no attribute 'rstrip'

1,708 views
Skip to first unread message

adohertyd

unread,
Jul 5, 2012, 9:55:06 AM7/5/12
to web...@googlegroups.com
Me again! I don't know why I am getting this error. The process() function takes in a value session.term, processes it, and returns it as a dict type so that it can be mapped to URL's in the next function. The results() function sends the variable 'term' to 3 different API URL's which all should return some json results. The entireweb and blekko results come back fine if I leave out the bing call; however, the bing results cause the error 'Dict object has no attribute rstrip'. The problem has to be in the way I'm calling the Bing API. Would really appreciate some guidance here. Thanks! This is my default.py controller

def index():
    form
= FORM('',
            INPUT
(_name='query', requires=IS_NOT_EMPTY()),
            INPUT
(_type='submit'))
   
if form.process().accepted:
        session
.term=request.vars.query
        redirect
(URL('results'))
   
elif form.errors:
        response
.flash = 'form has errors'
   
else:
        response
.flash = 'please fill the form'
   
return dict(form=form)
   
def __process():
   
import urllib
    term
=session.term
   
#do some processing on term
   
return dict(term=term)
   
def results():
   
import urllib
   
import requests #This is an external library in the site-packages folder
   
    blekko
= "http://www.blekko.com/?q=%(term)s+/json&auth=f4c8acf3" % __process()
    blekkoresults
= urllib.urlopen(blekko).read()
   
    entireweb
= "http://www.entireweb.com/xmlquery?pz=<mykey>&ip=<myIP>&n=10&of=0&format=json&q=%(term)s" %__process()
    entresults
= urllib.urlopen(entireweb).read()
   
    URL
= "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query=%(term)s&$top=50&$format=json"
    API_KEY
= 'mykey'
    term
= __process()
    r
= requests.get(URL % {'term': urllib.quote(term)}, auth=('', API_KEY))
    bingresults
= r.json['d']['results']
   
   
return blekkoresults, entresults, bingresults





Jonathan Lundell

unread,
Jul 5, 2012, 10:12:20 AM7/5/12
to web...@googlegroups.com
On 5 Jul 2012, at 6:55 AM, adohertyd wrote:
Me again! I don't know why I am getting this error. The process() function takes in a value session.term, processes it, and returns it as a dict type so that it can be mapped to URL's in the next function. The results() function sends the variable 'term' to 3 different API URL's which all should return some json results. The entireweb and blekko results come back fine if I leave out the bing call; however, the bing results cause the error 'Dict object has no attribute rstrip'. The problem has to be in the way I'm calling the Bing API. Would really appreciate some guidance here. Thanks! This is my default.py controller

Where is rstrip being called, and on which variable?

One thing: avoid using URL as a variable name. The symbol is globally defined in web2py as the URL function you're calling in index(). You might be getting away with it here, but best to use another name.

adohertyd

unread,
Jul 5, 2012, 10:23:16 AM7/5/12
to
That's the thing, I don't know where it is being called or why! I haven't specified it, so I'm assuming it's coming from this block of code:


    API_KEY
= 'mykey'
    term
= __process()

    r
= requests.get(bing % {'term': urllib.quote(term)}, auth=('', API_KEY))

    bingresults
= r.json['d']['results']

This line in particular:

r=requests.get(bing % {'term': urllib.quote(term)}, auth=('', API_KEY))

Jonathan Lundell

unread,
Jul 5, 2012, 10:25:50 AM7/5/12
to web...@googlegroups.com
On 5 Jul 2012, at 7:19 AM, adohertyd wrote:
That's the thing, I don't know where it is being called or why! I haven't specified it, so I'm assuming it's coming from this block of code:


    API_KEY= 'mykey'
    term = __process()
    r = requests.get(URL % {'term': urllib.quote(term)}, auth=('', API_KEY))
    bingresults = r.json['d']['results']


Don't you mean something like term = __process().get('term')?

adohertyd

unread,
Jul 5, 2012, 10:47:38 AM7/5/12
to web...@googlegroups.com
No that's not it either Jonathan. The problem I think is with the {'term' : urllib.quote(term)}. I think that because 'term' is a dict type, the urllib.quote is having issues with it.

Jonathan Lundell

unread,
Jul 5, 2012, 10:49:50 AM7/5/12
to web...@googlegroups.com
On 5 Jul 2012, at 7:47 AM, adohertyd wrote:
No that's not it either Jonathan. The problem I think is with the {'term' : urllib.quote(term)}. I think that because 'term' is a dict type, the urllib.quote is having issues with it.

That's what I mean. In the case of blekko and entireweb, you're using (implicitly) term = __process()['term'], but for Bing you're doing term = __process().

adohertyd

unread,
Jul 5, 2012, 11:04:39 AM7/5/12
to web...@googlegroups.com
Oh I see now. Ok tried to implement that still no joy. I'm trying a number of different things still no luck. It's just a matter of perseverance I think!

Jonathan Lundell

unread,
Jul 5, 2012, 11:06:16 AM7/5/12
to web...@googlegroups.com
On 5 Jul 2012, at 8:04 AM, adohertyd wrote:
Oh I see now. Ok tried to implement that still no joy. I'm trying a number of different things still no luck. It's just a matter of perseverance I think!

Show your current code, please. 

adohertyd

unread,
Jul 5, 2012, 11:18:56 AM7/5/12
to web...@googlegroups.com
Ok, I've gotten around that little problem by doing the following:


It works for some reason! The problem now is outputting the json data but that's another beast entirely. Thanks for your time and help Jonathan
Reply all
Reply to author
Forward
0 new messages