On Feb 25, 12:35 pm, wyleu wrote:
I'm trying to read a single keypress on Linux but expect to have the
programme running on Windows platform as well and find the mention in
import termios, fcntl, sys, os
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr = newattr & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
c = sys.stdin.read(1)
print "Got character", `c`
except IOError: pass
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
However this fails on the second line as sys.stdin seems to have no
Any idea how I might proceed?
I've never done this sort of thing (except in wxPython), but with a
little Google-magic I found the following:
A recipe that supposedly does this in a cross-platform way:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
And a Windows only module:http://effbot.org/librarybook/msvcrt.htm
From http Mon Feb 25 20:25:34 2008
From: http (Paul Rubin)
Date: 25 Feb 2008 11:25:34 -0800
bearophileHUGS at lycos.com writes:
def takenth(n, iterable):
"Returns the nth item"
return list(islice(iterable, n, n+1))
return islice(iterable, n).next()
isanymultiple = lambda x: any((x % y == 0) for y in nums)
return sum(filter(isanymultiple, xrange(end)))
This isn't so good, you really want to apply the filters recursively.
"""Return nth element of the fibonacci serie"""
if n == 0 or n == 1:
return fibonacci(n-1) + fibonacci(n-2)
uggh!!!! exponential blowup!
genfib = imap(fibonacci, count())
Are you kidding?
a,b = 1,2
a,b = b, a=b
What I think about such code:
- It's not much readable (but usually it can be read). ...
Your observations are generally good; I'd say it was done
without enough attention to the math too.
There is a full set of solutions on the haskell wiki, if anyone cares.