-
Notifications
You must be signed in to change notification settings - Fork 0
/
input2.py
48 lines (39 loc) · 1.1 KB
/
input2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''module to take input'''
import signal
class _getChUnix:
'''class to take input'''
def __init__(self):
'''init def to take input'''
import tty
import sys
def __call__(self):
'''def to call function'''
import sys
import tty
import termios
fedvar = sys.stdin.fileno()
old_settings = termios.tcgetattr(fedvar)
try:
tty.setraw(sys.stdin.fileno())
charvar = sys.stdin.read(1)
finally:
termios.tcsetattr(fedvar, termios.TCSADRAIN, old_settings)
return charvar
def alarmhandler(signum, frame):
''' input method '''
raise AlarmException
class AlarmException(Exception):
'''This class executes the alarmexception.'''
pass
def user_input(timeout=0.15):
''' input method '''
signal.signal(signal.SIGALRM, alarmhandler)
signal.setitimer(signal.ITIMER_REAL, timeout)
try:
text = _getChUnix()()
signal.alarm(0)
return text
except AlarmException:
pass
signal.signal(signal.SIGALRM, signal.SIG_IGN)
return '.'