<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andrea Grandi &#187; HowTo</title>
	<atom:link href="http://www.andreagrandi.it/category/howto/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.andreagrandi.it</link>
	<description>Pensieri, progetti e qualche informazione su di me</description>
	<lastBuildDate>Fri, 03 Feb 2012 20:03:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Nokia QtSDK installer crash on Ubuntu: how to fix it</title>
		<link>http://www.andreagrandi.it/2012/01/12/nokia-qtsdk-installer-crash-on-ubuntu-how-to-fix-it/</link>
		<comments>http://www.andreagrandi.it/2012/01/12/nokia-qtsdk-installer-crash-on-ubuntu-how-to-fix-it/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 18:36:15 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Ubuntu (EN)]]></category>
		<category><![CDATA[11.04]]></category>
		<category><![CDATA[11.10]]></category>
		<category><![CDATA[installer]]></category>
		<category><![CDATA[Natty]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[Oneiric]]></category>
		<category><![CDATA[SDK]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=602</guid>
		<description><![CDATA[If you try to install Nokia QtSDK on Ubuntu using the Nokia installer (that provides a newer version than the one distributed in Ubuntu Software Center) you could get an error like this: to fix it, you need to run the installer with a specific parameter: and everything should work!]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2012%2F01%2F12%2Fnokia-qtsdk-installer-crash-on-ubuntu-how-to-fix-it%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2012%2F01%2F12%2Fnokia-qtsdk-installer-crash-on-ubuntu-how-to-fix-it%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>If you try to install <strong>Nokia QtSDK</strong> on Ubuntu using the Nokia installer (that provides a newer version than the one distributed in Ubuntu Software Center) you could get an error like this:</p>
<pre class="brush: plain; title: ; notranslate">
(Qt_SDK_Lin32_offline_v1_1_3_en.run:3126): Gtk-CRITICAL **: IA__gtk_widget_style_get: assertion `GTK_IS_WIDGET (widget)' failed
</pre>
<p>to fix it, you need to run the installer with a specific parameter:</p>
<pre class="brush: plain; title: ; notranslate">
andrea@centurion:~/Downloads/Qt$ ./Qt_SDK_Lin32_offline_v1_1_4_en.run -style cleanlooks
</pre>
<p>and everything should work!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2012/01/12/nokia-qtsdk-installer-crash-on-ubuntu-how-to-fix-it/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Backing up all your Flickr photos using Linux</title>
		<link>http://www.andreagrandi.it/2012/01/05/backing-up-all-your-flickr-photos-using-linux/</link>
		<comments>http://www.andreagrandi.it/2012/01/05/backing-up-all-your-flickr-photos-using-linux/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 10:31:07 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ubuntu (EN)]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=599</guid>
		<description><![CDATA[I've been a Flickr pro user for 4 years, but the pro account costs 24,95$/year and I was looking for something cheaper. Anyway I was thinking that after all, even if I don't renew my account, I can always access to all my pictures.... wrong! If you don't renew your pro Flickr account you can [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2012%2F01%2F05%2Fbacking-up-all-your-flickr-photos-using-linux%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2012%2F01%2F05%2Fbacking-up-all-your-flickr-photos-using-linux%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I've been a Flickr pro user for 4 years, but the pro account costs <strong>24,95$/year</strong> and I was looking for something cheaper. Anyway I was thinking that after all, even if I don't renew my account, I can always access to all my pictures.... wrong! <strong>If you don't renew your pro Flickr account you can only access to the low resolution version of your own pictures.</strong> That's not acceptable for me, so I decided to download all my pictures and upload them somewhere else. Here comes the second disappointment: <strong>there is no automatic way to download all your pictures</strong>.</p>
<p>I simply had no time to write an application by myself, so I started searching on Google to see if there was something available to do this simple task. At the beginning I only found abandoned tools (closed source, the API was expired ecc...), paid tools, Windows only tools ecc... but finally I found this post <a href="http://hivelogic.com/articles/backing-up-flickr/">http://hivelogic.com/articles/backing-up-flickr/</a></p>
<p>There is a Python script that automatically downloads all your Flickr pictures getting the highest resolution available, you can download the script from here <a href="https://github.com/dan/hivelogic-flickrtouchr">https://github.com/dan/hivelogic-flickrtouchr</a></p>
<p>The usage is very simple</p>
<pre class="brush: plain; title: ; notranslate">
mkdir FlickrBackupFolder
python flickrtouchr.py FlickrBackupFolder
</pre>
<p>A browser's window will be opened and you'll be prompted for authorization. After that, all you pictures will be downloaded.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2012/01/05/backing-up-all-your-flickr-photos-using-linux/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Utilizzare la Carta Sanitaria Europea su Ubuntu Linux</title>
		<link>http://www.andreagrandi.it/2010/11/11/utilizzare-la-carta-sanitaria-europea-su-ubuntu-linux/</link>
		<comments>http://www.andreagrandi.it/2010/11/11/utilizzare-la-carta-sanitaria-europea-su-ubuntu-linux/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 13:32:06 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu (IT)]]></category>
		<category><![CDATA[CSE]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Regione Toscana]]></category>
		<category><![CDATA[SmartCard]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=425</guid>
		<description><![CDATA[In questo periodo le regioni stanno inviando a casa di ogni cittadino la nuova versione della Carta Sanitaria Europea, simile a quella che potete vedere nella foto. Questa nuova carta, oltre a conservare gli stessi utilizzi di quella vecchia, comprende anche un micro chip che permette di utilizzarla con i comuni lettori di smartcard. Ma [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2010%2F11%2F11%2Futilizzare-la-carta-sanitaria-europea-su-ubuntu-linux%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2010%2F11%2F11%2Futilizzare-la-carta-sanitaria-europea-su-ubuntu-linux%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img class="alignright size-full wp-image-426" title="CSE_Toscana" src="http://www.andreagrandi.it/wp-content/uploads/2010/11/jpg_2056020.jpg" alt="" width="149" height="118" />In questo periodo le regioni stanno inviando a casa di ogni cittadino la nuova versione della <strong>Carta Sanitaria Europea</strong>, simile a quella che potete vedere nella foto. Questa nuova carta, oltre a conservare gli stessi utilizzi di quella vecchia, comprende anche un micro chip che permette di utilizzarla con i comuni lettori di smartcard.</p>
<p>Ma a cosa serve poter utilizzare la CSE con un lettore di smartcard? Ad esempio ad accedere al proprio <strong>fascicolo sanitario</strong> tramite il sito della regione, che ci permetterà di vedere alcuni nostri dati come ad esempio: medicine prese, esenzioni, ricoveri in ospedale, ricoveri al pronto soccorso, risultati delle analisi ecc...</p>
<p>Per accedere all'area riservata non viene utilizzato il classico metodo di username/password, ma bensì l'autenticazione tramite smartcard.</p>
<h2>Installazione del lettore di smartcard</h2>
<p><img class="alignright size-full wp-image-435" title="miniLectorBox" src="http://www.andreagrandi.it/wp-content/uploads/2010/11/miniLectorBox.jpg" alt="" width="132" height="133" /></p>
<p>Prima di utilizzare la carta su <strong>Ubuntu Linux</strong> è necessario intanto procurarsi un lettore di smartcard (<strong>vi consiglio di acquistare il kit</strong> che vendono dove siete andati ad attivare la vostra CSE, poichè viene venduto ad un prezzo vantaggioso di <strong>4,20€</strong> mentre se provate a comprare il lettore altrove non lo troverete a meno di 15-20€) e poi installare sul proprio sistema alcuni pacchetti necessari al suo funzionamento.</p>
<p>Il lettore, una volta inserito nel proprio PC dovrebbe essere identificabile tramite la seguente stringa:</p>
<pre class="brush: plain; title: ; notranslate">
andrea@centurion:~$ lsusb
Bus 002 Device 004: ID 072f:90cc Advanced Card Systems, Ltd ACR38 SmartCard Reader
</pre>
<p>Per installare il software necessario, occorre eseguire il seguente comando da terminale:</p>
<pre class="brush: plain; title: ; notranslate">sudo apt-get install pcsc-tools pcscd libccid</pre>
<p>dopo di che dovrete procurarvi il driver del lettore, che potete trovare a <a href="http://www.regione.toscana.it/Carta_sanitaria/software/linux/Installazione_lettore_LINUX/lettore/soft_lettore.zip">questo indirizzo</a> ed installarlo con il seguente comando (dopo aver scompattato l'archivio in una cartella a piacimento):</p>
<pre class="brush: plain; title: ; notranslate">sudo dpkg -i libminilector38u-bit4id.deb</pre>
<p>Se tutto è stato eseguito correttamente, utilizzando il programma <strong>pcsc_scan</strong> da terminale, dovreste ottenere un output simile a questo:</p>
<pre class="brush: plain; title: ; notranslate">
andrea@centurion:~$ pcsc_scan
PC/SC device scanner
V 1.4.16 (c) 2001-2009, Ludovic Rousseau &lt;ludovic.rousseau@free.fr&gt;
Compiled with PC/SC lite version: 1.5.3
Scanning present readers...
0: ACS ACR 38U-CCID 00 00

Thu Nov 11 14:08:37 2010
 Reader 0: ACS ACR 38U-CCID 00 00
  Card state: Card inserted,
  ATR: 3B DF 18 00 81 31 FE 7D 00 6B 15 0C 01 81 01 11 01 43 4E 53 10 31 80 E8
</pre>
<h2>Configurazione di Firefox</h2>
<p>Prima di poter configurare Firefox è necessario installare un'ultima libreria che permetterà al browser di poter interagire con il lettore di smartcard. Dobbiamo scaricare l'archivio presente a <a href="http://www.regione.toscana.it/Carta_sanitaria/software/linux/Installazione_CSE_LINUX/Software/smart_card.zip">questo indirizzo</a>, scompattarlo e poi copiare uno dei due file presenti (a seconda che si utilizzi un sistema a 32 o 64 bit) all'interno della cartella <strong>/usr/lib</strong> ed infine digitare il comando <strong>sudo ldconfig</strong> per aggiornare l'elenco delle librerie.</p>
<p>A questo punto bisogna aprire <strong>Firefox</strong> ed andare su <strong>Modifica--&gt;Preferenze--&gt;Avanzate--&gt;Cifratura--&gt;Dispositivi di Sicurezza</strong> cliccare su <strong>Carica</strong> e specificare come descrizione <strong>"CSE"</strong> e come percorso quello dove avete copiato la libreria installata nel passo precedente (che ad esempio potrà trovarsi in <strong>/usr/lib/libaseCnsP11.so</strong> ).</p>
<p>Per verificare che tutto funzioni correttamente è sufficiente fare click su <strong>Accedi</strong> e se ci verrà chiesto di inserire la "password" (che nel nostro caso sarà il <strong>PIN</strong> della smartcard) vorrà dire che tutto sta funzionando nel modo corretto.</p>
<p>Per avere maggiori informazioni sulla <strong>Carta Sanitaria Europea</strong> e per conoscere tutti i servizi disponibili, vi consiglio di visitare la seguente pagina presente sul sito della <strong>Regione Toscana</strong>: <a href="http://www.regione.toscana.it/cartasanitaria/cse/cose/index.html">http://www.regione.toscana.it/cartasanitaria/cse/cose/index.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2010/11/11/utilizzare-la-carta-sanitaria-europea-su-ubuntu-linux/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Writing Python bindings of existing C libraries – (3) – Building and Installing with distutils</title>
		<link>http://www.andreagrandi.it/2009/08/13/writing-python-bindings-of-existing-c-libraries-%e2%80%93-3-%e2%80%93-building-and-installing-with-distutils/</link>
		<comments>http://www.andreagrandi.it/2009/08/13/writing-python-bindings-of-existing-c-libraries-%e2%80%93-3-%e2%80%93-building-and-installing-with-distutils/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 09:39:50 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Igalia]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Maemo (EN)]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[binding]]></category>
		<category><![CDATA[distutils]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[maemo]]></category>
		<category><![CDATA[setup]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=315</guid>
		<description><![CDATA[In the last post of this series, we saw how to write a simple binding and we finished to build and install it manually. This is of course not a good way to manage the building/installation procedure. In Python we can use a library called distutils that let us to automatize the building and installing [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F08%2F13%2Fwriting-python-bindings-of-existing-c-libraries-%25e2%2580%2593-3-%25e2%2580%2593-building-and-installing-with-distutils%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F08%2F13%2Fwriting-python-bindings-of-existing-c-libraries-%25e2%2580%2593-3-%25e2%2580%2593-building-and-installing-with-distutils%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In the last post of this series, we saw how to write a simple binding and we finished to build and install it manually. This is of course not a good way to manage the building/installation procedure.</p>
<p>In Python we can use a library called <strong>distutils</strong> that let us to automatize the building and installing process. I'll use the <strong>foo</strong> source code to create the package, so it will be easier to understand.</p>
<h2>Using distutils</h2>
<p>All we have to do is to write a <strong>setup.py</strong> file similar to this one:</p>
<pre class="brush: python; title: ; notranslate">
from distutils.core import setup, Extension

foomodule = Extension('foo', sources = ['foo.c'])

setup (name = 'Foo',
       version = '1.0',
       description = 'This is a package for Foo',
       ext_modules = [foomodule])
</pre>
<p>As you can see, we have to first import needed modules with: <strong>from distutils.core import setup, Extension</strong><br />
then we create an entry for each module we have (in this case just one, "foomodule"). We then call the <strong>setup()</strong> method passing it all the parameters and our <strong>setup.py</strong> is complete.</p>
<h2>Building and installing</h2>
<p>To test it we can try to build the package in this way:</p>
<p><code>python2.5 setup.py build</code></p>
<p>if we want to install the module in our system:</p>
<p><code>python2.5 setup.py install</code></p>
<h2>References</h2>
<ul>
<li>Official Python documentation: <a href="http://docs.python.org/extending/building.html">http://docs.python.org/extending/building.html</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2009/08/13/writing-python-bindings-of-existing-c-libraries-%e2%80%93-3-%e2%80%93-building-and-installing-with-distutils/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making Maemo email client usable with GMail</title>
		<link>http://www.andreagrandi.it/2009/08/08/making-maemo-email-client-usable-with-gmail/</link>
		<comments>http://www.andreagrandi.it/2009/08/08/making-maemo-email-client-usable-with-gmail/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 12:34:44 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Igalia]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Maemo (EN)]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[maemo]]></category>
		<category><![CDATA[modest]]></category>
		<category><![CDATA[recent]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=309</guid>
		<description><![CDATA[I must admit, I wasn't using Maemo email client, because I did find it was simply unusable, at least with my GMail account. I tried both POP3 and IMAP, but having about 25.000+ messages in my account, downloading just the headers was a job that the client simply couldn't manage. Yesterday I knew about "recent [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F08%2F08%2Fmaking-maemo-email-client-usable-with-gmail%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F08%2F08%2Fmaking-maemo-email-client-usable-with-gmail%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I must admit, I wasn't using Maemo email client, because I did find it was simply unusable, at least with my GMail account.</p>
<p>I tried both POP3 and IMAP, but having about 25.000+ messages in my account, downloading just the headers was a job that the client simply couldn't manage.</p>
<p>Yesterday I knew about "<strong>recent mode</strong>" support in <strong>POP3</strong>, a functionality that <strong>GMail</strong> supports too. This mode allow you to download<strong> only last 30 days</strong> messages (in my case, no more than 1000)  so the client can manage them without any problem.</p>
<p>All you have to do to enable this mode is put the "<strong>recent:</strong>" string before the username. For example: if your username is "username@gmail.com" you have to write "<strong>recent:username@gmail.com</strong>". Important: this mode only works with POP3, not with IMAP.</p>
<p>To conclude, let me say <strong>thank you</strong> to the kind guy who let me discover this mode. Thank you <a href="http://blogs.igalia.com/svillar/"><strong>Sergio</strong></a>! Now there is another thing I can do with my tablet!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2009/08/08/making-maemo-email-client-usable-with-gmail/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Writing Python bindings of existing C libraries – (2) – A simple example of binding</title>
		<link>http://www.andreagrandi.it/2009/08/06/writing-python-bindings-of-existing-c-libraries-%e2%80%93-2-%e2%80%93-a-simple-example-of-binding/</link>
		<comments>http://www.andreagrandi.it/2009/08/06/writing-python-bindings-of-existing-c-libraries-%e2%80%93-2-%e2%80%93-a-simple-example-of-binding/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 14:44:21 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Igalia]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Maemo (EN)]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[binding]]></category>
		<category><![CDATA[maemo]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=301</guid>
		<description><![CDATA[Introduction As I promised in the preceding post, I'll provide a very easy example of a python binding. Let's suppose we don't want to use the methods included in Python to sum two integer values and we want to do it in C and then call the add method from a python script. I'll write [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F08%2F06%2Fwriting-python-bindings-of-existing-c-libraries-%25e2%2580%2593-2-%25e2%2580%2593-a-simple-example-of-binding%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F08%2F06%2Fwriting-python-bindings-of-existing-c-libraries-%25e2%2580%2593-2-%25e2%2580%2593-a-simple-example-of-binding%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h2>Introduction</h2>
<p>As I promised in the preceding post, I'll provide a very easy example of a python binding. Let's suppose we don't want to use the methods included in Python to sum two integer values and we want to do it in C and then call the add method from a python script. I'll write the complete source code first and then I'll explain all the parts of it.</p>
<h2>Source Code</h2>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;Python.h&gt;

static PyObject *foo_add(PyObject *self, PyObject *args)
{
	int a;
	int b;

	if (!PyArg_ParseTuple(args, &quot;ii&quot;, &amp;a, &amp;b))
	{
		return NULL;
	}

	return Py_BuildValue(&quot;i&quot;, a + b);
}

static PyMethodDef foo_methods[] = {
	    { &quot;add&quot;, (PyCFunction)foo_add, METH_VARARGS, NULL },
	    { NULL, NULL, 0, NULL }
};

PyMODINIT_FUNC initfoo()
{
	    Py_InitModule3(&quot;foo&quot;, foo_methods, &quot;My first extension module.&quot;);
}
</pre>
<h2>How it works</h2>
<p>First of all we have to include <strong>Python.h</strong> in our C file. This allows us to write an extension for Python language. To be able to include this header, we must have the python development packages installed in our system. For example in Debian based distributions we can install them with this command:</p>
<p><code>sudo apt-get install python2.5-dev</code></p>
<p>Every module has at least three parts. In the first part we write methods we want to call from the final python module: in this case we have a method called <strong>foo_add</strong> where "<em>foo</em>" is the name of the module and "<em>add</em>" the name of the method. Every method is declared as <strong>static PyObject</strong>. The method does anything particular except calling PyArg_ParseTuple to validate the input (we'll discuss this later), adding the two passed numbers and returning the result.</p>
<p>In the second part we have something like a dictionary, defined as static <strong>PyMethodDef</strong> and called foo_methods (where "foo" again is the name of the module). For each method we want to expose in our python module, we have to add something like this:</p>
<p><code>{"add", (PyCFunction)foo_add, METH_VARARGS, NULL}</code></p>
<p>where "<em>add</em>" is the name of the method we want to be visible in our module, <em>(PyCFunction)foo_add</em> is a pointer to our foo_add method, implemented in the C module, METH_VARARGS means that we want to pass some parameters to the function and the last one would be the description of the method (we can leave it NULL if we want).</p>
<p>Third part allows us to register the defined method/s and the module:</p>
<p><code>Py_InitModule3("foo", foo_methods, "My first extension module.");</code></p>
<h3>Parsing Parameters</h3>
<p>The <strong>PyArg_ParseTuple</strong> function extracts arguments from the <strong>PyObject</strong> passed as parameter to the current method and follows almost the sscanf syntax to parse parameters (in this case we had <em>"ii"</em> for two integers). You can fin the complete reference here: <a href="http://docs.python.org/c-api/arg.html">http://docs.python.org/c-api/arg.html</a></p>
<h2>Building and installing</h2>
<p>To build the module, we have to be in the source directory and execute this command:</p>
<p><code>gcc -shared -I/usr/include/python2.5 foo.c -o foo.so</code></p>
<p>then we've to copy the generated module to the python's modules directory:</p>
<p><code>cp foo.so /usr/lib/python2.5/site-packages/</code></p>
<h2>Testing our module</h2>
<p>Testing the module is really easy. We've to start a python shell or create a python script with the following source code:</p>
<pre class="brush: python; title: ; notranslate">
import foo
print foo.add(2, 3)
</pre>
<p>if all is working fine, the printed result should be <strong>5</strong></p>
<h2>References</h2>
<ul>
<li><a href="http://docs.python.org/extending/extending.html">http://docs.python.org/extending/extending.html</a></li>
<li><a href="http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764596543.html">http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764596543.html</a></li>
</ul>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 399px; width: 1px; height: 1px;">python2.5-dev</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 399px; width: 1px; height: 1px;">python2.5-dev</div>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2009/08/06/writing-python-bindings-of-existing-c-libraries-%e2%80%93-2-%e2%80%93-a-simple-example-of-binding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Writing Python bindings of existing C libraries &#8211; (1) &#8211; Introduction</title>
		<link>http://www.andreagrandi.it/2009/08/03/writing-python-bindings-of-existing-c-libraries-1-introduction/</link>
		<comments>http://www.andreagrandi.it/2009/08/03/writing-python-bindings-of-existing-c-libraries-1-introduction/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 09:04:35 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Igalia]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Maemo (EN)]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[bindings]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[maemo]]></category>
		<category><![CDATA[pymaemo]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=298</guid>
		<description><![CDATA[This summer I'm having the pleasure of working in Igalia (a spanish free software company) for a couple of months and they assigned me to an interesting project: developing Python bindings for MAFW library (a Maemo multimedia library that will be used in Fremantle release). Having the opportunity to work both with C (yes, Python [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F08%2F03%2Fwriting-python-bindings-of-existing-c-libraries-1-introduction%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F08%2F03%2Fwriting-python-bindings-of-existing-c-libraries-1-introduction%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This summer I'm having the pleasure of working in <a href="http://www.igalia.com"><strong>Igalia</strong></a> (a spanish free software company) for a couple of months and they assigned me to an interesting project: developing <strong> Python bindings</strong> for <strong>MAFW</strong> library (a <strong>Maemo</strong> multimedia library that will be used in <strong>Fremantle</strong> release).</p>
<p>Having the opportunity to work both with <strong>C</strong> (yes, Python bindings are almost C code) and <strong>Python</strong> (it's a good practice to write unittest of all implemented methods) it's a good way to improve my knowledges in both languages and since I wasn't able to find much documentation about these kind of things, I'm going to share my own experiences.</p>
<p><strong>What is a Binding?</strong></p>
<p>A binding is a Python module, written in C language, that allows Python developers to call functions from existing C libraries from their python applications. It's just like a "<em>bridge</em>" from C world to Python one.</p>
<p><strong>Why writing bindings?</strong></p>
<p>There are a couple of reasons to write python bindings instead of writing a library in python language from scratch.</p>
<p>First of all I don't think is good duplicating code, so if a library already exists and it's written in C, why writing it again in another language? There's no reason. A lot of code already exist in C world and all we have to do is to create a bridge with python world.</p>
<p>Another good reason, in particular when a C library doesn't exist yet, is the fact that python code is slower than C code for some tasks (for example multimedia codecs). In these cases is good to implement the core library in C language and then create a python binding for it.</p>
<p><strong>Coming next</strong></p>
<p>As the title of this post says, this is only an introduction to the subjects I'm going to write about. If you have any particular request about any argument you would like to read, please feel free to leave me a comment. Next posts will talk about these things:</p>
<ul>
<li><strong>A simple example of binding:</strong> I'll write a simple library in C language and I'll show how to create the relative python binding, providing complete source code and an example for python developers.</li>
<li><strong>Building and installing python bindings with distutils:</strong> I'll explain how to use <strong>distutils</strong> to build and install the binding (using the well know method "python setup.py install").</li>
<li><strong>Defining new types:</strong> this post will be about how to write new types in C language and being able to use them from python code.</li>
<li><strong>Using codegen to write bindings:</strong> I'll explain how to use <strong>codegen</strong> utils to automate lot of tasks, to generate the most part of binding code and how to customize the generated code using overrides.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2009/08/03/writing-python-bindings-of-existing-c-libraries-1-introduction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ridimensionare immagini automaticamente con un batch e ImageMagick</title>
		<link>http://www.andreagrandi.it/2009/07/05/ridimensionare-immagini-automaticamente-con-un-batch-e-imagemagick/</link>
		<comments>http://www.andreagrandi.it/2009/07/05/ridimensionare-immagini-automaticamente-con-un-batch-e-imagemagick/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 16:56:16 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[batch]]></category>
		<category><![CDATA[foto]]></category>
		<category><![CDATA[fotoritocco]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[imagemagick]]></category>
		<category><![CDATA[immagini]]></category>
		<category><![CDATA[magick]]></category>
		<category><![CDATA[ridimensionare]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=285</guid>
		<description><![CDATA[Spesso capita di dover compiere azioni noiose e ripetitive su delle immagini, come ad esempio ridimensionarle o salvarle in formati diversi da quello originale. Queste operazioni possono richiedere moltissimo tempo, soprattutto se abbiamo a che fare con una grande quantità di immagini. Per chi usa Linux è disponibile però l'utility ImageMagick, che unita ad un [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F07%2F05%2Fridimensionare-immagini-automaticamente-con-un-batch-e-imagemagick%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F07%2F05%2Fridimensionare-immagini-automaticamente-con-un-batch-e-imagemagick%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Spesso capita di dover compiere azioni noiose e ripetitive su delle immagini, come ad esempio ridimensionarle o salvarle in formati diversi da quello originale. Queste operazioni possono richiedere moltissimo tempo, soprattutto se abbiamo a che fare con una grande quantità di immagini.</p>
<p>Per chi usa <strong>Linux</strong> è disponibile però l'utility <strong>ImageMagick</strong>, che unita ad un pizzico di script in <strong>bash</strong> ci permette di risolvere agevolmente il problema.</p>
<p>Per prima cosa è necessario installare il tool sulla propria distribuzione. Su <strong>Ubuntu</strong> (o in qualsiasi altra distribuzione basata su Debian) procedere nella seguente maniera:</p>
<p><code>sudo apt-get install imagemagick</code></p>
<p>A questo punto basta posizionarsi nella cartella dove si trovano le immagini (vi consiglio di crearvi una copia a parte delle immagini da modificare, visto che lo script andra' a lavorare direttamente su quelle originali) ed eseguire un comando come questo:</p>
<p><code>find ./ -iname '*.JPG' -exec convert '{}' -resize '1024' '{}' \;</code></p>
<p>Questo comando convertirà tutte le immagini .JPG che trova in un formato di 1024 pixel di larghezza, mantenendo ovviamente le proporzioni dell'immagine originale.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2009/07/05/ridimensionare-immagini-automaticamente-con-un-batch-e-imagemagick/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Risolvere il crash all&#8217;avvio di Songbird su Ubuntu</title>
		<link>http://www.andreagrandi.it/2009/06/24/risolvere-il-crash-allavvio-di-songbird-su-ubuntu/</link>
		<comments>http://www.andreagrandi.it/2009/06/24/risolvere-il-crash-allavvio-di-songbird-su-ubuntu/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 15:00:36 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu (IT)]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[libvisual]]></category>
		<category><![CDATA[nvidia]]></category>
		<category><![CDATA[songbird]]></category>
		<category><![CDATA[Ubuntu (EN)]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=282</guid>
		<description><![CDATA[Se si prova a far girare la versione di Songbird scaricabile dal sito ufficiale su Ubuntu (ma il problema sembra riguardare anche altre distribuzioni visto che si tratta di un bug del driver Nvidia) si otterrà quasi sicuramente un crash dell'applicazione stessa. Per risolvere il problema è sufficiente disinstallare un plugin (libvisual-0.4-plugins) utilizzando il seguente [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F06%2F24%2Frisolvere-il-crash-allavvio-di-songbird-su-ubuntu%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F06%2F24%2Frisolvere-il-crash-allavvio-di-songbird-su-ubuntu%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong><strong><img class="alignright size-full wp-image-283" title="songbird_screenshot_0" src="http://www.andreagrandi.it/wp-content/uploads/2009/06/songbird_screenshot_0.jpg" alt="songbird_screenshot_0" width="197" height="136" /></strong></strong>Se si prova a far girare la versione di <strong>Songbird</strong> scaricabile <a href="http://getsongbird.com">dal sito ufficiale</a> su <strong>Ubuntu</strong> (ma il problema sembra riguardare anche altre distribuzioni visto che si tratta di un bug del driver Nvidia) si otterrà quasi sicuramente un crash dell'applicazione stessa.</p>
<p>Per risolvere il problema è sufficiente disinstallare un plugin (<strong>libvisual-0.4-plugins</strong>) utilizzando il seguente comando: <strong>sudo apt-get remove <strong>libvisual-0.4-plugins</strong></strong></p>
<p>Una nota negativa: questo bug è presente fin dalla versione 8.10 di Ubuntu... che aspettano a correggerlo invece di far ricorrere gli utenti a questi trucchetti?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2009/06/24/risolvere-il-crash-allavvio-di-songbird-su-ubuntu/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Come passare dalla modalità enduser alla modalità developer su Fonera 2</title>
		<link>http://www.andreagrandi.it/2009/05/05/come-passare-dalla-modalita-enduser-alla-modalita-developer-su-fonera-2/</link>
		<comments>http://www.andreagrandi.it/2009/05/05/come-passare-dalla-modalita-enduser-alla-modalita-developer-su-fonera-2/#comments</comments>
		<pubDate>Tue, 05 May 2009 12:10:34 +0000</pubDate>
		<dc:creator>Andrea Grandi</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Recensione]]></category>
		<category><![CDATA[fon]]></category>
		<category><![CDATA[fonera]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.andreagrandi.it/?p=271</guid>
		<description><![CDATA[Fino ad ora, i possessori di Fonera 2.0 che volevano passare alla modalità "developer" (e quindi avere anche l'accesso via SSH) dovevano flashare la fonera con un'apposita immagine, facendo uso del cavo seriale oppure della procedura con il cavo di rete che utilizza Redboot. Da oggi è disponibile un nuovo plugin che permette di passare [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F05%2F05%2Fcome-passare-dalla-modalita-enduser-alla-modalita-developer-su-fonera-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.andreagrandi.it%2F2009%2F05%2F05%2Fcome-passare-dalla-modalita-enduser-alla-modalita-developer-su-fonera-2%2F&amp;source=andreagrandi&amp;style=normal&amp;service=is.gd&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Fino ad ora, i possessori di <a href="http://www.fon.com"><strong>Fonera 2.0</strong></a> che volevano passare alla modalità "<strong>developer</strong>" (e quindi avere anche l'accesso via <strong>SSH</strong>) dovevano flashare la fonera con un'apposita immagine, facendo uso del cavo seriale oppure della procedura con il cavo di rete che utilizza Redboot.</p>
<p>Da oggi è disponibile un nuovo plugin che permette di passare automaticamente alla modalità developer, basta andare (ad esempio) su questa pagina del router: <strong>http://192.168.0.105/luci/fon_devices/fon_plugins</strong></p>
<p>La Fonera passerà a questo punto in modalità developer e dovremo effettuare due reboot affinchè SSH sia attivato. Non dimenticatevi ovviamente di aprire l'accesso SSH nella configurazione del firewall della Fonera. Accedendo via SSH, vi troverete davanti questi messaggi:</p>
<pre class="brush: plain; title: ; notranslate">andy80@andy80-jaunty:~$ ssh root@192.168.0.105
The authenticity of host '192.168.0.105 (192.168.0.105)' can't be established.
RSA key fingerprint is e5:6e:fc:70:73:44:f6:cd:30:bd:ac:2d:53:d2:ab:a9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.105' (RSA) to the list of known hosts.
root@192.168.0.105's password: 

BusyBox v1.11.1 (2009-04-17 12:45:57 CEST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

                                        __
                                    _.-~  )
                         _..--~~~~,'   ,-/     _
                      .-'. . . .'   ,-','    ,' )
                    ,'. . . _   ,--~,-'__..-'  ,'
                  ,'. . .  (@)' ---~~~~      ,'
                 /. . . . '~~             ,-'
                /. . . . .             ,-'
               ; . . . .  - .        ,'
              : . . . .       _     /
             . . . . .          `-.:
            . . . ./  - .          )
           .  . . |  _____..---.._/ ____ Seal _
     ~---~~~~----~~~~             ~~                

                      Flipper                       

--------  Fonera 2.0 Firmware (v2.2.5.0) -----------
      * Based on OpenWrt - http://openwrt.org
      * Powered by FON - http://www.fon.com
----------------------------------------------------
root@Fonera:~# uname -a
Linux Fonera 2.6.26.2 #9 Tue Apr 21 11:32:31 CEST 2009 mips unknown
root@Fonera:~#</pre>
<p>Cercherò prossimamente, nel caso ci sia interesse, di scrivere qualche post piu' approfondito su questa nuova Fonera, in modo da mostrare le caratteristiche di questo dispositivo.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andreagrandi.it/2009/05/05/come-passare-dalla-modalita-enduser-alla-modalita-developer-su-fonera-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

