method

How to creating python module from C

9 Mar , 2012  

C always provide light-weight processing among all high-level programming language. Using python give advantages which we can extend into C as python modules. There are several workflow we should know before starting.

1. Naming Convention
Preferable we create C files always followed with module to indicating it’s a “Module”.
Eg:

1
temperaturemodule.c

2. Using Python Dev API
We should have “python-dev” installed in our Linux or Ubuntu.
Then we can call Python API to wrap C codes as python modules.

3. Use setup.py
Because we will treat C extension as python modules, we should provide setup.py installation.

At this example, I will show how to write “temperature” module in C and extend into python as module.

First, we create temperaturemodule.c. Then we build design by :

1
2
3
4
5
6
7
8
9
#INCLUDE HEADER

#CREATE METHOD fahrenheit_to_celcius

#CREATE METHOD celcius_to_fahrenheit

#REGISTER ALL METHODS

#INITIALIZE

1. #INCLUDE HEADER
We should call python header “Python.h”

2. #CREATE METHOD fahrenheit_to_celcius and #CREATE METHOD celcius_to_fahrenheit
We create to function to convert fahrenheit to celcius and vice-versa.

3. #REGISTER ALL METHODS
Then we should register all methods

4. #INITIALIZE
The last thing, we should initialize this modules.

The codes :
More…

,

method

Simple way to understand how Inheritance works in Python

8 Mar , 2012  

Inheritance is a way to reuse code of existing object. Differ with Java, Python have more flexible inheritance. We can derived from base class and overide as many as we want to. So what most simple implementation of inheritance in Python?

Open your Python console interpreter and start typing:
More…

,