Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use list comprehension #2

Open
c3ho opened this issue Sep 25, 2020 · 2 comments
Open

Use list comprehension #2

c3ho opened this issue Sep 25, 2020 · 2 comments

Comments

@c3ho
Copy link

c3ho commented Sep 25, 2020

detectURL/detectURL.py

Lines 33 to 36 in 695ab05

for line in file:
url = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', line)
if url:
urls += url

Hey, if you're ever interested in trying out list comprehension here's a stackoverflow thing you can look into making it a one-liner
https://stackoverflow.com/questions/52286057/list-comprehension-with-if-condition-in-python

 for line in file: 
     url = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', line) 
     if url: 
         urls += url 

to something like

urls = [line for line in file if re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', line)]
@jongwon-jang
Copy link
Owner

Thank you so much for the advice.
I'll try this way

jongwon-jang pushed a commit that referenced this issue Sep 25, 2020
jongwon-jang added a commit that referenced this issue Sep 25, 2020
@c3ho
Copy link
Author

c3ho commented Sep 26, 2020

Almost there, I tested the urls = [line for line in file if re.findall(r"https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+", line)] and it should work

def initialize():

    with open(sys.argv[1]) as file:
       #The [code here] part creates an array/list so no need to append
       #We're pretty much saying for each line in the file if it matches the regex we have, append it to a temp array/list then 
          assign the whole array/list to urls
        urls = [line for line in file if re.findall(r"https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+", line)]
        
        for link in urls:
            //rest of code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants