I have this simple code in Python:
import sys
class Crawler(object):
def __init__(self, num_of_runs):
self.run_number = 1
self.num_of_runs = num_of_runs
def single_run(self):
#do stuff
pass
def run(self):
while self.run_number <= self.num_of_runs:
self.single_run()
print self.run_number
self.run_number += 1
if __name__ == "__main__":
num_of_runs = sys.argv[1]
crawler = Crawler(num_of_runs)
crawler.run()
Then, I run it this way:
python path/crawler.py 10
From my understanding, it should loop 10 times and stop, right? Why it doesn't?
Why can't my program get out of a while loop? Simply because it isn't meeting the exit conditions. The expectation would be that the loop will execute 100 times but because counter is an unsigned 8 bit type it has a range 0–255 and when decremented from zero goes to 255 causing an infinite loop.
Breaking Out of While Loops. To break out of a while loop, you can use the endloop, continue, resume, or return statement. endwhile; If the name is empty, the other statements are not executed in that pass through the loop, and the entire loop is closed.
An infinite loop is a loop that runs indefinitely and it only stops with external intervention or when a break statement is found. You can stop an infinite loop with CTRL + C . You can generate an infinite loop intentionally with while True . The break statement can be used to stop a while loop immediately.
Basically, the infinite loop happens when the condition in the while loop always evaluates to true. This can happen when the variables within the loop aren't updated correctly, or aren't updated at all.
num_of_runs = sys.argv[1]
num_of_runs
is a string at that stage.
while self.run_number <= self.num_of_runs:
You are comparing a string
and an int
here.
A simple way to fix this is to convert it to an int
num_of_runs = int(sysargv[1])
Another way to deal with this is to use argparser
.
import argparse
parser = argparse.ArgumentParser(description='The program does bla and bla')
parser.add_argument(
'my_int',
type=int,
help='an integer for the script'
)
args = parser.parse_args()
print args.my_int
print type(args.my_int)
Now if you execute the script like this:
./my_script.py 20
The output is:
20
Using argparser also gives you the -h option by default:
python my_script.py -h
usage: i.py [-h] my_int
The program does bla and bla
positional arguments:
my_int an integer for the script
optional arguments:
-h, --help show this help message and exit
For more information, have a look at the argparser documentation.
Note: The code I have used is from the argparser documentation, but has been slightly modified.
When accepting input from the command line, data is passed as a string. You need to convert this value to an int
before you pass it to your Crawler
class:
num_of_runs = int(sys.argv[1])
You can also utilize this to determine if the input is valid. If it doesn't convert to an int, it will throw an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With