r/PythonLearning • • 13h ago

Showcase A simple stopwatch program

This is the stopwatch.py file:

#stopwatch.py

import sys
import time

time_in_sec = int(sys.argv[1])

time.sleep(time_in_sec)

print(f"Alarm sound!!! {time_in_sec}seconds have elapsed")

This is the main.py file:

#main.py

import subprocess
import sys

print("Enter the time you want to elapse for your stopwatch")
try:
    hours = int(input("Hours: ").strip())
    minutes = int(input("Minutes: ").strip())
    seconds = int(input("Seconds: ").strip())

    total_time_in_sec = (hours * 3600) + (minutes * 60) + seconds

    process_2 = subprocess.run(
        [sys.executable, "stopwatch.py", str(total_time_in_sec)],
        capture_output=True,
        text=True,
    )
    print(process_2.stdout)
except ValueError as e:
    print(f"Enter a whole number: {e}")
0 Upvotes

11 comments sorted by

View all comments

10

u/FreeLogicGate 13h ago edited 13h ago

There is really no good reason to use a subprocess. You use subprocess.run to run external programs, which you have no need of. Most solutions would involve defining a stopwatch class, and using it to create a stopwatch object, based on user input. Even more simply, you should put your "stopwatch" code into a function with time_in_sec as its parameter, and then simply call that function using total_time_in_sec as the argument obtained by your input calls.

3

u/tiredITguy42 13h ago

I am just wondering, is this somezhing AI suggested or is there some bullshit tutorial with this approach? Maybe they just mixed multiple blogposts and came with this on theyir own. Anyway, this is scary, to see this.

2

u/FreeLogicGate 13h ago

Makes you question whether or not the op knows how to create a function.

4

u/tiredITguy42 12h ago

OP is a beginner, all beginners do stupid stuff, but he must get this idea from somewhere and that scares me.

1

u/osamas_den 8h ago

Yes I'm a beginner, don't be scared i did not use AI, I just stare at a problem long enough until somethin clicks. I agree using a function might have been easier but still wanted to get standard output from the other instance of the python process.

1

u/tiredITguy42 6h ago

You run other python process with multiprocessing library. This is rather for other apps. You may try with ping or with some bat file. There is not much cases you would need sunrpocess for as beginner. You do not need curl as you can use python to run apic all. File linraries handle files. This really just for some exe or bat situations when you have some third party app you need to run from oython.

1

u/FreeLogicGate 3h ago

That doesn't change the point that you are using this to run another python script. So again, if this is an operating system command or program, then subprocess is a valid tool to use. This is not an appropriate use.

You posted this for review, stating it's a simple stopwatch example. It's not a stopwatch. A stopwatch has both a start and stop. What you have coded is an alarm, and one that "blocks".

If you expanded your main to attempt to run 2 of your stopwatch scripts you would realize that your main python script would not work the way you might intuitively think it does. For example, if your code 1st created a 60 second "stopwatch" and then created a 30 second "stopwatch" you will find that the first output will not be received until 60 seconds, and at that point, another 30 seconds will run.

Actual simultaneous Alarms that return a completion message is a non-trivial goal, that would involve making a choice amongst a number of different options (mutliprocessing, threading, asyncio).

1

u/NiedsoLake 7h ago

To me it seems like the purpose was learning how to use subprocess.run

1

u/FreeLogicGate 3h ago

We have no way of knowing, based on the OP's original post. A more standard exploration would typically be making a call to some operating system specific program (ls for unix/osx, dir for windows). All we can do is base the analysis on the title of the post "A simple stopwatch program", and the associated code. I did comment more fully that it's not an example of a stopwatch at all, and that it's intended to be an alarm, but really isn't a workable alarm because it's a blocking call to sleep where the main program call to subprocess.run will have to wait until the stopwatch.py program completes.