0

I'm trying to understand what is going on inside any function. Can't understand the loop inside it. Can anyone break it down for me?

with open(host_temp,'r+') as f:
    c = f.readlines()
    f.seek(0)
    for line in c:
        if not any(website in line for website in blocked_sites):
            f.write(line)
    f.truncate()
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • 1
    Does this answer your question? [How do Python's any and all functions work?](https://stackoverflow.com/questions/19389490/how-do-pythons-any-and-all-functions-work) – Jim G. Oct 22 '20 at 12:41
  • That's a generator expression. Are you familiar with list comprehensions? If yes, you can imagine that call as `any([website in line for website in blocked_sites])`. If no, consider learning about them: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions – tevemadar Oct 22 '20 at 12:51

4 Answers4

2

The Python built-in any() function returns True if any element of the iterable is true. If the iterable is empty, return False.

The syntax inside the any function in this example (x in y for x in z) is known as a generator expression.

[The syntax for generator expressions] is the same as for comprehensions, except that it is enclosed in parentheses instead of brackets or curly braces.

The main difference between a generator and a comprehension, is that a generator evaluates lazily (as needed) whereas a comprehension evaluates all of the variables immediately. More precisely, a generator expression yields a new generator object that lazily evaluates the variables whenever its next() method is called.

In this case, the generator expression is iterating through a container called blocked_sites. For each website in blocked_sites, it is checking if the website is contained in the current line of the file.

So, if any blocked website is found in a line of the file, then that line is skipped.

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • Can you elaborate on the ongoing process within the loop? I'm trying to understand what is happening when it says: website in line for website in blocked_sites ... is it taking a line from the text file that I opened and trying to match it with the blocked list items? Is that what is going there? – Forhez Ahmed Oct 22 '20 at 12:42
1

To complement Christopher Peisert's answer I split the code into how one might extent the any statement to make it more readable (however not preferable code-vice).

with open (host_temp,'r+') as f:
  c = f.readlines()
  f.seek(0)
  for line in c:
    website_in_blocked_sites = False
    for website in blocked_sites:
      if website in line:
        website_in_blocked_sites = True
    if website_in_blocked_sites:
      f.write(line)
  f.truncate()
Marcus
  • 401
  • 6
  • 13
0

The code is similar to this (providing the long version for this code):

for line in c:
    is_website_in_line = []
    for website in blocked_sites:
        is_website_in_line.append(website in line) # website in line check if a string is within another, so it returns either True or False
    if not any(is_website_in_line) # if all of the list has False values
        f.write(line)
Meny Issakov
  • 1,400
  • 1
  • 14
  • 30
0

any() will return true if any of the elements handed to it are "truthy" (same as if x:). It takes in this case a generator as a parameter. Because the comprehension code is without surrounding [] and {} and also between () the implication is that it is a generator.

for website in blocked_sites:
    if website in line:
        return True
return False
Scott P.
  • 1,054
  • 11
  • 12