Default/editable string to raw_input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paraic Gallagher

    #1

    Default/editable string to raw_input

    Hi,

    This is my first post to the list, I hope somebody can help me with this
    problem. Apologies if it has been posted before but I have been internet
    searching to no avail.

    What I am trying to do is provide a simple method for a user to change a
    config file, for a test suite.
    The config file consists of a number of keys, eg. build number, target
    device, etc.

    What I would like to do is give the user a key, and the previous value
    for that key.
    The user can then accept the previous value or edit it on the line.

    I have a method where the previous value of a key is presented to the
    user and can be accepted if the user hits return. But as some of the
    values are somewhat long, and the user may only need to change 1 or 2
    characters, it would be a nice feature to offer line editing.

    I can get the readline.set_pr e_input_hook() function to send text after
    a raw_input prompt, but this text cannot be edited.

    Thanks in advance for any help.
    Paraic.
  • Sybren Stuvel

    #2
    Re: Default/editable string to raw_input

    Paraic Gallagher enlightened us with:[color=blue]
    > What I am trying to do is provide a simple method for a user to
    > change a config file, for a test suite.[/color]

    My opinion: let the user edit the configuration file using his/her
    favourite text editor. Someone configuring a test suite should
    certainly be able to edit a text file.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa

    Comment

    • Paraic Gallagher

      #3
      Re: Default/editable string to raw_input

      Sybren Stuvel wrote:
      [color=blue]
      >My opinion: let the user edit the configuration file using his/her
      >favourite text editor. Someone configuring a test suite should
      >certainly be able to edit a text file.
      >
      >Sybren
      >
      >[/color]
      While I agree in principal to your opinion, the idea is that an absolute
      moron
      would be able to configure a testcell with smallest amount of effort
      possible.

      Moreover, I am curious as to whether somebody else out there knows if
      there is a solution to the problem.

      Paraic.

      Comment

      • Sybren Stuvel

        #4
        Re: Default/editable string to raw_input

        Paraic Gallagher enlightened us with:[color=blue]
        > While I agree in principal to your opinion, the idea is that an
        > absolute moron would be able to configure a testcell with smallest
        > amount of effort possible.[/color]

        Then explain to me why learning how to use your program to edit the
        file is easier than using an already familiar program.
        [color=blue]
        > Moreover, I am curious as to whether somebody else out there knows
        > if there is a solution to the problem.[/color]

        Give a default answer that's chosen when the user presses enter
        without providing an answer of his own.

        Sybren
        --
        The problem with the world is stupidity. Not saying there should be a
        capital punishment for stupidity, but why don't we just take the
        safety labels off of everything and let the problem solve itself?
        Frank Zappa

        Comment

        • David Wahler

          #5
          Re: Default/editable string to raw_input


          Paraic Gallagher wrote:[color=blue]
          > Hi,
          >
          > This is my first post to the list, I hope somebody can help me with this
          > problem. Apologies if it has been posted before but I have been internet
          > searching to no avail.
          >
          > What I am trying to do is provide a simple method for a user to change a
          > config file, for a test suite.
          > The config file consists of a number of keys, eg. build number, target
          > device, etc.
          >
          > What I would like to do is give the user a key, and the previous value
          > for that key.
          > The user can then accept the previous value or edit it on the line.
          >
          > I have a method where the previous value of a key is presented to the
          > user and can be accepted if the user hits return. But as some of the
          > values are somewhat long, and the user may only need to change 1 or 2
          > characters, it would be a nice feature to offer line editing.
          >
          > I can get the readline.set_pr e_input_hook() function to send text after
          > a raw_input prompt, but this text cannot be edited.[/color]

          With the disclaimer that, as others have said, this may not be the best
          user-interface choice:

          import readline
          readline.set_st artup_hook(lamb da: readline.insert _text(old_value ))
          try:
          new_value = raw_input()
          finally:
          readline.set_st artup_hook(None )

          Note that, among other issues, this isn't threadsafe (but then, you
          really shouldn't be doing console I/O from multiple threads anyway).
          Hope this helps.

          -- David

          Comment

          • Paraic Gallagher

            #6
            Re: Default/editable string to raw_input

            David Wahler wrote:
            [color=blue]
            >With the disclaimer that, as others have said, this may not be the best
            >user-interface choice:
            >
            > import readline
            > readline.set_st artup_hook(lamb da: readline.insert _text(old_value ))
            > try:
            > new_value = raw_input()
            > finally:
            > readline.set_st artup_hook(None )
            >
            >Note that, among other issues, this isn't threadsafe (but then, you
            >really shouldn't be doing console I/O from multiple threads anyway).
            >Hope this helps.
            >
            >-- David
            >
            >
            >[/color]
            Thanks David, this was exactly what I was looking for.

            Paraic.


            Comment

            • Sion Arrowsmith

              #7
              Re: Default/editable string to raw_input

              Sybren Stuvel <sybrenUSE@YOUR thirdtower.com. imagination> wrote:[color=blue]
              >Paraic Gallagher enlightened us with:[color=green]
              >> While I agree in principal to your opinion, the idea is that an
              >> absolute moron would be able to configure a testcell with smallest
              >> amount of effort possible.[/color]
              >Then explain to me why learning how to use your program to edit the
              >file is easier than using an already familiar program.[/color]

              You're assuming that the tester is already familiar with a text
              editor. And then they would have to learn the syntax of the
              configuration file, and the parameters available. A point-and-
              drool interface requires no such learning.

              --
              \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
              ___ | "Frankly I have no feelings towards penguins one way or the other"
              \X/ | -- Arthur C. Clarke
              her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

              Comment

              • Sybren Stuvel

                #8
                Re: Default/editable string to raw_input

                Sion Arrowsmith enlightened us with:[color=blue]
                > You're assuming that the tester is already familiar with a text
                > editor.[/color]

                Indeed. Someone working on a test suite sounded like someone who knows
                how to work with a text editor.
                [color=blue]
                > And then they would have to learn the syntax of the configuration
                > file, and the parameters available.[/color]

                True. Thorough comments and a clear syntax help a long way.
                [color=blue]
                > A point-and-drool interface requires no such learning.[/color]

                But that's not what you get with raw_input().

                Sybren
                --
                The problem with the world is stupidity. Not saying there should be a
                capital punishment for stupidity, but why don't we just take the
                safety labels off of everything and let the problem solve itself?
                Frank Zappa

                Comment

                • Ognjen Bezanov

                  #9
                  Converting hex to char help

                  Hi all, I am trying to convert a hexdecimal value to a char using this code:

                  print ' %c ' % int(0x62)

                  this works fine, but if I want to do this:

                  number = "62"
                  print ' %c ' % int("0x" + number)

                  I get an error:

                  Traceback (most recent call last):
                  File "<stdin>",l ine 1, in ?
                  ValueError: invalid literal for in(): 0x62

                  How can I convert a string "0x62" to int/hex without this problem?

                  Thanks!


                  Comment

                  • Sybren Stuvel

                    #10
                    Re: Converting hex to char help

                    Ognjen Bezanov enlightened us with:[color=blue]
                    > Hi all, I am trying to convert a hexdecimal value to a char using this code:
                    >
                    > print ' %c ' % int(0x62)[/color]
                    This is an integer ^^^^

                    [color=blue]
                    > this works fine, but if I want to do this:
                    >
                    > number = "62"
                    > print ' %c ' % int("0x" + number)[/color]
                    This is a string ^^^^^^^^^^^^^
                    [color=blue]
                    > How can I convert a string "0x62" to int/hex without this problem?[/color]

                    In [0]: exec('num=0x%s' % '62')

                    In [1]: num
                    Out[1]: 98


                    Sybren
                    --
                    The problem with the world is stupidity. Not saying there should be a
                    capital punishment for stupidity, but why don't we just take the
                    safety labels off of everything and let the problem solve itself?
                    Frank Zappa

                    Comment

                    • Tim Chase

                      #11
                      Re: Converting hex to char help

                      > How can I convert a string "0x62" to int/hex without this problem?

                      The call to int() takes an optional parameter for the base:
                      [color=blue][color=green][color=darkred]
                      >>> print int.__doc__[/color][/color][/color]
                      int(x[, base]) -> integer

                      Convert a string or number to an integer, if possible. A
                      floating point argument will be truncated towards zero (this
                      does not include a string representation of a floating point
                      number!) When converting a string, use the optional base.
                      It is an error to supply a base when converting a
                      non-string. If the argument is outside the integer range a
                      long object will be returned instead.
                      [color=blue][color=green][color=darkred]
                      >>> int('0x62', 16)[/color][/color][/color]
                      98

                      -tkc




                      Comment

                      • Ognjen Bezanov

                        #12
                        Re: Converting String to int

                        Hi all, Another problem, with the same error (error: "invalid literal for
                        int()")

                        code:

                        mynums = "423.523.674.32 4.342.122.943.4 21.762.158.830"

                        mynumArray = string.split(my nums,".")

                        x = 0
                        for nums in mynumArray:
                        if nums.isalnum() == true:
                        x = x + int(nums)
                        else:
                        print "Error, element contains some non-numeric characters"
                        break


                        /end code

                        This seemed like a simple thing, and I have done it before with no issues.
                        have I missed something obvious? taking into account my previous hex
                        question, I tried changing int(nums) to int(nums,10) but it still gives me
                        the error


                        Comment

                        • Heiko Wundram

                          #13
                          Re: Converting String to int

                          Am Sonntag 14 Mai 2006 22:23 schrieb Ognjen Bezanov:[color=blue]
                          > mynums = "423.523.674.32 4.342.122.943.4 21.762.158.830"
                          >
                          > mynumArray = string.split(my nums,".")[/color]

                          This is the old way of using string functions using the module string. You
                          should only write this as:

                          mynumArray = mynums.split(". ")

                          (using the string methods of string objects directly)
                          [color=blue]
                          > x = 0
                          > for nums in mynumArray:[/color]

                          This is misleading. Rename the variable to num, as it only contains a single
                          number.
                          [color=blue]
                          > if nums.isalnum() == true:[/color]

                          ..isalnum() checks whether the string consists of _alpha_-numeric characters
                          only. So, in this case, it may contain letters, among digits. .isdigit()
                          checks whether it is a (base <= 10) number.
                          [color=blue]
                          > x = x + int(nums)
                          > else:
                          > print "Error, element contains some non-numeric characters"[/color]

                          As you don't know what the offending element is, insert a:

                          print nums

                          here.
                          [color=blue]
                          > break[/color]

                          If you change the code as noted above, it works fine for me.

                          --- Heiko.

                          Comment

                          • Tim Chase

                            #14
                            Re: Converting String to int

                            > Hi all, Another problem, with the same error (error: "invalid literal for[color=blue]
                            > int()")[/color]

                            Having the actual code would be helpful...
                            [color=blue]
                            > code:
                            >
                            > mynums = "423.523.674.32 4.342.122.943.4 21.762.158.830"
                            >
                            > mynumArray = string.split(my nums,".")
                            >
                            > x = 0
                            > for nums in mynumArray:
                            > if nums.isalnum() == true:[/color]

                            This line would likely bomb, as in python, it's "True", not
                            "true" (unless you've defined lowercase versions elsewhere)
                            [color=blue]
                            > x = x + int(nums)
                            > else:
                            > print "Error, element contains some non-numeric characters"
                            > break[/color]

                            However, I modified your code just a spot, and it worked
                            like a charm:

                            mynums = "423.523.674.32 4.342.122.943.4 21.762.158.830"
                            mynumArray = mynums.split(". ")
                            x = 0
                            for num in mynumArray:
                            if num.isdigit():
                            x = x + int(num)
                            else:
                            print "Error"
                            break

                            and it worked fine.

                            A more pythonic way may might be

                            x = sum([int(q) for q in mynumArray if q.isdigit()])

                            or, if you don't need mynumArray for anything, you can just use

                            x = sum([int(q) for q in mynum.split("." ) if q.isdigit()])

                            Hope this gives you some stuff to work with,

                            -tkc









                            Comment

                            Working...