473,216 Members | 2,110 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,216 software developers and data experts.

Another C API Question

Hi,

I run into another C API question. What is the simplest way to convert
an PyObject into a double?

For example, I have

PyObject *obj;

I know obj is a number, but I do not know the exact type. How can I
convert it to double without writing a giant switch() that exhausts
every single type of number?

Thanks,
beginner

Jul 27 '07 #1
9 1679
beginner wrote:
Hi,

I run into another C API question. What is the simplest way to convert
an PyObject into a double?

For example, I have

PyObject *obj;

I know obj is a number, but I do not know the exact type. How can I
convert it to double without writing a giant switch() that exhausts
every single type of number?
Convert it to a Python float using PyNumber_Float(), then use PyFloat_AsDouble()
to get the C double value from it.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jul 27 '07 #2
beginner wrote:
I know obj is a number, but I do not know the exact type. How can I
convert it to double without writing a giant switch() that exhausts
every single type of number?
Try using the PyFloat_AsDouble(...) function, it should be able to
convert an object to a double, as long as the object implements the
__float__ method.
Jul 27 '07 #3
Hi Robert,

On Jul 26, 8:16 pm, Robert Kern <robert.k...@gmail.comwrote:
beginner wrote:
Hi,
I run into another C API question. What is the simplest way to convert
an PyObject into a double?
For example, I have
PyObject *obj;
I know obj is a number, but I do not know the exact type. How can I
convert it to double without writing a giant switch() that exhausts
every single type of number?

Convert it to a Python float using PyNumber_Float(), then use PyFloat_AsDouble()
to get the C double value from it.
Thanks a lot for your help.

Best regards,
beginner
Jul 27 '07 #4
Hi Farshid,

On Jul 26, 8:18 pm, Farshid Lashkari <n...@spam.comwrote:
beginner wrote:
I know obj is a number, but I do not know the exact type. How can I
convert it to double without writing a giant switch() that exhausts
every single type of number?

Try using the PyFloat_AsDouble(...) function, it should be able to
convert an object to a double, as long as the object implements the
__float__ method.
This works with PyFloat only. It does not work with integers.

Thanks,
b

Jul 27 '07 #5
beginner wrote:
This works with PyFloat only. It does not work with integers.
Did you try it out? I have used it on ints, bools, and objects that
implement the __float__ method. It does not work on strings though.
Jul 27 '07 #6
On Jul 27, 11:37 am, Farshid Lashkari <n...@spam.comwrote:
beginner wrote:
This works with PyFloat only. It does not work with integers.

Did you try it out? I have used it on ints, bools, and objects that
implement the __float__ method. It does not work on strings though.
I did and it did not seem to work. I ended up doing the following.
Verbose, isn't it?
If I do d=PyFloat_AsDouble(oDiscount); in the third "if", I get an
error. Maybe I missed something obvious.
if(oDiscount==Py_None)
{
}
else if(PyFloat_Check(oDiscount))
{
d=PyFloat_AsDouble(oDiscount);
}
else if(PyNumber_Check(oDiscount))
{
PyObject *pf=PyNumber_Float(oDiscount);
if(pfDiscount)
{
d=PyFloat_AsDouble(pfDiscount);
Py_DECREF(pfDiscount);
}
}
else ...

Jul 27 '07 #7
beginner wrote:
I did and it did not seem to work. I ended up doing the following.
Verbose, isn't it?
If I do d=PyFloat_AsDouble(oDiscount); in the third "if", I get an
error. Maybe I missed something obvious.
That's strange. I just tried the following code:

fprintf(stdout,"True = %lf\n",PyFloat_AsDouble(Py_True));
fprintf(stdout,"False = %lf\n",PyFloat_AsDouble(Py_False));
fprintf(stdout,"5 = %lf\n",PyFloat_AsDouble(PyInt_FromLong(5)));

And it printed the following:

True = 1.000000
False = 0.000000
5 = 5.000000

What version of Python are you using?
Jul 27 '07 #8
On Jul 27, 4:50 pm, Farshid Lashkari <n...@spam.comwrote:
beginner wrote:
I did and it did not seem to work. I ended up doing the following.
Verbose, isn't it?
If I do d=PyFloat_AsDouble(oDiscount); in the third "if", I get an
error. Maybe I missed something obvious.

That's strange. I just tried the following code:

fprintf(stdout,"True = %lf\n",PyFloat_AsDouble(Py_True));
fprintf(stdout,"False = %lf\n",PyFloat_AsDouble(Py_False));
fprintf(stdout,"5 = %lf\n",PyFloat_AsDouble(PyInt_FromLong(5)));

And it printed the following:

True = 1.000000
False = 0.000000
5 = 5.000000

What version of Python are you using?
Interesting. Let me try it again. I definitely want to remove the
intermediate step that creates a temporary Py object. Well, Python is
probably doing it internally anyway, but at least I don't have to see
it. :-)

Jul 27 '07 #9
On Jul 27, 4:50 pm, Farshid Lashkari <n...@spam.comwrote:
beginner wrote:
I did and it did not seem to work. I ended up doing the following.
Verbose, isn't it?
If I do d=PyFloat_AsDouble(oDiscount); in the third "if", I get an
error. Maybe I missed something obvious.

That's strange. I just tried the following code:

fprintf(stdout,"True = %lf\n",PyFloat_AsDouble(Py_True));
fprintf(stdout,"False = %lf\n",PyFloat_AsDouble(Py_False));
fprintf(stdout,"5 = %lf\n",PyFloat_AsDouble(PyInt_FromLong(5)));

And it printed the following:

True = 1.000000
False = 0.000000
5 = 5.000000

What version of Python are you using?
I am using 2.5.1 and Windows XP. Thanks for your help.

Jul 27 '07 #10

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

Similar topics

3
by: fig000 | last post by:
Hi, I'm relatively new to Javascript so please bear with me on what might sound like silly questions. This is what I want to do: I'm working in classic asp (I have to for this project). I...
6
by: anon | last post by:
Post Forwarding question...... For this control below, <asp:Button runat="server" PostTargetUrl="page2.aspx" /> The Attribute: PostTargetUrl="page2.aspx" Is this PostTargetUrl Attribute...
2
by: terence.parker | last post by:
I am often faced with the dilemma of whether to use a JOIN query across three tables in order to grab a bunch of results - or whether to create another table to represent what I want. The latter is...
0
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
3
by: Brad | last post by:
I have another hopefully simple question. I am so used to writing VB .Net windows apps that there are some things in ASP .Net that just does not easily cross over. I know how to pass variables to...
27
by: Javier Martinez | last post by:
Hi I have asp application in a machine with a virtual directory referring a shared directory in another machine When I try to load any aspx page of my portal I get the following error: ...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
24
by: David Mathog | last post by:
On a Solaris 8 system if a user "joe" logs in, for instance via ssh, cuserid() returns "joe". That's the expected behavior and so far so good. However if that user then does: % su - sally ...
13
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid control (doesn't have to be a grid control, whatever...
4
by: MichaelK | last post by:
Hello. I have all data already collected on the current page? I want to open another window with the form, fill the fields and submit that form. So basically the question is how can I fill all...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.