coding style - Can this Python code be further shrunk? -


below python code fetches of sub-domains within domain. takes file input contains page source website. second argument domain name. example: "https://www.sometime.com".

import re def getsubdomains(fil,domain):    open(fil) f:     subdomainlst = []     line in f:       m = re.findall(r'\bhref="\https://[\w+\.*]+%s/'%domain,line)       if(m):          ele in m: subdomainlst.append(ele.split('/')[2])       else:             continue    subdomainlst = list(set(subdomainlst))    ele in subdomainlst: print ele  def main():     fil1,domain1 = raw_input("enter file name\n"),raw_input("enter domain\n")     getsubdomains(fil1,domain1) main() if __name__ == '__main__' else pass 

i've tried shrinking inner 'if else statement' to

for ele in m: subdomainlst.append(ele.split('/')[2]) if(m) else continue 

but gives error.

can above code shrunk further (16 lines now) takes minimum number of lines , become more pythonic?

you don't need add continue. can try doing this, though not recommend makes code unreadable.

subdomainlst = [ele.split('/')[2] line in f ele in re.findall(r'\bhref="\https://[\w+\.*]+%s/' % domain, line)] 

btw should indent code 4 spaces , try avoid one-line uncomprehensible statements: pythonic means readable

complete code:

if __name__ == '__main__':     import re      fil, domain = raw_input("enter file name\n"), raw_input("enter domain\n")     open(fil) f:         print '\n'.join([ele.split('/')[2] line in f ele in re.findall(r'\bhref="\https://[\w+\.*]+%s/' % domain, line)]) 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -