0

I have the following code:

from sys import stdin
import re

def main():
    def hidden1_test(): return hidden1('test')
    def hidden2_test(): return hidden2('test')
    tasks = [dna, sorted, hidden1_test, hidden2_test, equation, parentheses, sorted3]
    print('Skriv in teststrängar:')
    while True:
        line = stdin.readline().rstrip('\r\n')
        if line == '': break
        for task in tasks:
            result = '' if re.search(task(), line) else 'INTE '
            print('%s(): "%s" matchar %suttrycket "%s"' % (task.__name__, line, result, task()))

if __name__ == '__main__': main()

Functions in the list tasks are defined above but they are not important for my question. I understand the entire run of the function main up until the last two rows:

result = '' if re.search(task(), line) else 'INTE '    
print('%s(): "%s" matchar %suttrycket "%s"' % (task.__name__, line, result, task()))

I don't understand what task.__name__ means. Name must be __name__ == '__main__' so i suppose my question becomes what does task.'__main__' mean (correct me if i am wrong)? I interpret the first row of the two above as:

result = '' 
if re.search(task(), line): 
else:
    'INTE '

I know this can't be right since there is nothing under the if statement, but I don't know how to test the code to get an understanding of what it means. I am just trying to get a better understanding so an explanation for someone who is not a very advanced programmer would be nice

Thanks in advance!

Eric Citaire
  • 4,355
  • 1
  • 29
  • 49

2 Answers2

1

There are multiple questions here.

What is __name__?

In Python, __name__ is a special attribute automatically created for some type of constructs like modules, classes, functions, etc. (see Python Data Model for more details)

In your code, it is used twice :

  • task.__name__ will be the name of whatever task represents. I guess it will be a function in your code.

    For example, for the task hidden1_test, task.__name__ will be 'hidden1_test'.

  • if __name__ == '__main__': main() is a classic way in Python to determine if the current script is the main Python program.

    Here, __name__ is the name of the current module. If it equals to the value '__main__', then it means the current script is the main program.

    You can find more information on this in What does if __name__ == "__main__": do?

How does work result = 'A' if matches else 'B'?

This is a conditional expression, also known as ternary operator.

In most languages inspired by C, the same code will be written result = matches ? 'A' : 'B'.

If the return value of re.search(task(), line) is truthy, the variable result will be assigned the value ''. If not, it will be assigned the value 'INTE '.

Eric Citaire
  • 4,355
  • 1
  • 29
  • 49
1

So you have a list of tasks.

tasks = [dna, sorted, hidden1_test, hidden2_test, equation, parentheses, sorted3]

You do not know when those tasks will end, so you keep checking until they are finished : while True:

To know which one is still running, you read a line from the standard input.

line = stdin.readline().rstrip('\r\n')

see: python 3.8.2, stdin

If all tasks are finished, you have nothing to read, you break out of the while loop and end the program.

if line == '': break

# same as
if line == '':
  break

For informational purpose we want to print which tasks are still running and which ones have been INTErupted.

Is this task still running (is the task name found in line)?

First we use regex, which is a tool to search for patterns in text.

re.search(pattern, text) # Return true if it found something!

Play with RegExr, you'll see that it's pretty cool.

Then, if the task is not found (it is finished), we add INTE to mean that it has been interrupted.

result = '' if re.search(task(), line) else 'INTE '

# Above is a ternary operator, which is the same as...
if re.search(task(), line) :
  result = ''
else:
  result = 'INTE'

Here we use what most people call a ternary operator.

... and then, we print a text. task.__name__ is simply the name of the task. Most likely, your task is an object, and this object has a name. A good guess would be that your task name could be dna, sorted, hidden1_test, ...

Florian Fasmeyer
  • 795
  • 5
  • 18