How to change parameters without stopping the program - python-2.7

Suppose I write a program like this.
# run the program until the user gives a "stop" input
usrinpt=""`
n=1
while usrinpt!="stop":
n+=1
---- do-something -----
---- do-something -----
---- do-something -----
print n # print the number of loops it has gone through.
Now program will run until I manually change the parameter usrinpt to "stop". But using raw_input will stop the simulation at every step which is not what I want.
So, is there a way to change the usrinpt without stopting the simulation?

A more involved solution using a thread:
from __future__ import print_function # Python 2/3 compatibility
import sys
from time import sleep
from threading import Thread
if sys.version_info.major < 3:
input = raw_input
def do_something():
# doing the work
sleep(1)
usrinpt = ''
def main():
n = 1
while usrinpt != 'stop':
n += 1
do_something()
print('\nnumber of loops', n)
thread = Thread(target=main)
thread.start()
while True:
print('Enter "stop" to terminate program')
usrinpt = input().strip().lower()
if usrinpt == 'stop':
break
thread.join()
Sample program run:
python stop.py
Enter "stop" to terminate program
hello
Enter "stop" to terminate program
stop
number of loops 6

You can catch a KeyboardInterrupt exception:
from __future__ import print_function # Python 2/3 compatibility
n = 1
try:
while True:
n += 1
except KeyboardInterrupt:
print('\nnumber of loops', n)
When the user types <CTRL>-<C> the program prints the number of iterations and continues.

Related

how to run 2 if statements simultaneously

i have this code and i'm having a problem with it, when the first if statement is working i can't run the second if statement until the last delay finishes and if the second if statement starts i can't run the first statement and vise versa
so what would be the be code to fix this problem i tried def and while but couldn't get to what i need
import RPi.GPIO as GPIO
import time
import lcddriver
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
display=lcddriver.lcd()
GPIO.cleanup()
# Configure Relay Output Pins
Relay1=31
Relay2=32
GPIO.setup(Relay1,GPIO.OUT)
GPIO.setup(Relay2,GPIO.OUT)
# Configure Relay Input Pins
IN1=29
IN2=15
GPIO.setup(IN1,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(IN2,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
while (1):
if GPIO.input(IN1)==1:
GPIO.output(Relay1,1)
display.lcd_clear()
display.lcd_display_string(" Input 1 ", 1)
display.lcd_display_string(" Opened ", 2)
time.sleep(0.5)
GPIO.output(Relay1,0)
display.lcd_clear()
time.sleep(1)
if GPIO.input(IN2)==1:
GPIO.output(Relay2,1)
display.lcd_clear()
display.lcd_display_string(" Input 2 ", 1)
display.lcd_display_string(" Opened ", 2)
time.sleep(0.5)
GPIO.output(Relay2,0)
time.sleep(1)
display.lcd_clear()
GPIO.cleanup()
That's because you're not in a multi-process program
Only one thread of your CPU is used
If you really need to run the two if statements at the same time you should change your code so that the parts you need to run is in the same if statement
If condition1:
Part1
If condition2:
Part2
Will become
If condition3:
Part1
Part2
It's an algorithmic error, you shouldn't seperate your block within two if(s)
I can suggest to use a function like this
def do_part_two():
Part2
def do_part_one():
Part1
If condition1:
Part1
do_part_two()
If condition2:
Part2
do_part_one()
But still I encourage you to change your if so that you're in my first solution case
N.B:
You can create a delay function and call it whenever you need
def delay(x):
time.sleep(x)

Printing numbers from a loop on a line python

Using
for i in range(5):
print i+1,
it prints 1 2 3 4 5
Is there anyway for it to print without any spaces e.g. 12345
The key is to create a single string that gets printed once. You can do
print ''.join(map(str, range(1,6)))
or
print ''.join(str(i+1) for i in range(5))
Or use the Python 3 compatible print function which takes an end argument.
from __future__ import print_function
for i in range(5):
print(i+1, end='')
The print function also takes a sep argument so you can print your entire range in one go.
from __future__ import print_function
print(*range(1,6), sep='')
>>> s=''
>>> for i in range(5):
... s=s+str(i+1)
...
>>> s
'12345'

Looping in Python Slowing Down with each Iteration

The following code produces the desired result, but the root of my issue stems from the process slowing down with each append. In the first 100 pages or so, it runs at less than one second per page, but by the 500s it gets up to 3 seconds and into the 1000s it runs at about 5 seconds per append. Are there suggestions for how to make this more efficient or explanations for why this is just the way things are?
import lxml
from lxml import html
import itertools
import datetime
l=[]
for pageno in itertools.count(start=1):
time = datetime.datetime.now()
url = 'http://example.com/'
parse = lxml.html.parse(url)
try:
for x in parse.xpath('//center'):
x.getparent().remove(x)
x.clear()
while x.getprevious() is not None:
del x.getparent()[0]
for n in parse.xpath('//tr[#class="rt"]'):
l.append([n.find('td/a').text.encode('utf8').decode('utf8').strip()\
,n.find('td/form/p/a').text.encode('utf8').decode('utf8').strip()\
,n.find('td/form/p/a').attrib['title'].encode('utf8').decode('utf8').strip()]\
+[c.text.encode('utf8').decode('utf8').strip() for c in n if c.text.strip() is not ''])
n.clear()
while n.getprevious() is not None:
del n.getparent()[0]
except:
print 'Page ' + str(pageno) + 'Does Not Exist'
print '{0} Pages Complete: {1}'.format(pageno,datetime.datetime.now()-time)
I have tried a number of solutions, such as disabling the garbage collector, writing one list as a row to file instead of appending to a large list, etc. I look forward to learning more from potential suggestions/answers.

How can i make it so the program progresses while a 'while loop' is running a timer (python)

I need to know how i can enter user input while this while loop counting time is running. I know my program is not efficient nor organized as I am new to python. A simple but longer fix is better than a short fix that I would not understand.
import time
print 'Welcome To the Speedy Type Game!'
time.sleep(1)
print "You're objective is to win the game by typing the specific word before the time runs out"
print "Don't forget to press 'enter' after typing the word!!!!"
print "For round 1 you will have 5 seconds"
null = raw_input('Press enter when you are ready to start')
print '3'
time.sleep(1)
print '2'
time.sleep(1)
print '1'
time.sleep(1)
print 'GO'
C = 'poop'
x = 5
while C in ['poop']:
x = x - 1
time.sleep(1) #This is where my program comes to a halt.
C = raw_input("Print the word 'Calfornia': ") #I dont know how to make the program progress to here without stopping the timer above.
if x < 0:
print 'You have failed, game over!'
else:
print 'Good Job! let us move to the next round!'
There is no easy way to do this in Python - a single process is running at a time, so without e.g. threading (see https://stackoverflow.com/a/2933423/3001761) you can't overlap user input with counting. An easier approach might be:
def test(word, limit=5):
started = time.time()
while True:
result = raw_input("Type {0!r}: ".format(word))
finished = time.time()
if result == word and finished <= started + limit:
print 'Good Job! let us move to the next round!'
return True
elif finished > started + limit:
break
print 'Wrong, try again.'
print 'You have failed, game over!'
return False
Then call e.g.:
>>> test("California")
Type 'California': California # typed quickly
Good Job! let us move to the next round!
True
>>> test("California")
Type 'California': foo # typed wrongly but quickly
Wrong, try again.
Type 'California': California # typed quickly
Good Job! let us move to the next round!
True
>>> test("California")
Type 'California': foo # typed wrongly and slowly
You have failed, game over!
False
>>> test("California")
Type 'California': California # typed slowly
You have failed, game over!
False

Conditional quit multiprocess in python

I'm trying to build a python script that runs several processes in parallel. Basically, the processes are independent, work on different folders and leave their output as text files in those folders. But in some special cases, a process might terminate with a special (boolean) status. If so, I want all the other processes to terminate right away. What is the best way to do this?
I've fiddled with multiprocessing.condition() and multiprocessing.manager, after reading the excellent tutorial by Doug Hellmann:
http://pymotw.com/2/multiprocessing/communication.html
However, I do not seem to understand how to get a multiprocessing process to monitor a status indicator and quit if it takes a special value.
To examplify this, I've written the small script below. It somewhat does what I want, but ends in an exception. Suggestions on more elegant ways to proceed are gratefully welcomed:
br,
Gro
import multiprocessing
def input(i):
"""arbitrary chosen value(8) gives special status = 1"""
if i == 8:
value = 1
else:
value = 0
return value
def sum_list(list):
"""accumulative sum of list"""
sum = 0
for x in range(len(list)):
sum = sum + list[x]
return sum
def worker(list,i):
value = input(i)
list[i] = value
print 'processname',i
if __name__ == '__main__':
mgr = multiprocessing.Manager()
list = mgr.list([0]*20)
jobs = [ multiprocessing.Process(target=worker, args=(list,i))
for i in range(20)
]
for j in jobs:
j.start()
sumlist = sum_list(list)
print sumlist
if sumlist == 1:
break
for j in jobs:
j.join()