0

I am developing an application that has a website and a series of keywords and queries the Google API at what position they are up to page 5.

For example:

Website: `www.stackoverflow.com`
Keywords: `web design`, `programming`, `help`

This will search up to the 5th page on Google and if it's not found then it will create an object with the keyword and the position would be "n/a".

I have created the following recusive function:

def getRankingsFromGoogle(keyword, website, page = 1, count = 1): 

    if count is 5: 
       print "NOT ON PAGE 5" 

    service = build("customsearch", "v1",
              developerKey="")

    res = service.cse().list(
        q=keyword, 
        cx='',
        start=page, 
    ).execute()

    obj = parseGoogleResponse(res, keyword, website)

    if not bool(obj):
        print "adding object to database.."
        return 
    else:
        page = page + 10 
        count += 1
        getRankingsFromGoogle(keyword, website, page, count)


def main(): 

    websites = getTrackedWebsites(); 

    for website in websites: 

        keywords = website['keywords']

        for keyword in keywords: 
            getWebsiteRankingsByKeywords(keyword, website['website'])

The function goes into the else statement, but never actually reaches the if count is 5 which is where I need it to be in order to create the object for the keyword was not found.

I have also created another script:

def getRankingsFromGoogle(keyword, website, page = 1, count = 1):

    if count is 5: 
        print "YAY"
        return;

    page = 10
    count += 1

    print page 
    getRankingsFromGoogle("asf", "www.google.com", page, count)


getRankingsFromGoogle("af", "www.google.com", 1, 1)

Which reaches the condition count is 5.

I don't understand why this recursive function is not working.

smci
  • 32,567
  • 20
  • 113
  • 146
Phorce
  • 4,424
  • 13
  • 57
  • 107
  • 5
    `if count == 5:` ? – WhatsThePoint Jan 06 '17 at 15:10
  • Ignore me. I can't read. – Eli Sadoff Jan 06 '17 at 15:10
  • 2
    @WhatsThePoint `count is 5` and `count == 5` are equivalent to the best of my belief. – Eli Sadoff Jan 06 '17 at 15:11
  • Add print statements at strategic locations in the code to see what is going on. – wwii Jan 06 '17 at 15:12
  • @WhatsThePoint For integers they are the same (see [here](http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python)). – Eli Sadoff Jan 06 '17 at 15:12
  • My guess is that `if not bool(obj)` is returning before you can get to `count is 5`. – Eli Sadoff Jan 06 '17 at 15:14
  • ive never actually come across using `is` like that, is it just general practice to use `==`? – WhatsThePoint Jan 06 '17 at 15:16
  • @WhatsThePoint They have different use cases. It's more similar to JavaScript's `===`. For example `2 is 2.0` is `False`, but `2 == 2.0` is `True`. – Eli Sadoff Jan 06 '17 at 15:18
  • @EliSadoff thanks for clearing that up. is there any examples on when an `is` would behave completely different to an `==` if you know what i mean? – WhatsThePoint Jan 06 '17 at 15:23
  • 1
    @WhatsThePoint Yes there are! Arrays are a great example. `[] is []` is false, but `[] == []` is true. Also `[1, 2, 3] is [1, 2, 3]` vs. `[1, 2, 3] == [1, 2, 3]` follows the same pattern. – Eli Sadoff Jan 06 '17 at 15:26
  • @EliSadoff thanks for clearing that up :) – WhatsThePoint Jan 06 '17 at 15:31
  • 2
    I think the fact that `is` can be used in place of `==` for small integers is an implementation detail, and not part of the Python spec. As such, it could change between implementations or between versions of the same implementation. Best to use `==` unless you actually want to know if they're the same object (which is almost never the case when you're dealing with integers). – glibdud Jan 06 '17 at 15:32
  • 1
    Two approaches giving the same result doesn't mean that are both "conceptually" correct. `A is B` means that A and B are the same object. `A==B` means that two different objects are identical (given a comparison metric). Just use the operator that makes "conceptual sense". So even if `A=2; B=2; A is B` returns the "correct result" (True) it doesn't mean is "correct" at all. –  Jan 06 '17 at 15:41
  • So what output do you actually get? – w08r Feb 02 '20 at 06:33

1 Answers1

0

I'm a bit confused on the following logic:

def getRankingsFromGoogle(keyword, website, page = 1, count = 1): 

   if count is 5: 
   print "NOT ON PAGE 5" 

is you're not passing in a count when calling the getRankingsFromGoogle function, why would you ever expect it to hit this condition?

Hani Zion
  • 11
  • 4