LinuxCabal talk on Python and C
Saturday, 28. July 2007, 22:16:41
This saturday Patricio Paez gave a talk at Linux cabal on Saturday 28. So the talk main focus is not learning C or Python but a broad view of the interaction between Python and C. The first part was thinking about the way code works. First we will need the following elements:
- Language
- Platform
- Compilers
- Libraries
- gcc - the compiler
- as - the assembler
- ld - linker
- libc - the C library
This saturday Patricio Paez gave a talk at Linux cabal on Saturday 28. So the talk main focus is not learning C or Python but a broad view of the interaction between Python and C. The first part was thinking about the way code works. First we will need the following elements:
- Language
- Platform
- Compilers
- Libraries
- gcc - the compiler
- as - the assembler
- ld - linker
- libc - the C library
A simple sample would be a Hellow World hola-1.c:
#include <stdio.h>
int main()
{
printf("Hola Mundo\n");
}
The compilation process would be:
$ gcc -o hola-1.c hola
We will add some changes with the same result:
#include <stdio.h>
int main()
{
printf("Hola);
printf ("Mundo\n");
}The result would be the same.A very importat topic is using version control. Like we went through the last change we had a version 1 of hola and a second version on hola-2.c
To use this versioning we could use some tools mainly:
- diff
- patch
]$ diff hola-1.c hola-2.c
5c5,6
< printf("Hola Mundo\n");
---
> printf("Hola);
> printf ("Mundo\n");
We look at the string 5c5,6 which basically tellus that we have something that says that a change ocurred on line 5 until 6.
The other tool we can see is sdiff what sdiff do is that it will have two areas of codes. This means that we will see the original file and the modified file on the same output side-by-side.
$ sdiff hola-1.c hola-2.c
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
printf("Hola Mundo\n"); | printf("Hola);
> printf ("Mundo\n");
} }We can see the > and the | stablishing the change from one file to the next. We will use the -o option to merge the differences. This is also an interactive mode.For the command line impared there is tkdiff which basically is a gui front end to the difference. tkdiff is available on most distros. Although I couldnt find it and instead have gtkdiff, a curses tool also exist called imediff. imediff2 is a curses based tool which uses colors to quickly see the differences as opposed of using a | and > < characters. Unfortunately I coldn't find this tool in mandriva.
Finally the most popular is vimdiff, vimdiff is based on the vim editor. Doing vimdiff tools you will see a combination of curses and the vim editor. You will have 2 panes with each file at each side of the window. You will be able to put some of the keys.
- dp - delete and put the difference
- ctl-w -> / L will switch the left panes.
- g - get the changes
diff -o hola-1.c hola-2.c > hola-path.c patch -p0 < hola-patch.cTo have a versioning systems is another tool which are used on multi-user projects we can use systems like:
- rcs - revision control system
- cvs - control/concurrent versioning system
- svn - subversion
First we will do some compiling:
# gcc -o loop loop.c # ./loop
now we will have a debugging using the following option:
# gcc -g -o loop loop.c # gdb loopWe will have a second command line, some tools are:
- run - will execute the file with errors
- start - will
- n/next - next will go step by step
- arrow bottons - you can go to the next or previous step
- step - similar to next but will get within the functions
- backtrace - we can see what happened previously
- kill - salir del programa de manera emergente
- finish - suficientes next hasta terminar
- print buff - we can print the current buffer
- quit/q - to end the process
$vi -d loop-{1,2}.c
There is a document called the linux kernel module programming guide which has a good guide about this kind of tools for those lonely nights.
Python
So now we move into Python and the way to use it:
Python is wonderful!!!
Python is a language that is commonly based on modules. Phython modules are very popular to code a module is rather simple. There is very small diffrerence between a module and a regular file.
python:
>> import re
>> help(re)
>> re.findall('a+', 'abcdefg aabccefg') We can make extensions in for python in C. This is a bit more challenging but it still possible.













