[capi-sig] String to number conversion

Rasmus Andersson rasmus at spotify.com
Wed May 7 18:28:49 CEST 2008


On 7 maj 2008, at 17.46, M.-A. Lemburg wrote:

> On 2008-05-07 17:27, Hrvoje Niksic wrote:
>> I miss a function able to convert the string representation of a
>> Python primitive number (int, long, float, complex) to an actual
>> number, basically the reverse of repr for numbers.
>> <snip>
>> Would anyone else find this kind of function useful?
>
> Yes.
>
> Perhaps as PyNumber_FromString() ?!

I rarely wrap primitives in objects, as this increases both memory  
usage, complexity and overhead.  (However, sometimes you _might_ need  
to, but I can't come up with a scenario)

So, in most cases this would probably happen:

PyObject *num = PyNumber_FromString(num_s);
if(!PyLong_Check(num)) {
   // Error...
}
else {
   self->event_id = PyLong_AsLongLong(num);
}

And you lose, because this would be simpler:

self->event_id = strtoll(PyString_AsString(num_s), (char *)NULL, 10);
if(errno == EINVAL) {
   // Error...
}

Meaning, you rarely need to take input which can be any kind of number  
manifested as a string and return it as any number manifested as a  
native number. (The examples above indicate the output type is known  
(signed 64bit integer))

Could you maybe give a real-world scenario where this would be needed?  
Where neither the input not the output number type is known.

>
>
> -- 
> Marc-Andre Lemburg
> eGenix.com
>
> Professional Python Services directly from the Source  (#1, May 07  
> 2008)
> >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
> >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
> >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
> ________________________________________________________________________
>
> :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for  
> free ! ::::
>
>
>   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
>    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>           Registered at Amtsgericht Duesseldorf: HRB 46611
> _______________________________________________
> capi-sig mailing list
> capi-sig at python.org
> http://mail.python.org/mailman/listinfo/capi-sig



÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Rasmus Andersson
Spotify
+46 733 117 326




More information about the capi-sig mailing list