473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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_pre_input_hook() function to send text after
a raw_input prompt, but this text cannot be edited.

Thanks in advance for any help.
Paraic.
Mar 22 '06 #1
13 3928
Paraic Gallagher enlightened us with:
What I am trying to do is provide a simple method for a user to
change a config file, for a test suite.


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
Mar 22 '06 #2
Sybren Stuvel wrote:
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

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.

Mar 22 '06 #3
Paraic Gallagher enlightened us with:
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.
Then explain to me why learning how to use your program to edit the
file is easier than using an already familiar program.
Moreover, I am curious as to whether somebody else out there knows
if there is a solution to the problem.


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
Mar 22 '06 #4

Paraic Gallagher wrote:
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_pre_input_hook() function to send text after
a raw_input prompt, but this text cannot be edited.


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

import readline
readline.set_startup_hook(lambda: readline.insert_text(old_value))
try:
new_value = raw_input()
finally:
readline.set_startup_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

Mar 22 '06 #5
David Wahler wrote:
With the disclaimer that, as others have said, this may not be the best
user-interface choice:

import readline
readline.set_startup_hook(lambda: readline.insert_text(old_value))
try:
new_value = raw_input()
finally:
readline.set_startup_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

Thanks David, this was exactly what I was looking for.

Paraic.
Mar 22 '06 #6
Sybren Stuvel <sy*******@YOURthirdtower.com.imagination> wrote:
Paraic Gallagher enlightened us with:
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.

Then explain to me why learning how to use your program to edit the
file is easier than using an already familiar program.


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 -- si***@chiark.greenend.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
Mar 23 '06 #7
Sion Arrowsmith enlightened us with:
You're assuming that the tester is already familiar with a text
editor.
Indeed. Someone working on a test suite sounded like someone who knows
how to work with a text editor.
And then they would have to learn the syntax of the configuration
file, and the parameters available.
True. Thorough comments and a clear syntax help a long way.
A point-and-drool interface requires no such learning.


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
Mar 23 '06 #8
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>",line 1, in ?
ValueError: invalid literal for in(): 0x62

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

Thanks!
May 14 '06 #9
Ognjen Bezanov enlightened us with:
Hi all, I am trying to convert a hexdecimal value to a char using this code:

print ' %c ' % int(0x62) This is an integer ^^^^

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

number = "62"
print ' %c ' % int("0x" + number) This is a string ^^^^^^^^^^^^^
How can I convert a string "0x62" to int/hex without this problem?


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
May 14 '06 #10
> How can I convert a string "0x62" to int/hex without this problem?

The call to int() takes an optional parameter for the base:
print int.__doc__ 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.
int('0x62', 16)

98

-tkc


May 14 '06 #11
Hi all, Another problem, with the same error (error: "invalid literal for
int()")

code:

mynums = "423.523.674.324.342.122.943.421.762.158.830"

mynumArray = string.split(mynums,".")

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
May 14 '06 #12
Am Sonntag 14 Mai 2006 22:23 schrieb Ognjen Bezanov:
mynums = "423.523.674.324.342.122.943.421.762.158.830"

mynumArray = string.split(mynums,".")
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)
x = 0
for nums in mynumArray:
This is misleading. Rename the variable to num, as it only contains a single
number.
if nums.isalnum() == true:
..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.
x = x + int(nums)
else:
print "Error, element contains some non-numeric characters"
As you don't know what the offending element is, insert a:

print nums

here.
break


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

--- Heiko.
May 14 '06 #13
> Hi all, Another problem, with the same error (error: "invalid literal for
int()")
Having the actual code would be helpful...
code:

mynums = "423.523.674.324.342.122.943.421.762.158.830"

mynumArray = string.split(mynums,".")

x = 0
for nums in mynumArray:
if nums.isalnum() == true:
This line would likely bomb, as in python, it's "True", not
"true" (unless you've defined lowercase versions elsewhere)
x = x + int(nums)
else:
print "Error, element contains some non-numeric characters"
break


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

mynums = "423.523.674.324.342.122.943.421.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



May 15 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
8
by: **Developer** | last post by:
Been reading some of the Marshal doc and am a little confused. Anyway, I've been using the following and it works OK. Public Declare Auto Function SHGetFolderPath Lib "shell32.dll" (ByVal hWnd...
21
by: planetthoughtful | last post by:
Hi All, As always, my posts come with a 'Warning: Newbie lies ahead!' disclaimer... I'm wondering if it's possible, using raw_input(), to provide a 'default' value with the prompt? I would...
3
by: David Greenberg | last post by:
Hi When we installed SqlServer2000 we left the default collation name (Sql_Latin1_General_CPI_CI_AS). The user defined databases we created afterwards were defined with a different collation name...
2
by: shapper | last post by:
Hello, How can I check if a parameter, for example "name", is available in a QueryString? I need to create a default value if the parameter is not available. Thanks, Miguel
1
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
2
by: SammyBar | last post by:
Hi all, I'm trying to convert the xml obtained from a XmlReader object into a UTF-8 array. My general idea is to read the XmlReader and write into a MemoryStream. Then convert the MemoryStream...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.