program in interactive mode

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • B.G.R.

    #1

    program in interactive mode

    Hi all,

    I'm working on an interpreter for a university subject, which is programmed
    in python under linux.
    I got most of the work done and I'm just trying to settle some problems I've
    found on my way.
    Right now, the most important is reading the user input. Everything goes
    through the standard input, and now I got something like this:

    numline=0
    for line in sys.stdin:
    numline+=1
    workwithline(li ne)

    A little bit more complex, but that's the idea. That will work if the user
    does something like "./myprog.py < code" or "cat code | ./myprog.py", and
    that's ok, but if the user only does "./myprog.py" then I got to get into
    interactive mode and show a prompt in every line expecting the user input
    for that line. Problem is I don't know how to tell if I've been "piped" or
    not.
    I used to think that the piped program doesn't know anything and it's just
    OS dependant to close and open the right descriptors, but I'm not sure
    anymore. Any help or pointer in the right direction would be greatly
    appreciated.

    Happy christmas everyone,

    RGB




  • M.E.Farmer

    #2
    Re: program in interactive mode

    B.G.R. wrote:[color=blue]
    > Hi all,[/color]
    [snip][color=blue]
    > A little bit more complex, but that's the idea. That will work if the[/color]
    user[color=blue]
    > does something like "./myprog.py < code" or "cat code | ./myprog.py",[/color]
    and[color=blue]
    > that's ok, but if the user only does "./myprog.py" then I got to get[/color]
    into[color=blue]
    > interactive mode and show a prompt in every line expecting the user[/color]
    input[color=blue]
    > for that line. Problem is I don't know how to tell if I've been[/color]
    "piped" or[color=blue]
    > not.
    > I used to think that the piped program doesn't know anything and it's[/color]
    just[color=blue]
    > OS dependant to close and open the right descriptors, but I'm not[/color]
    sure[color=blue]
    > anymore. Any help or pointer in the right direction would be greatly
    > appreciated.
    >
    > Happy christmas everyone,
    >
    > RGB[/color]
    Hello RGB,
    what you are loking for is sys.stdin.isatt y()

    py> if sys.stdin.isatt y():
    .... print 'Console'
    .... else:
    .... print 'Redirected'

    Hth,
    M.E.Farmer

    Comment

    • Alex Martelli

      #3
      Re: program in interactive mode

      B.G.R. <nospam@yahoo.t k> wrote:
      ...[color=blue]
      > numline=0
      > for line in sys.stdin:
      > numline+=1
      > workwithline(li ne)[/color]

      Consider the alternative:
      for numline, line in enumerate(sys.s tdin):

      Nothing to do with your main program, but still neater...
      [color=blue]
      > that's ok, but if the user only does "./myprog.py" then I got to get into
      > interactive mode and show a prompt in every line expecting the user input
      > for that line. Problem is I don't know how to tell if I've been "piped" or[/color]

      sys.stdin.isatt y() should serve you well.


      Alex

      Comment

      • B.G.R.

        #4
        Re: program in interactive mode


        "Alex Martelli" <aleaxit@yahoo. com> escribió en el mensaje
        news:1gpf7qp.c9 e4w97cdazzN%ale axit@yahoo.com. ..[color=blue]
        > B.G.R. <nospam@yahoo.t k> wrote:
        > ...[color=green]
        > > numline=0
        > > for line in sys.stdin:
        > > numline+=1
        > > workwithline(li ne)[/color]
        >
        > Consider the alternative:
        > for numline, line in enumerate(sys.s tdin):
        >
        > Nothing to do with your main program, but still neater...
        >[color=green]
        > > that's ok, but if the user only does "./myprog.py" then I got to get[/color][/color]
        into[color=blue][color=green]
        > > interactive mode and show a prompt in every line expecting the user[/color][/color]
        input[color=blue][color=green]
        > > for that line. Problem is I don't know how to tell if I've been "piped"[/color][/color]
        or[color=blue]
        >
        > sys.stdin.isatt y() should serve you well.
        >
        >
        > Alex[/color]


        Thank you all very much for your help and tips, that's exactly what I was
        looking for.
        I guess my knowledge of the libraries is still quite limited. Time to work
        on that too.

        Regards
        RGB



        Comment

        • Scott David Daniels

          #5
          Re: program in interactive mode

          B.G.R. wrote:[color=blue]
          > numline=0
          > for line in sys.stdin:
          > numline+=1
          > workwithline(li ne)[/color]

          I'd use:

          for numline, line in enumerate(sys.s tdin):
          workwithline(li ne)

          Note: The line numbers start at 0, but that is often acceptable.

          --Scott David Daniels
          Scott.Daniels@A cm.Org

          Comment

          • Mike Meyer

            #6
            Re: program in interactive mode

            "B.G.R." <nospam@yahoo.t k> writes:
            [color=blue]
            > Hi all,
            >
            > I'm working on an interpreter for a university subject, which is programmed
            > in python under linux.
            > I got most of the work done and I'm just trying to settle some problems I've
            > found on my way.
            > Right now, the most important is reading the user input. Everything goes
            > through the standard input, and now I got something like this:
            >
            > numline=0
            > for line in sys.stdin:
            > numline+=1
            > workwithline(li ne)
            >
            > A little bit more complex, but that's the idea. That will work if the user
            > does something like "./myprog.py < code" or "cat code | ./myprog.py", and
            > that's ok, but if the user only does "./myprog.py" then I got to get into
            > interactive mode and show a prompt in every line expecting the user input
            > for that line. Problem is I don't know how to tell if I've been "piped" or
            > not.
            > I used to think that the piped program doesn't know anything and it's just
            > OS dependant to close and open the right descriptors, but I'm not sure
            > anymore. Any help or pointer in the right direction would be greatly
            > appreciated.[/color]

            I've discovered a truly elegant trick with python programs that
            interpret other data. You make them ignore lines that start with # at
            the beginning of the line, and accept the name of a file to be
            interpreted as a first argument. Your users can then put

            #!/usr/bin/env mycode.py

            at the top of their files, and then treat their files as normal
            executables. mycode.py has to be on their path; if not, they need to
            plug in the full path to mycode.py.

            I save the state of TkInter programs by writing this out then pickling
            the objects that define the state to the file. Executing that file
            will bring the program back up in the same state it was saved in.

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

            Comment

            • John Machin

              #7
              Re: program in interactive mode


              Mike Meyer wrote:[color=blue]
              >
              > I've discovered a truly elegant trick with python programs that
              > interpret other data.[/color]

              Q0. Other than what?
              [color=blue]
              > You make them ignore lines that start with # at
              > the beginning of the line,[/color]

              Q1. After the first user accidentally gets a # at the start of a real
              data line, a few hundred lines into their file, then what will you do?
              Fix your script to detect this error and re-issue your documentation,
              emphasising that this is not a general comment convention?

              Q2. Then when users 2+ steam up complaining that they have stacks of
              files containing lines like "#### Next section is frappenwanger
              readings in picoHertz ####", and the script is printing out a whole lot
              of what they regard as gobbledegook followed by
              "HashmarkAtStar tOfOtherThanLin eZeroError", and then stopping, what do
              you do?
              [color=blue]
              > and accept the name of a file to be
              > interpreted as a first argument. Your users can then put
              >
              > #!/usr/bin/env mycode.py[/color]

              Q3. Will that work on 'Doze?

              Q4. Doesn't that tie their file to your script, or force other scripts
              to ignore the first line?
              [color=blue]
              >
              > at the top of their files, and then treat their files as normal
              > executables. mycode.py has to be on their path; if not, they need to
              > plug in the full path to mycode.py.[/color]

              Q5. For comparison purposes, could you please post an example of what
              you regard as a filthy ugly trick?

              Comment

              • Mike Meyer

                #8
                Re: program in interactive mode

                "John Machin" <sjmachin@lexic on.net> writes:
                [color=blue]
                > Mike Meyer wrote:[color=green]
                >>
                >> I've discovered a truly elegant trick with python programs that
                >> interpret other data.[/color]
                > Q0. Other than what?[/color]

                Other than Python code.
                [color=blue][color=green]
                >> You make them ignore lines that start with # at
                >> the beginning of the line,[/color]
                > Q1. After the first user accidentally gets a # at the start of a real
                > data line, a few hundred lines into their file, then what will you do?
                > Fix your script to detect this error and re-issue your documentation,
                > emphasising that this is not a general comment convention?[/color]

                Depends on how you implement it. Possibly issue an error
                message. Possibly treat this as data. Possibly treat this as a comment.
                [color=blue]
                > Q2. Then when users 2+ steam up complaining that they have stacks of
                > files containing lines like "#### Next section is frappenwanger
                > readings in picoHertz ####", and the script is printing out a whole lot
                > of what they regard as gobbledegook followed by
                > "HashmarkAtStar tOfOtherThanLin eZeroError", and then stopping, what do
                > you do?[/color]

                You don't implement the hashmark that way, of course.
                [color=blue][color=green]
                >> and accept the name of a file to be
                >> interpreted as a first argument. Your users can then put
                >>
                >> #!/usr/bin/env mycode.py[/color]
                > Q3. Will that work on 'Doze?[/color]

                Probably not. I don't know if this is part of the Posix compatability
                level or not.
                [color=blue]
                > Q4. Doesn't that tie their file to your script, or force other scripts
                > to ignore the first line?[/color]

                This trick is really only applicable to data where you control the
                file format. As I mentioned, I use it to treat pickled program
                configuration files as executables.
                [color=blue][color=green]
                >> at the top of their files, and then treat their files as normal
                >> executables. mycode.py has to be on their path; if not, they need to
                >> plug in the full path to mycode.py.[/color]
                >
                > Q5. For comparison purposes, could you please post an example of what
                > you regard as a filthy ugly trick?[/color]

                f = __import__(__na me__)
                f.__dict__['name'] = value

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

                Comment

                Working...