0

I have to ask the user for 2 inputs. The first one is an integer to be used here.

workbook = xlsxwriter.Workbook(str(date.today() - timedelta(days=days)) + '-' + excel_name + '.xlsx')

so,

days = int(input("Enter the number of days :")

wait for say, 10 seconds..if nothing is entered, make the value of days to 1. Same thing for the next option. User needs to select from a list of choice, if nothing is entered for 10 seconds, choose the 1st option.

excel_name = ['a','b','c','d']
for i in range(len(excel_name)):
    print(i, ".", excel_name[i])
    choice = int(input("Enter the number of Menu Choice ")

I tried this..this always reaches timeout and doesn't care about what is entered.

from inputimeout import inputimeout, TimeoutOccurred
try:
    something = inputimeout(prompt='>>', timeout=5)
except TimeoutOccurred:
    something = 'something'
print(something)
   

Any ideas?

As someone pointed out..This should work..

import msvcrt
import time

t0 = time.time()
while time.time() - t0 < 600:
    if msvcrt.kbhit():
        if msvcrt.getch() == '\r': # not '\n'
            break
    time.sleep(0.1)

EDIT :- As suggested, I tried one of the linked answers,

class TimeoutExpired(Exception):
    pass

def input_with_timeout(prompt, timeout, timer=time.monotonic):
    sys.stdout.write(prompt)
    sys.stdout.flush()
    endtime = timer() + timeout
    result = []
    while timer() < endtime:
        if msvcrt.kbhit():
            result.append(msvcrt.getwche()) #XXX can it block on multibyte characters?
            if result[-1] == '\r':
                return ''.join(result[:-1])
        time.sleep(0.04) # just to yield to other processes/threads
    raise TimeoutExpired

It reaches timeout and ignores the input.

Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38
  • are you on linux? – Aven Desta Feb 18 '21 at 14:42
  • look if you can find something [here](https://stackoverflow.com/questions/15528939/python-3-timed-input) for timed input – Leonardo Scotti Feb 18 '21 at 14:43
  • the script with `inputimeout` works as expected for me. it dumps the user input or `something` based on the timeout value of 5 seconds. – Krishna Chaurasia Feb 18 '21 at 14:43
  • 1
    Does this answer your question? [Python: wait for user input, and if no input after 10 minutes, continue with program](https://stackoverflow.com/questions/19508353/python-wait-for-user-input-and-if-no-input-after-10-minutes-continue-with-pro) – Aven Desta Feb 18 '21 at 14:44
  • Unfortunately , on Windows. :( – Abhishek Rai Feb 18 '21 at 14:45
  • @AvenDesta Can you give an answer? – Abhishek Rai Feb 18 '21 at 14:49
  • I'm not on windows, I can't test if the code on the link works or not. Try all the codes on the link I provided – Aven Desta Feb 18 '21 at 14:50
  • @AvenDesta I don't understand how to code this for my situation..where is it asking for input?..I updated the question. – Abhishek Rai Feb 18 '21 at 14:51
  • https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout does this answer your question – Rishabh Kumar Feb 18 '21 at 15:00
  • @RishabhKumar If you think this question has an answer somewhere else in this site - [flag it as duplicate](https://stackoverflow.com/help/privileges/flag-posts) instead of posting a link as a comment... – Tomerikoo Feb 18 '21 at 15:03
  • No, these links don't answer my question. I tried the answer in the suggested links. It says right there that the accepted answer is wrong. Read it. The other method suggested which I have included in the question also does not work. None of the links answer this question – Abhishek Rai Feb 18 '21 at 15:05
  • @Tomerikoo It is not a duplicate and neither has the link the answer I am looking for..I want it to take a certain value if nothing is entered. The answers there, always reach the Exception and do not consider the input..like in the method I tried with `inputimeout` same result with `mscvrt` – Abhishek Rai Feb 18 '21 at 15:12
  • @AbhishekRai in your `inputimeout` module solution try to catch the `e.args` in your except block, and see what actualy maybe going wrong for you. – Rishabh Kumar Feb 18 '21 at 15:12
  • @RishabhKumar Updated the question – Abhishek Rai Feb 18 '21 at 15:15
  • No, not in that, the answer in your original post, where you imported `inputimeout` module :: `from inputimeout import inputimeout, TimeoutOccurred try: something = inputimeout(prompt='>>', timeout=5) except TimeoutOccurred as e: something = 'something' print(e.args)` – Rishabh Kumar Feb 18 '21 at 15:18
  • @RishabhKumar ..I think, that might be it. Let me check – Abhishek Rai Feb 18 '21 at 15:20
  • @RishabhKumar.Yea..No luck. Shows `()` and executes the Exception block..ignores the input – Abhishek Rai Feb 18 '21 at 15:26
  • From a small attempt to debug, it seems that the `if msvcrt.kbhit():` is never entered for some reason – Tomerikoo Feb 18 '21 at 15:30
  • @Tomerikoo None of these answers actually work. I don't know how to use the Threading Time in my case. Another one is SIGNAL?..That is for linux, I think. – Abhishek Rai Feb 18 '21 at 15:32
  • @AbhishekRai Shouldn't the `else` in your `except TimeoutExpired:` example be a `finally:`? – Axe319 Feb 18 '21 at 17:43
  • @Axe319 I finally got it...Posted the answer. – Abhishek Rai Feb 18 '21 at 17:54
  • @Tomerikoo Found the working way..Check answer. – Abhishek Rai Feb 18 '21 at 17:54
  • @RishabhKumar Posted the working way as an answer. Thanks for your effort. – Abhishek Rai Feb 18 '21 at 17:55

1 Answers1

0

This is the option that works on Windows. pip3 install func-timeout

from func_timeout import FunctionTimedOut, func_timeout
try:
    days = func_timeout(5, lambda: int(input('Enter the number of days :\n')))
    print(days)
except FunctionTimedOut:
    days = 1
    print(days)

for i in range(len(excel_name)):
    print(i, ".", excel_name[i])

try:
    choice = func_timeout(5, lambda: int(input('Enter the Menu Choice number :\n')))
    print(choice)
except FunctionTimedOut:
    choice = 0
    print(choice)
    
Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38
  • 1
    Good that it works for you. I researched and attempted to do this in a native way, but with no luck. – Rishabh Kumar Feb 18 '21 at 18:03
  • However the `inputimeout` works properly on my linux system – Rishabh Kumar Feb 18 '21 at 18:04
  • There are other options also that work on `linux` problem was this needs to be run on windows. – Abhishek Rai Feb 18 '21 at 18:04
  • Yes, I still think it had something specifically to do with the console its working on. I say this because I came across cases when this worked on terminal but not on python console, even on same OS and also because some solutions that were specifically knitted for windows still didn't worked your case. – Rishabh Kumar Feb 18 '21 at 18:09
  • Well, the current answers on timed input in python for windows, either don't work or are hit and miss types and complicated. Hopefully, this question and answer will help someone in the future. – Abhishek Rai Feb 18 '21 at 18:12