473,395 Members | 1,678 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,395 software developers and data experts.

C function in a Python context

To write quick C things that Python won't do up to speed. So it's got
a redundancy.

import ext
extA= ext.Ext()
extA[ 'enumfactors' ]= r"""
int enumfactors( int a, const char* sep ) {
int ret= 0, i;
for( i= 1; i<= a; i++ ) {
if( a% i== 0 ) {
ret+= 1;
if( i1 ) {
printf( "%s", sep );
}
printf( "%i", i );
}
}
printf( "\n" );
return ret;
}
""", ("i","i","s")

factorsn= extA.enumfactors( 209677683, ', ' )
print( "%i factors total."% factorsn )

import sys
sys.exit()
1, 3, 23, 69, 131, 393, 3013, 9039, 23197, 69591, 533531, 1600593,
3038807, 9116
421, 69892561, 209677683
16 factors total.
Feb 9 '08 #1
1 1136
#include <string>
#include <vector>
This modification required:

compilercommand= 'c:/programs/mingw/bin/g++'

and

strctypes= { 'i': 'int', 's': 'const char*',
'O': 'PyObject*' }

The output is:

#include <c:/programs/python/include/Python.h>

[ user code ]

static PyObject *
extcode_enumfactors(PyObject *self, PyObject *args) {
PyObject* result;
int arg0;
const char* arg1;
PyArg_ParseTuple(args, "is", &arg0, &arg1 );
result= enumfactors( arg0, arg1 );
return Py_BuildValue( "O", result );
}

static PyMethodDef ExtcodeMethods[] = {
{ "enumfactors", extcode_enumfactors, METH_VARARGS, "" },
{NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initextcode(void) {
(void) Py_InitModule("extcode", ExtcodeMethods);
}

The benefits are automatic parameter parsing and automatic method
table construction. The costs are portability and parameter parsing
flexibility. Add up.
Feb 19 '08 #2

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

Similar topics

3
by: Daniel Ehrenberg | last post by:
One of the most common bugs that people have on the Python Tutor list are caused by the fact the default arguments for functions are evaluated only once, not when the function is called. The...
12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
3
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .... >>> X.g <unbound method X.f>
40
by: Xah Lee | last post by:
is it possible in Python to create a function that maintains a variable value? something like this: globe=0; def myFun(): globe=globe+1 return globe
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...
38
by: Lasse Vågsæther Karlsen | last post by:
After working through a fair number of the challenges at www.mathschallenge.net, I noticed that some long-running functions can be helped *a lot* by caching their function results and retrieving...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
12
by: castironpi | last post by:
I want to talk to the newsgroup. As I have found that its readers will be non-trivially attentive, I esteem it a worthwhile production, for finely divided values of worth & while. (Disclaimer:...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.