python without OO

Claudio Grondi claudio.grondi at freenet.de
Wed Jan 26 05:26:08 EST 2005


I can't resist to point here to the
  Re: How to input one char at a time from stdin?
posting in this newsgroup to demonstrate, what
this thread is about.

Claudio

> >On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
> ><brent.hughes at comcast.net> wrote:
> >> I'd like to get a character from stdin, perform some action, get
another
> >> character, etc.  If I just use stdin.read(1), it waits until I finish
typing
> >> a whole line before I can get the first character.  How do I deal with
this?
> >
> >This is exactly what you need:
> >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
> >Title: "getch()-like unbuffered character reading from stdin on both
> >Windows and Unix"
>
> Nice to know how, but all those double underscores made my eyes bleed.
> Three classes? What's wrong with something simple like the following
> (not tested on Unix)?
>
>
> import sys
> bims = sys.builtin_module_names
> if 'msvcrt' in bims:
>     # Windows
>     from msvcrt import getch
> elif 'termios' in bims:
>     # Unix
>     import tty, termios
>     def getch():
>         fd = sys.stdin.fileno()
>         old_settings = termios.tcgetattr(fd)
>         try:
>             tty.setraw(sys.stdin.fileno())
>             ch = sys.stdin.read(1)
>         finally:
>             termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
>         return ch
> else:
>     raise NotImplementedError, '... fill in Mac Carbon code here'

"Davor" <davorss at gmail.com> schrieb im Newsbeitrag
news:1106689788.676121.48490 at c13g2000cwb.googlegroups.com...
> Is it possible to write purely procedural code in Python, or the OO
> constructs in both language and supporting libraries have got so
> embedded that it's impossible to avoid them? Also, is anyone aware of
> any scripting language that could be considered as "Python minus OO
> stuff"? (As you can see I'm completely new to Python and initially
> believed it's a nice&simple scripting language before seeing all this
> OO stuff that was added in over time)
> Thanks,
> Davor

Here the OO "solution" (activestate recipe 134892):

class _Getch:
    """Gets a single character from standard input.  Does not echo to the
screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()


class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch


class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()


getch = _Getch()





More information about the Python-list mailing list