-2

Here is the code. 2 functions.

def get_domains(self): #returns test@test.com, test2@test.com etc in json. 
    if self.domain_names == None:
        r = requests.get(GET_DOMAINS_URL)
        if r.status_code != 200:
            raise ValueError("Can't get domains")
        self.domain_names = [item["name"] for item in r.json()]
    return self.domain_names

def is_valid_email(self, email):
    return email[email.find("@")+1:] in self.get_domains()

So what does part "+1:" in function is_valid_email ? How it works?

Gabio
  • 9,126
  • 3
  • 12
  • 32
Bogdan Mind
  • 65
  • 1
  • 1
  • 8
  • 2
    It's the index of the character after the `@` in the email. So it's the start of the domain name. – Barmar Sep 23 '21 at 18:48
  • 1
    So if `email` is `foo@gmail.com`, the slice extracts `gmail.com` – Barmar Sep 23 '21 at 18:49
  • 1
    `find` returns the index of the `@`. If you just did `email[email.find("@"):]`, the slice would include the "@". So, "@example.com". By adding 1, you get the domain name after the @, "example.com". – tdelaney Sep 23 '21 at 18:52
  • 1
    This code is also bugged, since str.find returns -1 if the substring is not found, and then adding 1 just slices from the beginning of the string. – wim Sep 24 '21 at 03:02

1 Answers1

0

This is a string slicing:

email[email.find("@")+1:]

It means - take all the characters from email string from the first index after the @ char till the end of string.

Or in simple words - extract the domain from an email address :)

Gabio
  • 9,126
  • 3
  • 12
  • 32