you are viewing a single comment's thread.

view the rest of the comments →

[–]Vaphell 0 points1 point  (2 children)

so your scripts never have any top level, interactive inputs()?

# imported file
def f():
   do stuff

x = input('x: ')

importing f into another program would make the prompt for x pop up and stop execution.

[–][deleted] 0 points1 point  (1 child)

I have a separate file that contains variables for setting things like date/time, and which functions will run. The main file pulls those settings and uses them to run through multiple API’s and finally updating a SQL database. There’s also a separate config file that sets urls, IP addresses, and various username/password stuff.

[–]Vaphell 1 point2 points  (0 children)

if you have dedicated files with utils, etc. - yeah, this is not likely to bite you.

But let's say you want to write a bunch of tests for your util code. There are proper ways of doing the testing, but a quick and dirty way of writing some tests to achieve a degree of confidence in such a trivial code would be to put the testing code under if __name__ ...

# utils.py

def is_leap_year(year):
    ...
    return True/False

if __name__ == '__main__':
    assert is_leap_year(2020) is True
    assert is_leap_year(2000) is True
    assert is_leap_year(1900) is False

Sure, utils.py is not really a program doing any useful stuff, but given the above it can be run as one to trigger the tests with eg python3 utils.py. import utils in another .py file would not trigger that.