1

I want to finish a python script(func) which has below functions:

1.there are two threads,one thread print a updating line which shows the left time before input to the terminal

2.another thread waiting for my input to the terminal(sys.stdin)

3.my question is,while i use my func like:

print '''input your file path,the default value will be "/root/targets.txt":>'''
get_input_intime([''],'/root/targets.txt',20)

but I can not input a long file abs path,coz the first thread is always print the left time,and I want to achieve:

a)there are two lines in the terminal

b)the first line as the first thread's print string's place

c)the second line will accept my input,so i can input a file path in my need and will not be flushed by the first thread,like this:

in terminal:

first line:sys.stdout.write("%s seconds left...please input your chioce:>\r" % timeout)

second line:(wainting for my input)

4.my code is:

1)get chioce in some seconds,if no input,return the default chioce

2)the first param is a list contain undefault chioces.

eg.there are four chioce 1,2,3,4,if I want to make the default chioce as 2,then the undefault_chioce_list is ['1','3','4'],each of the list is str type.

3)the second param is the default chioce value

4)the third param is the time before input(choose)

5)the return value is the chioce from raw_input()

class MyThread(threading.Thread):
    def __init__(self,func,args,name=''):
        threading.Thread.__init__(self)
        self.name=name
        self.func=func
        self.args=args
    def run(self):
        self.result=apply(self.func,self.args)
    def get_result():
        return self.result

def get_input_intime(undefault_chioce_list,default_choose,timeout=5):
    default_choose=[default_choose]
    timeout=[timeout]
    choosed=[0]
    chioce=['']
    def print_time_func():
        while (choosed[0]==0 and timeout[0]>0):
            sys.stdout.write("%s seconds left...please input your chioce:>\r" % timeout)
            sys.stdout.flush()
            time.sleep(1)
            timeout[0]-=1
        if choosed[0]==0:
            chioce[0]=default_choose[0]
    def input_func():
        rlist, _, _ = select([sys.stdin], [], [], timeout[0])
        if rlist:
            chioce_and_enter = sys.stdin.readline()
            choosed[0]=1
            if chioce_and_enter[0] not in undefault_chioce_list:
                chioce[0]=default_choose[0]
            else:
                chioce[0]=chioce_and_enter[0]
            print "you choosed %s" % chioce[0]
        else:
            print "\nyou didn't input..."
            print "I will choose the default chioce for you:%s" % default_choose[0]
    time_left_thread=MyThread(print_time_func,())
    input_thread=MyThread(input_func,())
    time_left_thread.start()
    input_thread.start()
    time_left_thread.join()
    input_thread.join()
    return chioce[0]

    

1 Answer 1

0

finally,I achieve it in below code,but can not print the left time in one line,it will print the left time in lines: ----------------------output:---------------------

20 seconds left...please input your chioce:>

19 seconds left...please input your chioce:>

18 seconds left...please input your chioce:>

17 seconds left...please input your chioce:>

16 seconds left...please input your chioce:>

15 seconds left...please input your chioce:>

14 seconds left...please input your chioce:>

> this is my choose line

you choosed d

-------------------------end---------------------

import threading
import time
import sys
import readline
class MyThread(threading.Thread):
    def __init__(self,func,args,name=''):
        threading.Thread.__init__(self)
        self.name=name
        self.func=func
        self.args=args
    def run(self):
        self.result=apply(self.func,self.args)
    def get_result():
        return self.result

def get_input_intime(undefault_chioce_list,default_choose,timeout=5):
    default_choose=[default_choose]
    timeout=[timeout]
    choosed=[0]
    chioce=['']
    def print_time_func():
        while choosed[0]==0 and timeout[0]>0:
            time.sleep(1)
            sys.stdout.write('\r'+' '*(len(readline.get_line_buffer())+2)+'\r')
            print "%s seconds left...please input your chioce:>" % timeout[0]
            sys.stdout.write('> ' + readline.get_line_buffer())
            sys.stdout.flush()
            timeout[0]-=1
        if choosed[0]==0:
            chioce[0]=default_choose[0]
    def input_func():
        while choosed[0]==0 and timeout[0]>0:
            s = raw_input('> ')
            #rlist, _, _ = select([sys.stdin], [], [], timeout[0])
            if s or len(s)==0:
                input_value=s
                choosed[0]=1
                if input_value not in undefault_chioce_list:
                    chioce[0]=default_choose[0]
                else:
                    chioce[0]=input_value
                print "you choosed %s" % chioce[0]
    time_left_thread=MyThread(print_time_func,())
    input_thread=MyThread(input_func,())
    time_left_thread.start()
    input_thread.setDaemon(True)
    input_thread.start()
    time_left_thread.join()
    if choosed[0]==0:
        print "i choose the default chioce for you:%s" % chioce[0]
    return chioce[0]
get_input_intime(['a','b','c'],'d',20)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.