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

Call the python function from C compiler

Expand|Select|Wrap|Line Numbers
  1. from ctypes import *
  2. class HELLO(Structure):
  3.     _fields_=[("a",c_int),
  4.               ("b",c_int)]
  5. x=input()
  6. y=input()
  7. hai=HELLO(x, y)
  8. print hai.a,hai.b
This my code in python.now i can call the function HELLO(x,y) from c compiler and also can i see the output of this code in C compiler window...Is it possible....If its possible plz give idea...
Mar 22 '07 #1
4 1298
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. from ctypes import *
  2. class HELLO(Structure):
  3.     _fields_=[("a",c_int),
  4.               ("b",c_int)]
  5. x=input()
  6. y=input()
  7. hai=HELLO(x, y)
  8. print hai.a,hai.b
This my code in python.now i can call the function HELLO(x,y) from c compiler and also can i see the output of this code in C compiler window...Is it possible....If its possible plz give idea...
Not possible. ctypes is used for calling C library functions with C type arguments FROM Python.
Mar 22 '07 #2
ok....Is there any possible to write structure in c wrapper code
Example this is my c wrapper code for factorial ok....
Expand|Select|Wrap|Line Numbers
  1. #include <Python.h>
  2.  
  3. int fact(int n)
  4. {
  5.     if ((n == 0)||(n==1))
  6.         return 1;
  7.     else
  8.         return fact(n*fact(n-1));
  9. }
  10.  
  11. static PyObject* fact(PyObject* self, PyObject* args)
  12. {
  13.     const char *command;
  14.     int n;
  15.  
  16.     if (!PyArg_ParseTuple(args, "i", &n))
  17.         return NULL;
  18.  
  19.     return Py_BuildValue("i", fact(n));
  20. }
  21.  
  22. static PyMethodDef FactMethods[] = {
  23.     {"fact", fact, METH_VARARGS, "Calculate the Factorial of given numbers."},
  24.     {NULL, NULL, 0, NULL}
  25. };
  26.  
  27. PyMODINIT_FUNC
  28. initfact(void)
  29. {
  30.     (void) Py_InitModule("fact", FactMethods);
  31. }

here i could use fact() function calling .......instead of fuction Is there any possible to write structure in this code
Mar 22 '07 #3
bartonc
6,596 Expert 4TB
ok....Is there any possible to write structure in c wrapper code
Example this is my c wrapper code for factorial ok....
Expand|Select|Wrap|Line Numbers
  1. #include <Python.h>
  2.  
  3. int fact(int n)
  4. {
  5.     if ((n == 0)||(n==1))
  6.         return 1;
  7.     else
  8.         return fact(n*fact(n-1));
  9. }
  10.  
  11. static PyObject* fact(PyObject* self, PyObject* args)
  12. {
  13.     const char *command;
  14.     int n;
  15.  
  16.     if (!PyArg_ParseTuple(args, "i", &n))
  17.         return NULL;
  18.  
  19.     return Py_BuildValue("i", fact(n));
  20. }
  21.  
  22. static PyMethodDef FactMethods[] = {
  23.     {"fact", fact, METH_VARARGS, "Calculate the Factorial of given numbers."},
  24.     {NULL, NULL, 0, NULL}
  25. };
  26.  
  27. PyMODINIT_FUNC
  28. initfact(void)
  29. {
  30.     (void) Py_InitModule("fact", FactMethods);
  31. }

here i could use fact() function calling .......instead of fuction Is there any possible to write structure in this code
Looks like you are on the right track.
And you are over my head with all that C stuff, but I'm sure that what you want to do is possible.
Mar 22 '07 #4
Anyway thanks your reply........i dont know its possible or not........ok thanks da
Mar 22 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: kbass | last post by:
In different articles that I have read, persons have constantly eluded to the productivity gains of Python. One person stated that Python's productivity gain was 5 to 10 times over Java in some in...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
58
by: Svein Ove Aas | last post by:
Is anyone working on a python-to-native compiler? I'd be interested in taking a look. Come to think of it, is anyone working on a sexpr-enabled version of Python, or anything similar? I really...
8
by: Lucas Lemmens | last post by:
Dear pythonians, I've been reading/thinking about the famous function call speedup trick where you use a function in the local context to represent a "remoter" function to speed up the 'function...
19
by: Riko Wichmann | last post by:
hi everyone, I'm googeling since some time, but can't find an answer - maybe because the answer is 'No!'. Can I call a function in python inline, so that the python byte compiler does...
118
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely...
16
by: per9000 | last post by:
Hi, I recently started working a lot more in python than I have done in the past. And I discovered something that totally removed the pretty pink clouds of beautifulness that had surrounded my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.