Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 26th, 2005, 12:00 PM   #1
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Question Timed Inputs??

Is there a way to skip on from the raw_input() or input() if nothing is entered in a certain ammount of time??
So if a user has not entered anything in say 5 seconds, it will declare the variable blank, and move on!
__________________
while me is alive:
	make(life,simple)
SaturN is offline   Reply With Quote
Old Jun 26th, 2005, 12:28 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
You'll have to use some type of programming that reads the keyboard input buffer and checks it for new key entries and times out if the buffer is still empty after X number of seconds.
__________________

tempest is offline   Reply With Quote
Old Jun 26th, 2005, 12:38 PM   #3
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Right, ok then thanks.
__________________
while me is alive:
	make(life,simple)
SaturN is offline   Reply With Quote
Old Jun 26th, 2005, 2:43 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
I'm pretty sure you'll need Pygame for that. Once you get on MSN I'll probably help you with that.
Sane is online now   Reply With Quote
Old Jun 26th, 2005, 4:13 PM   #5
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
I'm pretty sure you'll need Pygame for that. Once you get on MSN I'll probably help you with that.
Not really, there are a thousand other ways you could do it. My first idea was to send a signal to the program after 5 seconds and then catch and continue with the program, but that's not cross-platform (*nix only). My second idea was to raise an exception from another thread after timeout, but you can't do that as exceptions raised in one thread don't effect the other. My third idea (and IMO the cleanest) is to just use select.poll. Like so:

import sys, select

def readWithTimeout(message, timeout):
    """ Like raw_input, but you specify a timeout (in milliseconds). If that time
    is reached, then this just returns a blank string. """
    poller = select.poll()
    poller.register(sys.stdin, select.POLLIN)
    output = poller.poll(timeout)
    if output:
        return sys.stdin.readline()
    else:
        return ""

# Timeouts after 5 seconds.
name = readWithTimeout("Yeah, your name like: ", 5000)
Cerulean is offline   Reply With Quote
Old Jun 27th, 2005, 9:06 AM   #6
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Angry

Ran Cerulean's code and got this error ...
poller = select.poll()
AttributeError: 'module' object has no attribute 'poll'
I am using Python 2.4 on an XP box.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jun 27th, 2005, 9:36 AM   #7
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Meh, select.poll is *nix only as well. Oh well.. here's the same example using select.select instead of select.poll (which, unless i'm grossly mistaken, is cross platform ). The reason I didn't use it to begin with is that select.poll is more readable.
import sys, select

def readWithTimeout(message, timeout):
    """ Like raw_input, but you specify a timeout (in seconds). If that time
    is reached, then this just returns a blank string. """
    output = select.select([sys.stdin], [], [], timeout)
    if len(output[0]):
        return sys.stdin.readline()
    else:
        return ""

# Timeouts after 5 seconds.
name = readWithTimeout("Yeah, your name like: ", 5)
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC