Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100662 > unrolled thread

return from function

Started byEmil Natan <shlyoko@gmail.com>
First post2015-12-20 12:34 +0000
Last post2015-12-22 14:23 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  return from function Emil Natan <shlyoko@gmail.com> - 2015-12-20 12:34 +0000
    Re: return from function Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-12-21 12:07 +0000
    Re: return from function Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-22 14:23 +0000

#100662 — return from function

FromEmil Natan <shlyoko@gmail.com>
Date2015-12-20 12:34 +0000
Subjectreturn from function
Message-ID<mailman.22.1450699272.2237.python-list@python.org>
I'm completely new to Python.
I have the following function to find the parent for domain. It removes the
left most label from the name and then checks if SOA record exists for the
reminder, if not it calls itself recursively removing another label and
checking again for SOA record. It works well for 'co.uk' and 'amazon.co.uk'
for example. It does not return the expected value 'uk' when invoked for '
amazon1.co1.uk', though the print command before the return prints what is
expected. Can someone explain why? Thanks.

>>> find_parent_domain('amazon.co1.uk.')
Test for parent domain co1.uk.
NXDOMAIN: invoke find_parent_domain recursively
Test for parent domain uk.
the parent domain we use is: uk.
>>>

import dns.resolver
from dns.exception import DNSException

def find_parent_domain(domainname):

    if domainname == '.':
        parent_domain = None
        return parent_domain

    parent_domain = domainname.partition('.')[2]
    try:
        print('Test for parent domain %s' % parent_domain)
        z = dns.resolver.query(parent_domain, 'SOA')
        print('the parent domain we use is: %s' % parent_domain)
        return parent_domain
    except dns.resolver.NXDOMAIN:
        print('NXDOMAIN: invoke find_parent_domain recursively')
        find_parent_domain(parent_domain)
    except dns.resolver.NoAnswer:
        print('NoAnswer: invoke find_parent_domain recursively')
        find_parent_domain(parent_domain)

[toc] | [next] | [standalone]


#100664

FromJon Ribbens <jon+usenet@unequivocal.co.uk>
Date2015-12-21 12:07 +0000
Message-ID<slrnn7fr0q.1mi.jon+usenet@frosty.unequivocal.co.uk>
In reply to#100662
On 2015-12-20, Emil Natan <shlyoko@gmail.com> wrote:
> I have the following function to find the parent for domain. It removes the
> left most label from the name and then checks if SOA record exists for the
> reminder, if not it calls itself recursively removing another label and
> checking again for SOA record. It works well for 'co.uk' and 'amazon.co.uk'
> for example. It does not return the expected value 'uk' when invoked for '
> amazon1.co1.uk', though the print command before the return prints what is
> expected. Can someone explain why? Thanks.

You probably want to look at this: https://publicsuffix.org/

>     except dns.resolver.NXDOMAIN:
>         print('NXDOMAIN: invoke find_parent_domain recursively')
>         find_parent_domain(parent_domain)

          return find_parent_domain(parent_domain)

>     except dns.resolver.NoAnswer:
>         print('NoAnswer: invoke find_parent_domain recursively')
>         find_parent_domain(parent_domain)

          return find_parent_domain(parent_domain)

[toc] | [prev] | [next] | [standalone]


#100728

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-12-22 14:23 +0000
Message-ID<n5bmd5$v26$2@dont-email.me>
In reply to#100662
On Sun, 20 Dec 2015 12:34:40 +0000, Emil Natan wrote:

> I'm completely new to Python.

>     parent_domain = domainname.partition('.')[2]
>     try:
>         print('Test for parent domain %s' % parent_domain)
>         z = dns.resolver.query(parent_domain, 'SOA')
>         print('the parent domain we use is: %s' % parent_domain)
>         return parent_domain
>     except dns.resolver.NXDOMAIN:
>         print('NXDOMAIN: invoke find_parent_domain recursively')
>         find_parent_domain(parent_domain)

None is being returned in this case!

>     except dns.resolver.NoAnswer:
>         print('NoAnswer: invoke find_parent_domain recursively')
>         find_parent_domain(parent_domain)

And in this case.

Do you want to return None in the NXDOMAIN and NoAnswer cases? If not, a 
return statement might help in returning a value.

When you recurse back into a function you still need to return the result 
of the recursion.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web