Connecting Tech Pros Worldwide Forums | Help | Site Map

Converting a string to the most probable type

Pierre Quentel
Guest
 
Posts: n/a
#1: Mar 6 '08
Hi,

I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)

Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123

I could code it myself, but this wheel is probably already invented

Regards,
Pierre

George Sakkis
Guest
 
Posts: n/a
#2: Mar 6 '08

re: Converting a string to the most probable type


On Mar 6, 9:27 am, Pierre Quentel <quentel.pie...@wanadoo.frwrote:
Quote:
Hi,
>
I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)
>
Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123
>
I could code it myself, but this wheel is probably already invented
Maybe, but that's a so domain-specific and easy to code wheel that
it's no big deal reinventing.

George
rockingred
Guest
 
Posts: n/a
#3: Mar 6 '08

re: Converting a string to the most probable type


On Mar 6, 3:20*pm, George Sakkis <george.sak...@gmail.comwrote:
Quote:
On Mar 6, 9:27 am, Pierre Quentel <quentel.pie...@wanadoo.frwrote:
>
>
>
>
>
Quote:
Hi,
>
Quote:
I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)
>
Quote:
Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123
>
Quote:
I could code it myself, but this wheel is probably already invented
>
Maybe, but that's a so domain-specific and easy to code wheel that
it's no big deal reinventing.
>
George- Hide quoted text -
>
- Show quoted text -
Actually you could probably write your own code very easily, a couple
of try/except clauses. I would recommend you try int() first, then
try float(), then try date check and when all else fails leave it a
string. However, it may be an interesting challenge for those who are
willing to make the attempt. "Define Cell/Field contents".
=?ISO-8859-1?Q?Luis_M=2E_Gonz=E1lez?=
Guest
 
Posts: n/a
#4: Mar 6 '08

re: Converting a string to the most probable type


On 6 mar, 11:27, Pierre Quentel <quentel.pie...@wanadoo.frwrote:
Quote:
Hi,
>
I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)
>
Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123
>
I could code it myself, but this wheel is probably already invented
>
Regards,
Pierre
Quote:
Quote:
Quote:
>>def convert(x):
if '.' in x:
try: return float(x)
except ValueError: return x
else:
try: return int(x)
except: return x

Quote:
Quote:
Quote:
>>convert('123')
123
Quote:
Quote:
Quote:
>>convert('123.99')
123.98999999999999
Quote:
Quote:
Quote:
>>convert('hello')
'hello'
hvendelbo.dev@googlemail.com
Guest
 
Posts: n/a
#5: Mar 7 '08

re: Converting a string to the most probable type


On Mar 6, 9:17 pm, Luis M. González <luis...@gmail.comwrote:
Quote:
On 6 mar, 11:27, Pierre Quentel <quentel.pie...@wanadoo.frwrote:
>
>
>
Quote:
Hi,
>
Quote:
I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)
>
Quote:
Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123
>
Quote:
I could code it myself, but this wheel is probably already invented
>
Quote:
Regards,
Pierre
Quote:
>def convert(x):
>
if '.' in x:
try: return float(x)
except ValueError: return x
else:
try: return int(x)
except: return x
>
Quote:
Quote:
>convert('123')
123
Quote:
Quote:
>convert('123.99')
123.98999999999999
Quote:
Quote:
>convert('hello')
>
'hello'
Neat solution. The real challenge though is whether to support
localised dates, these are all valid:
20/10/01
102001
20-10-2001
20011020
castironpi@gmail.com
Guest
 
Posts: n/a
#6: Mar 7 '08

re: Converting a string to the most probable type


And so on and so forth. *The tricky bit is how to tell the difference
Quote:
between Day, Month and Year.
There isn't one.
Paul Rubin
Guest
 
Posts: n/a
#7: Mar 8 '08

re: Converting a string to the most probable type


