LinuxCabal talk on Python and C

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:

  1. Language
  2. Platform
  3. Compilers
  4. Libraries

The stack of C will be:

  1. gcc – the compiler
  2. as – the assembler
  3. ld – linker
  4. libc – the C library

Additionally we will need a text editor, and a debugger usually known gdb.

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:

  1. Language
  2. Platform
  3. Compilers
  4. Libraries

The stack of C will be:

  1. gcc – the compiler
  2. as – the assembler
  3. ld – linker
  4. libc – the C library

Additionally we will need a text editor, and a debugger usually known gdb.

A simple sample would be a Hellow World hola-1.c:

#include <stdio.h>int main(){   printf("Hola Mundon");}

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 ("Mundon");}

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:

  1. diff
  2. patch

We can use diff by typing:

]$ diff hola-1.c hola-2.c 5c5,6<          printf("Hola Mundon");--->          printf("Hola);>             printf ("Mundon");

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 Mundon");        |            printf("Hola);                                          >               printf ("Mundon");}                                         }

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.

  1. dp – delete and put the difference
  2. ctl-w -> / L will switch the left panes.
  3. g – get the changes

Finally a python based is meld, also is pretty nice stylish way. Patch was created by Larry Wall, to use path commonly people will make a diff to a 3rd file.

Media_httpfilesmyoper_dkbbb

diff -o hola-1.c hola-2.c > hola-path.cpatch -p0 < hola-patch.c

To have a versioning systems is another tool which are used on multi-user projects we can use systems like:

  1. rcs – revision control system
  2. cvs – control/concurrent versioning system
  3. svn – subversion

Finally the debugging steps which we can use using some samples which I wont put on this post. However the steps to debugg we will asume we have some files called loop.c.

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 loop

We will have a second command line, some tools are:

  1. run – will execute the file with errors
  2. start – will
  3. n/next – next will go step by step
  4. arrow bottons – you can go to the next or previous step
  5. step – similar to next but will get within the functions
  6. backtrace – we can see what happened previously
  7. kill – salir del programa de manera emergente
  8. finish – suficientes next hasta terminar
  9. print buff – we can print the current buffer
  10. quit/q – to end the process

Here we can go back to the difference using vi -d or vimdiff we can save having 2 files using on their panes doing:

$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.

Advertisements