A completely silly question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Ringer

    #16
    Re: A completely silly question

    On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:[color=blue]
    > This must be the silliest question ever:
    >
    > What about user input in Python? (like stdin)
    > Where can I find it? I can't find any references to it in the documentation.[/color]

    Under UNIX, I generally either use curses, or just put the terminal into
    raw mode:

    ..>>> def sane():
    ..... os.system("stty sane")
    .....
    ..>>> def raw():
    ..... os.system("stty raw")
    .....
    ..>>> raw()
    ..>>> x = sys.stdin.read( 1)
    a
    ..>>> sane()
    ..>>> x
    'a'

    but that's more for the benefit of others here, since you're on Windows.
    Needless to say this isn't portable.

    It can often be worth also using the 'echo' and 'noecho' arguments to
    stty to prevent characters getting echoed in ugly places. If you do much
    of this, it's probably worth just using curses, but if you have a fairly
    basic app that just needs to read raw characters sometimes this approach
    should be fine.

    --
    Craig Ringer


    Comment

    • Mike Meyer

      #17
      Re: A completely silly question

      Craig Ringer <craig@postnews papers.com.au> writes:
      [color=blue]
      > On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:[color=green]
      >> This must be the silliest question ever:
      >>
      >> What about user input in Python? (like stdin)
      >> Where can I find it? I can't find any references to it in the documentation.[/color]
      >
      > Under UNIX, I generally either use curses, or just put the terminal into
      > raw mode:
      >
      > .>>> def sane():
      > .... os.system("stty sane")
      > ....
      > .>>> def raw():
      > .... os.system("stty raw")[/color]

      The termios gives module gives you the tools to manipulate the tty
      directly, without invoking stty. The tty module gives you an easier
      interface to those routines. However, it's missing a setsane
      functions. Hmm. I think it's time for another PEP.

      <mike
      --
      Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

      Comment

      • Keith Dart

        #18
        Re: A completely silly question

        Mike Meyer wrote:[color=blue]
        > Craig Ringer <craig@postnews papers.com.au> writes:
        >
        >[color=green]
        >>On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:
        >>[color=darkred]
        >>>This must be the silliest question ever:
        >>>
        >>>What about user input in Python? (like stdin)
        >>>Where can I find it? I can't find any references to it in the documentation.[/color]
        >>
        >>Under UNIX, I generally either use curses, or just put the terminal into
        >>raw mode:
        >>
        >>.>>> def sane():
        >>.... os.system("stty sane")
        >>....
        >>.>>> def raw():
        >>.... os.system("stty raw")[/color]
        >
        >
        > The termios gives module gives you the tools to manipulate the tty
        > directly, without invoking stty. The tty module gives you an easier
        > interface to those routines. However, it's missing a setsane
        > functions. Hmm. I think it's time for another PEP.
        >
        > <mike[/color]

        In the pyNMS package (http://sourceforge.net/projects/pynms/) there is a
        module called "termtools" . This module can be used in place of the "tty"
        module. It has many improvements, including a "sane" function, a "raw"
        function, and an "stty" function. This module also replaces the
        "getpass" module, as it has the same functions found there. The PagedIO
        object is used by the CLI framework in pyNMS.






        --
        -- ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~
        Keith Dart <kdart@kdart.co m>
        public key: ID: F3D288E4
        =============== =============== =============== =============== =========

        Comment

        • David Bolen

          #19
          Re: A completely silly question

          "Fredrik Lundh" <fredrik@python ware.com> writes:
          [color=blue][color=green][color=darkred]
          > >> Well, but that's true as well for getchar() (at least in many cases of
          > >> interactive input and line buffering), so in that respect I do think
          > >> it's a fairly direct replacement, depending on how the OP was going to
          > >> use getchar() in the application.[/color]
          > >
          > > The OP said "wait for a single character input". sys.stdin.read( 1)
          > > waits for a newline.[/color]
          >
          > in the same same sentence, the OP also said that he wanted something like
          > C's getchar(). if you guys are going to read only parts of the original post,
          > you could at least try to read an entire sentence, before you start arguing...[/color]

          Not even sure what's there to argue about - getchar() does do single
          character input, so the OPs (full) original sentence seems plausible
          to me, and his example was using it in a while loop which I took to
          represent processing some input one character at a time.

          In any event - I also gave a way (Windows-specific) to truly obtain
          the single next character without any buffering, so just ignore any
          controversy in the first part of the response if desired.

          -- David

          Comment

          Working...