Pierre Quentel <quentel.pierre@wanadoo.frwrites:
Quote:
I would like to know if there is a module that converts a string to a
value of the "most probable type"
Python 2.4.4 (#1, Oct 23 2006, 13:58:00)
Quote:
Quote:
Quote:
>>import this
The Zen of Python, by Tim Peters
...
In the face of ambiguity, refuse the temptation to guess.
John Machin
Guest
 
Posts: n/a
#8: Mar 8 '08

re: Converting a string to the most probable type


On Mar 8, 11:13 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
Quote:
Pierre Quentel <quentel.pie...@wanadoo.frwrites:
Quote:
I would like to know if there is a module that converts a string to a
value of the "most probable type"
>
Python 2.4.4 (#1, Oct 23 2006, 13:58:00)
Quote:
Quote:
>>import this
The Zen of Python, by Tim Peters
...
In the face of ambiguity, refuse the temptation to guess.
Reminds me of the story about the avionics software for a fighter
plane which when faced with the situation of being exactly upside-down
refused to guess whether it should initiate a left roll or a right
roll :-)
Steven D'Aprano
Guest
 
Posts: n/a
#9: Mar 8 '08

re: Converting a string to the most probable type


On Fri, 07 Mar 2008 16:13:04 -0800, Paul Rubin wrote:
Quote:
Pierre Quentel <quentel.pierre@wanadoo.frwrites:
Quote:
>I would like to know if there is a module that converts a string to a
>value of the "most probable type"
>
Python 2.4.4 (#1, Oct 23 2006, 13:58:00)
Quote:
Quote:
>>import this
The Zen of Python, by Tim Peters
...
In the face of ambiguity, refuse the temptation to guess.
Good advice, but one which really only applies to libraries. At the
application level, sometimes (but not always, or even most times!)
guessing is the right thing to do.

E.g. spreadsheet applications don't insist on different syntax for
strings, dates and numbers. You can use syntax to force one or the other,
but by default the application will auto-detect what you want according
to relatively simple, predictable and intuitive rules:

* if the string looks like a date, it's a date;
* if it looks like a number, it's a number;
* otherwise it's a string.

Given the user-base of the application, the consequences of a wrong
guess, and the ease of fixing it, guessing is the right thing to do.

Auto-completion is another good example of guessing in the face of
ambiguity. It's not guessing that is bad, but what you do with the guess.


--
Steven
Pierre Quentel
Guest
 
Posts: n/a
#10: Mar 8 '08

re: Converting a string to the most probable type


>def convert(x):
Quote:
>
* * * * if '.' in x:
* * * * * * * * try: return float(x)
* * * * * * * * except ValueError: return x
* * * * else:
* * * * * * * * try: return int(x)
* * * * * * * * except: return x
>
Quote:
Quote:
>convert('123')
123
Quote:
Quote:
>convert('123.99')
123.98999999999999
Quote:
Quote:
>convert('hello')
>
Hi,

That's fine for people who write floats with a "." ; but others learn
to enter them with ","

For the same float, the French write the literal 123.456.789,99 when
others write 123,456,789.99 ; for us, today is 8/3/2008 (or
08/03/2008) where for others it's 3/8/2008 or perhaps 2008/3/8

Popular spreadsheets know how to "guess" literals ; if the guess is
not correct users can usually specify the pattern to use. My question
was just to know if something similar had already been developed in
Python ; I understand that the answer is no

Thanks,
Pierre
castironpi@gmail.com
Guest
 
Posts: n/a
#11: Mar 8 '08

re: Converting a string to the most probable type


On Mar 8, 12:05*pm, Pierre Quentel <quentel.pie...@wanadoo.frwrote:
Quote:
Quote:
Quote:
>>def convert(x):
>
Quote:
* * * * if '.' in x:
* * * * * * * * try: return float(x)
* * * * * * * * except ValueError: return x
* * * * else:
* * * * * * * * try: return int(x)
* * * * * * * * except: return x
>
Quote:
Quote:
>>convert('123')
123
Quote:
>>convert('123.99')
123.98999999999999
Quote:
>>convert('hello')
>
Hi,
>
That's fine for people who write floats with a "." ; but others learn
to enter them with ","
>
For the same float, the French write the literal 123.456.789,99 when
others write 123,456,789.99 ; for us, today is 8/3/2008 (or
08/03/2008) where for others it's 3/8/2008 or perhaps 2008/3/8
>
Popular spreadsheets know how to "guess" literals ; if the guess is
not correct users can usually specify the pattern to use. My question
was just to know if something similar had already been developed in
Python ; I understand that the answer is no
>
Thanks,
Pierre
In particular, you can retain access to the user/interface, and always
just query when probabilities aren't 100%. In general, retain access
to a higher power, such as offering a hook ( def butdonotguess( raw,
guesses ):). If you're making the decision, and a case-by-case is too
expensive, then you've made a policy, and someone gets shaft "'cause
policy". You can throw an exception that's guaranteed to be handled
or exits. '121212' isn't as bad as '121110'! If you want to find out
who and when writes dates like that, apply to PR. Obviously a person
knew at one point what the string was; how much is left?

P.S. def welltheydidthatthistime( raw, guesses ):.
cokofreedom@gmail.com
Guest
 
Posts: n/a
#12: Mar 10 '08

re: Converting a string to the most probable type


The trick in the case of when you do not want to guess, or the choices
grow too much, is to ask the user to tell you in what format they want
it and format according to their wishes.

Neatly avoids too much guessing and isn't much extra to add.
John Machin
Guest
 
Posts: n/a
#13: Mar 10 '08

re: Converting a string to the most probable type


On Mar 11, 3:19 am, cokofree...@gmail.com wrote:
Quote:
The trick in the case of when you do not want to guess, or the choices
grow too much, is to ask the user to tell you in what format they want
it and format according to their wishes.
>
Neatly avoids too much guessing and isn't much extra to add.
The plot is about understanding input, not formatting output.

Lie
Guest
 
Posts: n/a
#14: Mar 16 '08

re: Converting a string to the most probable type


On Mar 11, 4:15*am, John Machin <sjmac...@lexicon.netwrote:
Quote:
On Mar 11, 3:19 am, cokofree...@gmail.com wrote:
>
Quote:
The trick in the case of when you do not want to guess, or the choices
grow too much, is to ask the user to tell you in what format they want
it and format according to their wishes.
>
Quote:
Neatly avoids too much guessing and isn't much extra to add.
>
The plot is about understanding input, not formatting output.
And what he meant is simply to make an agreement with the user on how
he/she would format his/her input and to disallow input from formats
that haven't been agreed to avoid guessing. That is the cleanest and
most polite solution, although I'd suspect it would be considered less
user friendly by regular user although power user would be most happy
with that.
Closed Thread


Similar Python bytes