Extending C++ with Boost Python

Posted on February 26th, 2010 in C++, Linux, Programacion, Python by Miggs

Lately I have been messing around with the boost python library to extend c++ (Being able to instantiate C++ over python). Since I wanted to create my own Makefiles I wanted to avoid using bjam. To build shared objects you can do it as follows.

Boost Python Hello World

g++ -shared -fPIC -o hello_ext.so hello.cpp -I/usr/local/include/boost/python -I/usr/local/include/python2.6 -lboost_python

hello.cpp

// Copyright Joel de Guzman 2002-2004. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
// Hello World Example from the tutorial
// [Joel de Guzman 10/9/2002]

#include
#include

char const* greet()
{
return “hello, world”;
}

BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def(“greet”, greet);
}

hello.py

# Copyright Joel de Guzman 2002-2007. Distributed under the Boost
# Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
# or copy at http://www.boost.org/LICENSE_1_0.txt)
# Hello World Example from the tutorial

import hello_ext
print hello_ext.greet()


Ruby & Ruby on Rails

Posted on June 3rd, 2009 in Programacion, tutoriales by Miggs

Here are a some excelent video casts from U.C. Berkeley on Ruby and Ruby on Rails that should help you get started with such cool technology!

Ruby on Rails – Part 1: Hello World

Ruby on Rails – Part 2: Just Enough Ruby

Ruby on Rails – Part 3: Basic Rails

Ruby on Rails – Part 4: Advanced Active Record

Ruby on Rails – Part 5: AJAX and Testing

Ruby on Rails – Part 6: Configuration and Deploy


awk: elemento separador

Posted on July 1st, 2008 in Linux, Programacion, tutoriales by Miggs

Nota rápida pero quite handy. Como cambiar el símbolo separador de argumentos en awk (por defecto es el espacio)

# awk -F. ‘{…}’

Saludos


Forma Rapida Segura y Sencilla de ELIMINAR archivos

Posted on July 1st, 2008 in Linux, Programacion, Seguridad, tutoriales by Miggs

El otro dia mi jefe me pidio que le llamase antes de borrar un archivo para asegurarnos de que realmente los datos eran eliminados. El proceso es simple, sobreescribir el contenido del archivo con datos. Idilicamente se debería de hacer con datos aleatorios, pero tampoco es cuestion de ser paranoicos. Basta con repetir el proceso unas cuantas veces antes de borrarlo. En cualquier si alguien tiene a mano como hacerlo correctamente que escriba un comentario y lo anado.

$ sed -i -e ’s/./X/g’ ./path/to/fichero

Saludos


Generador de palabras. Wordgenerator.c

Posted on May 23rd, 2008 in FreeBSD, Linux, Programacion, Seguridad, tutoriales by Miggs

beastyHace tiempo, trasteando con las redes wifi y la clásica encriptacion WEP llegue a un punto en el que necesitaba una diccionario de claves. Como las claves eran ’semi-aleatorias’ no me servia cualquier diccionario, necesitaba algo que me generase todas las posibles claves de determinada longitud con un determinado juego de caracteres. Así que decidí poner en buen uso mis conocimientos de C y crearme una herramienta que me lo hiciera. El problema era conceptual mente simple así que me puse manos a la obra.

Inicialmente resolví el problema con recursividad (lento), mejorandolo mas tarde utilizando un algoritmo lineal (mucho mucho mas rápido). Aquí os dejo el código fuente.

Compilar

Si dispones de gcc el proceso es muy sencillo.

$ gcc -o wordgenerator.c wordgenerator

Ejecucion

Desde la linea de comandos basta con

$ ./wordgenerator

Código

//Librerias necesarias
#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
//Prototipos
int getOptions(int,char**,char**,int*,int*);
void ayuda();
void generator(char *, char *,int, int);
//
//Funcion: wordgenerator.c
//Description: Genera palabras linear o recursivamente.
//Autor: Miggs
//Date: 18/10/2006
//
int main (int argc, char *argv[])
{
//Caracteres a usar por defecto
char *c = “1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM”;
int len;
//Usamos recursividad?
int r = 0; //No a no ser que nos lo pidan…

if(getOptions(argc,argv,&c,&len,&r)<0)
ayuda();

char actual[len + 1];
actual[len] = ‘\0′;

if(r == 0){
//Lineal = rapido
int i,j;
for(i = 0; i < len ; i ++)
for(j = 0; j< strlen(c) ; j ++){
actual[i] = c[j];
printf(“%s\n”,actual);
}
}else
//Recursividad = lento
generator(c,actual,len,0);
return 0;
}
//
//Funcion: generator
//Description: funcion recursiva
//Date: 18/10/2006
//
void generator(char *c,char *actual, int len, int level)
{
int i;
if(level < len)
{
for(i = 0; i < strlen(c);i = i + 1){
actual[level] = c[i];
generator(c,actual,len,level+1);
}
}
else
printf(“%s\n”,actual);
}

//
//Funcion: getOptions
//Description: Interpreta las opciones de la linea de comandos
//Date: 18/10/2006
//
int getOptions(int argc, char **argv, char **vC, int *l, int *r)
{
int c;
int retValue = -1;

while((c = getopt(argc, argv, “l:c:r”)) != -1){
switch(c){
case ‘l’:
*l = atoi(optarg);
if(*l > 0)
retValue = 1;
break;
case ‘c’:
*vC =optarg;
break;
case ‘r’:
*r = 1;
default:
break;
}
}
return retValue;
}

//
//Funcion: ayuda
//Description: Imprime la ayuda por pantalla
//Date: 18/10/2006
//
void ayuda()
{
printf(“\n// wordGenerator 0.1 6/Feb/2008\n”);
printf(“// icaix.com//\n”);
printf(“\n Uso:\n”)//
printf(” ./wordGenerator [-C CaracteresAUsar] [-r] -l <longitudPalabra> \n”);
printf(“\n Generacion palabras de longitud 3, metodo lineal\n”);
printf(” ./wordGenerator -l 3\n”);
printf(“\n Generacion palabras de longitud 3, metodo recursivo\n”);
printf(” ./wordGenerator -r -l 3\n”);
printf(” Generacion palabras de longitud 3 con vocabulario abcdefghABCDEFGH\n”);
printf(” ./wordGenerator -l 3 -C abcdefghABCDEFGH\n\n”);
_exit(0);
}