<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dacav&#039;s trick</title>
	<atom:link href="http://dacavtricks.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://dacavtricks.wordpress.com</link>
	<description>Just another WordPress.com site</description>
	<lastBuildDate>Thu, 26 Jan 2012 14:03:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='dacavtricks.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Dacav&#039;s trick</title>
		<link>http://dacavtricks.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://dacavtricks.wordpress.com/osd.xml" title="Dacav&#039;s trick" />
	<atom:link rel='hub' href='http://dacavtricks.wordpress.com/?pushpress=hub'/>
		<item>
		<title>WebArchitectures: first (and not so useful) post</title>
		<link>http://dacavtricks.wordpress.com/2012/01/25/webarchitectures-first-useful-post/</link>
		<comments>http://dacavtricks.wordpress.com/2012/01/25/webarchitectures-first-useful-post/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 18:13:43 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Web Architectures]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=242</guid>
		<description><![CDATA[Well, today the &#8220;game&#8221; is over, let&#8217;s start posting. To be honest this is the first day I come out with something useful, and this would not be so bad if only I didn&#8217;t waste about one week in pointless experiments. A lot of effort has been spent in trying to figure out how Eclipse <a href="http://dacavtricks.wordpress.com/2012/01/25/webarchitectures-first-useful-post/" class="excerpt-more-link">[&#8230;]</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=242&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Well, today the &#8220;game&#8221; is over, let&#8217;s start posting.</p>
<p>To be honest this is the first day I come out with something useful, and this would not be so bad if only I didn&#8217;t waste about <strong>one week</strong> in pointless experiments. A lot of effort has been spent in trying to figure out how <em><strong>Eclipse</strong></em> works. I&#8217;m totally not a big fan of<em> IDE</em>s: I&#8217;ve always preferred the<em> GNU/Linux Shell</em> and a good<em> text editor</em> (yes, I&#8217;m a<em> Vim</em> user), but I feel I can get used with some exercise. I just hope to speed up a little, or I will never finish!</p>
<p>Enough philosophy for today, let&#8217;s talk about <em>Hibernate</em>. There&#8217;s no much to say anyways…</p>
<p>Ingredients for the recipe:</p>
<ul>
<li>A <a href="http://geek.co.il/wp/2010/03/14/how-to-build-a-chroot-jail-environment-for-centos" target="_blank"><em>chroot</em> sandbox</a> for the <em>mysql</em> daemon (I like to separate stuff, and a <em>chroot jails</em> are just awesome)</li>
<li>All required libraries as explained <a href="http://docs.jboss.org/hibernate/core/4.0/quickstart/en-US/html/hibernate-gsg-obtain.html" target="_blank">here</a>;</li>
<li>The java connector for <em>mysql</em>, which can be found <a href="http://dev.mysql.com/downloads/connector/j/3.0.html" target="_blank">here</a>.</li>
</ul>
<p>As probably half of the pepole who tried this, I experimented some troubles in figuring out why the <em>gorram</em> &#8220;<em>hibernate.cfg.xml&#8221;</em> file cannot be found by the library. The problem was actually rooted in my lack of experience with <em>Java</em>, but I guess that the cumulated experience will prevent me from stumbling again in such trivial issues.</p>
<p>Meh. What a useless post…</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/242/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/242/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/242/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/242/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/242/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/242/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/242/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/242/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/242/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/242/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/242/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/242/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/242/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/242/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=242&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2012/01/25/webarchitectures-first-useful-post/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>
	</item>
		<item>
		<title>Who&#8217;s writing me?</title>
		<link>http://dacavtricks.wordpress.com/2012/01/19/whos-writing-me/</link>
		<comments>http://dacavtricks.wordpress.com/2012/01/19/whos-writing-me/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 10:35:40 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=214</guid>
		<description><![CDATA[Have you ever noticed how some applications seems to be aware of how you pass them data? Take the su program, for instance: $ echo 'my password' &#124; su su: must be run from a terminal It&#8217;s said that he great power of the Unix operating system comes from the fact that everything is a <a href="http://dacavtricks.wordpress.com/2012/01/19/whos-writing-me/" class="excerpt-more-link">[&#8230;]</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=214&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Have you ever noticed how some applications seems to be aware of how you pass them data? Take the <code>su</code> program, for instance:</p>
<pre><code>
$ echo 'my password' | su
su: must be run from a terminal
</code></pre>
<p> <span id="more-214"></span></p>
<p>It&#8217;s said that he great power of the <em>Unix operating system</em> comes from the fact that <em>everything is a file</em>. <em>So they say (I guess it&#8217;s pretty ok)</em>.</p>
<p>It&#8217;s true that abstraction removes the hassle of checking case-by-case what&#8217;s going on, but this doesn&#8217;t mean that if you <em>want</em> to do it you cannot!</p>
<p>The <code>fstat</code> system call can be useful in that. Let me show you this piece of code:</p>
<pre><code>
$ more test.c

#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;stdio.h&gt;

int main ()
{
    struct stat buf;

    if (fstat(0, &amp;buf) == -1) {
        perror("Eh...");
    }

    const char * cases[] = {
        "regular", "directory", "chardev",
        "blockdev", "fifo", "link", "socket",
        "?"
    };

    int t = S_ISREG(buf.st_mode)    ? 0:
            S_ISDIR(buf.st_mode)    ? 1:
            S_ISCHR(buf.st_mode)    ? 2:
            S_ISBLK(buf.st_mode)    ? 3:
            S_ISFIFO(buf.st_mode)   ? 4:
            S_ISLNK(buf.st_mode)    ? 5:
            S_ISSOCK(buf.st_mode)   ? 6:
                                      7;

    printf("stdin is %s\n", cases[t]);
}

$ make test
cc     test.c   -o test

$ echo hello | ./test
stdin is fifo

$ ./test
stdin is chardev

$ cat /etc/fstab | ./test
stdin is fifo
</code></pre>
<p>I didn&#8217;t check the source code for the <code>su</code> command, but I guess this would work <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/214/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=214&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2012/01/19/whos-writing-me/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>
	</item>
		<item>
		<title>SOPA STRIKE</title>
		<link>http://dacavtricks.wordpress.com/2012/01/18/sopa-strike/</link>
		<comments>http://dacavtricks.wordpress.com/2012/01/18/sopa-strike/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 10:57:03 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=196</guid>
		<description><![CDATA[http://americancensorship.org/modal/state-dept-petition/index.html<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=196&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1><a href="http://americancensorship.org/modal/state-dept-petition/index.html" target="_blank">http://americancensorship.org/modal/state-dept-petition/index.html</a></h1>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/196/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/196/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/196/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/196/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/196/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/196/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/196/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/196/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/196/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/196/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/196/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/196/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/196/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/196/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=196&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2012/01/18/sopa-strike/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>
	</item>
		<item>
		<title>Interesting article</title>
		<link>http://dacavtricks.wordpress.com/2011/11/24/interesting-article/</link>
		<comments>http://dacavtricks.wordpress.com/2011/11/24/interesting-article/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 10:30:31 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Links]]></category>
		<category><![CDATA[Programming languages]]></category>
		<category><![CDATA[Erlang]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=190</guid>
		<description><![CDATA[That guy seems to be pretty much aware of what&#8217;s Erlang: http://www.javalimit.com/2011/05/erlang-is-not-a-concurrent-functional-programming-language.html Worthy read.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=190&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>That guy seems to be pretty much aware of what&#8217;s <em>Erlang</em>:<br />
<a href="http://www.javalimit.com/2011/05/erlang-is-not-a-concurrent-functional-programming-language.html"> http://www.javalimit.com/2011/05/erlang-is-not-a-concurrent-functional-programming-language.html</a></p>
<p>Worthy read.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/190/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=190&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2011/11/24/interesting-article/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>
	</item>
		<item>
		<title>Erlang make &#8211; Give priority to behaviors</title>
		<link>http://dacavtricks.wordpress.com/2011/11/18/erlang-make-give-priority-to-behaviors/</link>
		<comments>http://dacavtricks.wordpress.com/2011/11/18/erlang-make-give-priority-to-behaviors/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 13:19:45 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Erlang]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=179</guid>
		<description><![CDATA[Erlang/OTP has a nice pattern which strictly resambles Java&#8217;s Interfaces: behaviors. I&#8217;m using a behavior (&#8220;conf&#8220;) inside my application for {enter technical details here}, so I stumbled into a little itchy issue during compile time: The solution comes strightforward: compile src/conf.erl before anything else. How to do that? Well, I surfed the Internet, searching for <a href="http://dacavtricks.wordpress.com/2011/11/18/erlang-make-give-priority-to-behaviors/" class="excerpt-more-link">[&#8230;]</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=179&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Erlang/OTP has a nice pattern which strictly resambles <em>Java&#8217;s Interfaces</em>: <em><a href="http://www.erlang.org/doc/design_principles/des_princ.html" target="_blank">behaviors</a>. I&#8217;m using a behavior (&#8220;<code>conf</code>&#8220;) inside my application</em> for {enter technical details here}, so I stumbled into a little itchy issue during compile time:</p>
<div id="attachment_181" class="wp-caption aligncenter" style="width: 510px"><a href="http://dacavtricks.files.wordpress.com/2011/11/issue.png"><img class="size-full wp-image-181" title="issue" src="http://dacavtricks.files.wordpress.com/2011/11/issue.png?w=500&#038;h=282" alt="" width="500" height="282" /></a><p class="wp-caption-text">The problem… I hate warnings!</p></div>
<p>The solution comes strightforward: compile <code>src/conf.erl</code> <strong>before</strong> anything else. How to do that? Well, I surfed the Internet, searching for solutions. It turns out that, for instance, <a href="http://www.process-one.net/en/ejabberd/" target="_blank"><em>ejabberd</em></a> is using <em>Autotools</em> instead of the standard <em>Erlang</em> compile system. Autotools are a really nice piece of <del>crap</del> software, but I don&#8217;t have time to discover how to use it with <em>Erlang</em>.</p>
<p>Then I just reconnected my brain…</p>
<div id="attachment_182" class="wp-caption aligncenter" style="width: 510px"><a href="http://dacavtricks.files.wordpress.com/2011/11/solution.png"><img class="size-full wp-image-182" title="solution" src="http://dacavtricks.files.wordpress.com/2011/11/solution.png?w=500&#038;h=282" alt="" width="500" height="282" /></a><p class="wp-caption-text">Easy huh?</p></div>
<p><strong>Note:</strong> Place the <pre class="brush: plain;">-pa</pre> flag <strong>before</strong> <pre class="brush: plain;">-make</pre>, otherwise the <em>make</em> command will not consider the output directory in the path.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/179/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=179&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2011/11/18/erlang-make-give-priority-to-behaviors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>

		<media:content url="http://dacavtricks.files.wordpress.com/2011/11/issue.png" medium="image">
			<media:title type="html">issue</media:title>
		</media:content>

		<media:content url="http://dacavtricks.files.wordpress.com/2011/11/solution.png" medium="image">
			<media:title type="html">solution</media:title>
		</media:content>
	</item>
		<item>
		<title>Sparse matrix with Python</title>
		<link>http://dacavtricks.wordpress.com/2011/11/08/sparse-matrix/</link>
		<comments>http://dacavtricks.wordpress.com/2011/11/08/sparse-matrix/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 12:03:47 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Zen coding]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=168</guid>
		<description><![CDATA[For some applications you may have big sparse matrices, basicallly it&#8217;s filled of zeros everywhere, except for some points in which you setted some value, depending on your application logic. In this case a whole N×M matrix is a waste of memory (expecially for huge N and M) not to mention the fact you may <a href="http://dacavtricks.wordpress.com/2011/11/08/sparse-matrix/" class="excerpt-more-link">[&#8230;]</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=168&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For some applications you may have big sparse matrices, basicallly it&#8217;s filled of zeros everywhere, except for some points in which you setted some value, depending on your application logic. In this case a whole N×M matrix is a waste of memory (expecially for huge N and M) not to mention the fact you may want a matrix with more than two dimensions!</p>
<p>As you probably know, a good idea here would be using a mapping instead, say a <em>Hash table</em>, to store the elements you would put in the matrix with the coordinates as keys. In order to explain this concept to a friend of mine, I sketched an implementation of this principle in <em>Python</em>, and soon I realized it&#8217;s extremely easy!</p>
<p><span id="more-168"></span>Just look at this:<br />
<code><br />
&gt;&gt;&gt; class Foo:<br />
... def __getitem__ (self, *args): print(args); return 0<br />
... def __setitem__ (self, *args): print(args);<br />
...<br />
&gt;&gt;&gt; test = Foo()<br />
&gt;&gt;&gt; test[1]<br />
(1,)<br />
0<br />
&gt;&gt;&gt; test[1,2,3]<br />
((1, 2, 3),)<br />
0<br />
&gt;&gt;&gt; test[5,6,7] = 100<br />
((5, 6, 7), 100)<br />
&gt;&gt;&gt;<br />
</code></p>
<p>Does that ring some bell in your mind?<br />
<code><br />
&gt;&gt;&gt; M = dict()<br />
&gt;&gt;&gt; M[1,2] = 300<br />
&gt;&gt;&gt; M[1,2]<br />
300<br />
</code></p>
<p>Yes, is that simple. It has just one drawback:<br />
<code><br />
&gt;&gt;&gt; M[100,200]<br />
Traceback (most recent call last):<br />
File "", line 1, in<br />
KeyError: (100, 200)<br />
</code></p>
<p>But it can be solved <strong>very</strong> easily! <a title="Python: default values in a dictionary" href="http://dacavtricks.wordpress.com/2011/05/23/python-default-values-in-a-dictionary/">Do you remember</a> how awesome is the <em>collections.defaultdict</em> class?<br />
<code><br />
&gt;&gt;&gt; from collections import defaultdict<br />
&gt;&gt;&gt; M = defaultdict(lambda : 0)<br />
&gt;&gt;&gt; M[1,2] = 300<br />
&gt;&gt;&gt; M[1,2]<br />
300<br />
&gt;&gt;&gt; M[100,200]<br />
0<br />
</code></p>
<p>Surprisely nice huh? Happy hacking!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/168/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=168&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2011/11/08/sparse-matrix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>
	</item>
		<item>
		<title>The with statement</title>
		<link>http://dacavtricks.wordpress.com/2011/06/25/the-with-statement/</link>
		<comments>http://dacavtricks.wordpress.com/2011/06/25/the-with-statement/#comments</comments>
		<pubDate>Sat, 25 Jun 2011 11:07:17 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Links]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=119</guid>
		<description><![CDATA[I heard about Python&#8216;s with statement, however I didn&#8217;t know about it. I suggest this link: it explains how that works in an effective and concise way.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=119&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I heard about <em>Python</em>&#8216;s <em>with</em> statement, however I didn&#8217;t know about it. I suggest <a href="http://effbot.org/zone/python-with-statement.htm" target="_blank">this</a> link: it explains how that works in an effective and concise way.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/119/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=119&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2011/06/25/the-with-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>
	</item>
		<item>
		<title>Private Symbols</title>
		<link>http://dacavtricks.wordpress.com/2011/06/01/private-symbols/</link>
		<comments>http://dacavtricks.wordpress.com/2011/06/01/private-symbols/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 18:48:13 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Zen coding]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[disassemble]]></category>
		<category><![CDATA[object code]]></category>
		<category><![CDATA[Stack Overflow]]></category>
		<category><![CDATA[static]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=114</guid>
		<description><![CDATA[The wise C programmer knows how souce code gets linked, so he or she is also aware of how symbols are managed into object code. In our module we typically want to export some symbols, like for instance a function or, if you really need to do it a global variable. Note also that types <a href="http://dacavtricks.wordpress.com/2011/06/01/private-symbols/" class="excerpt-more-link">[&#8230;]</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=114&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="bodyContent">
<p>The wise C programmer knows how souce code gets linked, so he or she is also aware of how symbols are managed into object code. In our module we typically want to export some symbols, like for instance <em>a function</em> or, if you really need to do it <em>a global variable</em>. Note also that types are not object symbols.<span id="more-114"></span>It&#8217;s useful to figure symbols from two distinct point of view:</p>
<ol>
<li>From the <em>syntactical/sematical</em> point of view, you&#8217;ve the <strong>prototypes</strong>:
<ol>
<li>The programmer puts them in the header file;</li>
<li>The compiler uses them for static checking.</li>
</ol>
</li>
<li>From the <em>low level</em> side there are <strong>object symbols</strong>:
<ol>
<li>The assembler puts them in the object file;</li>
<li>The linker uses them as reference in order to produce the executable.</li>
</ol>
</li>
</ol>
<p>The private symbols which we are talking about are not supposed to be exported, thus they</p>
<ol>
<li>Don&#8217;t get represented in header files;</li>
<li>Are declared <em><strong>static</strong></em> in the module.</li>
</ol>
<p>A bit confused? An example will make it clear!</p>
<h2>Example module</h2>
<p>Suppose we want to provide a function which computes the squared norm of a vector <em>v</em>. The norm is defined as</p>
<p><img class="aligncenter" src="http://sourceforge.net/userapps/mediawiki/simgidacav/nfs/user/s/si/simgidacav/math/c/2/e/c2e35eec359a9e4efb4f26c0f6cd0497.png" alt="||v|| = \sqrt{\sum_{i=1}^n v_i^2}" /></p>
<p>and we will provide | | <em>v</em> | | <sup>2</sup>. We need the function <em>square</em> in order to compute <img src="http://sourceforge.net/userapps/mediawiki/simgidacav/nfs/user/s/si/simgidacav/math/0/b/c/0bc218b415c1f9c4edb0d564c6223f14.png" alt="v_i^2" />, but we don&#8217;t want to export it, since it&#8217;s just an auxiliary function.</p>
<p><a id="Natural_source_code" name="Natural_source_code"></a></p>
<h3>Naive source code</h3>
<p>Let&#8217;s write it in a naive way: for the moment we just write the needed functions in the file <tt>norm.c</tt>:</p>
<pre>#include "norm.h"

double square (double value)
{
    return value * value;
}

double norm (double values[], int n)
{
    double acc = 0;
    int i;

    for (i = 0; i &lt; n; i ++)
        acc += square(values[i]);
    return acc;
}</pre>
<p>The header file (<tt>norm.h</tt>) is just what you expect: we define the <tt>norm</tt> function, and not the <tt>square</tt> function:</p>
<pre>#ifndef __defined_norm_h
#define __defined_norm_h

double norm (double values[], int n);

#endif /* __defined_norm_h */</pre>
<p>The main will be the following:</p>
<pre>#include &lt;stdio.h&gt;
#include "norm.h"

int main (int argc, char **argv)
{
    double values [] = { 1.0, 3.3, 4.2, 1.2, 9.0 };
    printf("norm is: %G\n", norm(values, 5));
    return 0;
}</pre>
<p><a id="Object_code_after_compilation" name="Object_code_after_compilation"></a></p>
<h3>Object code after compilation</h3>
<p>Of course this natural approach is functional enough, now let&#8217;s make it classy. First let&#8217;s check what happens after compilation:</p>
<pre>dacav@mithril:&lt;test&gt;$ ls
main.c  norm.c  norm.h
dacav@mithril:&lt;test&gt;$ gcc norm.c -Wall -c
dacav@mithril:&lt;test&gt;$ nm norm.o
0000000000000015 T norm
0000000000000000 T square
dacav@mithril:&lt;test&gt;$</pre>
<p>As you can see, despite the fact we declared only <tt>norm</tt> in the header file, the object file still exports <tt>square</tt> in the <em>.text</em> section. Pragmatically speaking, this is potentially dangerous only in some cases (which we&#8217;ll see in a while). However this lets us do something stupid right now. Let&#8217;s write this alternative <tt>main</tt> into the file <tt>jerk.c</tt>:</p>
<pre>#include &lt;stdio.h&gt;

extern double square (double);

int main (int argc, char **argv)
{
    printf("2 square is %G\n", square(2));
    return 0;
}</pre>
<p>And link it with the object file we produced before:</p>
<pre>dacav@mithril:&lt;test&gt;$ ls
a.out*  jerk.c  main.c  norm.c  norm.h  norm.o
dacav@mithril:&lt;test&gt;$ gcc jerk.c norm.o
dacav@mithril:&lt;test&gt;$ ./a.out
2 square is 4
dacav@mithril:&lt;test&gt;$</pre>
<p>As you can see we managed to use the symbol even if it was supposed to be private. You may argue that doing this is kind of a hack you&#8217;ll never do, but it&#8217;s a nice proof of concept.</p>
<p>If we want to do everything right, we can avoid this pretty easily: just add the <strong><tt>static</tt></strong> keyword for anything is not declared in the header. In our case we make <tt>static</tt> only the <tt>square</tt> function:</p>
<pre>static
double square (double value)
{
    return value * value;
}</pre>
<p>Let&#8217;s see what happens:</p>
<pre>dacav@mithril:&lt;test&gt;$ gcc norm.c -Wall -c
dacav@mithril:&lt;test&gt;$ nm norm.o
0000000000000015 T norm
0000000000000000 t square
dacav@mithril:&lt;test&gt;$</pre>
<p>Awesome! The object file still has the <tt>square</tt> symbol in the <em>.text</em> section, but it&#8217;s no longer exported! Let&#8217;s try to comple again <tt>jerk.c</tt> as we did before.</p>
<pre>dacav@mithril:&lt;test&gt;$ gcc jerk.c norm.o
/tmp/cc5yYxqr.o: In function `main':
jerk.c:(.text+0x18): undefined reference to `square'
collect2: ld returned 1 exit status
dacav@mithril:&lt;test&gt;$</pre>
<p><a id="Advantages_and_Threats" name="Advantages_and_Threats"></a></p>
<h2>Advantages and Threats</h2>
<p><a id="Avoid_namespace_pollution" name="Avoid_namespace_pollution"></a></p>
<h3>Avoid namespace pollution</h3>
<p>As you probably know, the C language doesn&#8217;t provide a <em>namespace</em> mechanism as C++ or other languages do. As consequence all symbols exported by a <em>modern enough</em> library are typically decorated with some common prefix. The symbols exported by the <em>OpenGL</em> API, for instance, use the <em>gl</em> prefix. This doesn&#8217;t give any further information on what the symbol is about: it&#8217;s there just for technical reasons.</p>
<p>Within your module you certainly know what your <em>namespace</em> looks like. Provided that any externally linked library has its common prefix, you don&#8217;t want to add prefixes for your private functions as well, since you already know what is module you are writing. In a scenario in which all modules declare private objects as <tt>static</tt>, any ambiguity is restricted to exported symbols, which are wisely prefixed in order to avoid problems.</p>
<p><a id="Not_so_hidden_ambiguity" name="Not_so_hidden_ambiguity"></a></p>
<h3>Not so hidden ambiguity</h3>
<p>Let&#8217;s hack again the very first version of our example (the one without <tt>static</tt>), and suppose we made a mistake in naming <tt>square</tt>:</p>
<pre>// should be "square"!
double squae (double value)
{
    return value * value;
}</pre>
<p>Also let&#8217;s add another file, <tt>something.c</tt>, which declares a totally unrelated function <tt>square</tt> which is supposed to be private. Such a function has also a different signature and does exactly nothing. As we said before, types are not symbols!</p>
<pre>dacav@mithril:&lt;test&gt;$ more something.c
void square (void) { }</pre>
<p>Now let&#8217;s compile everything from scratch.</p>
<pre>dacav@mithril:&lt;test&gt;$ ls
a.out*  jerk.c  main.c  norm.c  norm.h  norm.o  something.c  something.o
dacav@mithril:&lt;test&gt;$ rm *o a.out
dacav@mithril:&lt;test&gt;$ ls
jerk.c  main.c  norm.c  norm.h  something.c
dacav@mithril:&lt;test&gt;$ gcc -c norm.c
dacav@mithril:&lt;test&gt;$ gcc -c something.c
dacav@mithril:&lt;test&gt;$ gcc main.c *o</pre>
<p>Note that, for the moment, we are not compiling with the <em>-Wall</em> flag (later on I&#8217;ll tell you why). Ready for the execution?</p>
<pre>dacav@mithril:&lt;test&gt;$ ./a.out
norm is: 5
dacav@mithril:&lt;test&gt;$</pre>
<p>Which is <strong>wrong</strong>: the correct norm is 111.97! What happened?</p>
<ol>
<li>We forgot to make private things static;</li>
<li>We also wrote <tt>squae</tt> instead of <tt>square</tt></li>
<li>When the linker searched for the <tt>square</tt> function, he found the one declared in <tt>something.c</tt>. Since he&#8217;s not supposed to run static analysis, any <tt>square</tt> symbol into the <em>.text</em> section is good enough for him!</li>
<li>The <tt>square</tt> function of <tt>something.c</tt> does nothing, so the return value is just the content of the return register (which depends on the [<a title="http://en.wikipedia.org/wiki/Application_binary_interface" href="http://en.wikipedia.org/wiki/Application_binary_interface" rel="nofollow">ABI</a>]).</li>
</ol>
<p><a id="Crashed_into_the_-Wall" name="Crashed_into_the_-Wall"></a></p>
<h3>Crashed into the -Wall</h3>
<p>Things screwed. Let&#8217;s do it again with the <em>-Wall</em> option:</p>
<pre>dacav@mithril:&lt;test&gt;$ ls
a.out*  jerk.c  main.c  norm.c  norm.h  norm.o  something.c  something.o
dacav@mithril:&lt;test&gt;$ rm *o a.out
dacav@mithril:&lt;test&gt;$ ls
jerk.c  main.c  norm.c  norm.h  something.c
dacav@mithril:&lt;test&gt;$ gcc -Wall -c norm.c
norm.c: In function ‘norm’:
norm.c:14: warning: implicit declaration of function ‘square’
dacav@mithril:&lt;test&gt;$</pre>
<p>Well, this saves the day. The compiler noticed that we are using a function which is not declared by an header. Note that the <tt>square</tt> function is not listed into the <tt>norm.h</tt> file!</p>
<p>The <em>-Wall</em> option seems to make this discussion pointless, but what if we are using a private function prototypes inside a module? This happens, for instance when we have two functions which reference one another.</p>
<p>Also take this in account: there&#8217;s plenty of guys out there, and many of them are not supposed to be programmers. Some day they will need to write some (typically poorly designed) software. They hate warnings or don&#8217;t know what the <em>-Wall</em>option is&#8230; You got the concept, don&#8217;t you?</p>
<h2>Not in C++</h2>
<p><a title="http://stackoverflow.com/questions/154469/unnamed-anonymous-namespaces-vs-static-functions" href="http://stackoverflow.com/questions/154469/unnamed-anonymous-namespaces-vs-static-functions" rel="nofollow">As I learned</a>, using <tt>static</tt> in C++ is possible but not advisable. By declaring an <em>anonymous namespace</em> you can obtain the same protection as in <tt>static</tt>, but you can also protect types. I cannot figure if this is actually useful, however.</p>
<h2>Notes</h2>
<p>I learned this information by studying how the <a title="http://en.wikipedia.org/wiki/Executable_and_Linkable_Format" href="http://en.wikipedia.org/wiki/Executable_and_Linkable_Format" rel="nofollow">ELF</a> format works.</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=114&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2011/06/01/private-symbols/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>

		<media:content url="http://sourceforge.net/userapps/mediawiki/simgidacav/nfs/user/s/si/simgidacav/math/c/2/e/c2e35eec359a9e4efb4f26c0f6cd0497.png" medium="image">
			<media:title type="html">&#124;&#124;v&#124;&#124; = \sqrt{\sum_{i=1}^n v_i^2}</media:title>
		</media:content>

		<media:content url="http://sourceforge.net/userapps/mediawiki/simgidacav/nfs/user/s/si/simgidacav/math/0/b/c/0bc218b415c1f9c4edb0d564c6223f14.png" medium="image">
			<media:title type="html">v_i^2</media:title>
		</media:content>
	</item>
		<item>
		<title>Attributes Doxygen</title>
		<link>http://dacavtricks.wordpress.com/2011/06/01/attributes-doxygen/</link>
		<comments>http://dacavtricks.wordpress.com/2011/06/01/attributes-doxygen/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 18:42:33 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Doxygen]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=111</guid>
		<description><![CDATA[Sometime you need to use the attributesfor structs or functions. By example you may define: typedef struct { uint8_t foo; uint16_t bar; } __attribute__((packed)) foo_t; From the doxygen point of view this could be nasty, since it will start thinking __attribute__((packed)) is the name of the struct. Doxygen provides a solution to this through a <a href="http://dacavtricks.wordpress.com/2011/06/01/attributes-doxygen/" class="excerpt-more-link">[&#8230;]</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=111&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometime you need to use the attributesfor structs or functions. By example you may define:</p>
<pre>typedef struct {
    uint8_t foo;
    uint16_t bar;
} __attribute__((packed)) foo_t;</pre>
<p>From the doxygen point of view this could be nasty, since it will start thinking <tt>__attribute__((packed))</tt> is the name of the struct.</p>
<p>Doxygen provides a solution to this through a special command inside the Doxyfile:</p>
<pre># The PREDEFINED tag can be used to specify one or more macro names that
# are defined before the preprocessor is started (similar to the -D option of
# gcc). The argument of the tag is a list of macros of the form: name
# or name=definition (no spaces). If the definition and the = are
# omitted =1 is assumed. To prevent a macro definition from being
# undefined via #undef or recursively expanded use the := operator
# instead of the = operator.

PREDEFINED             =</pre>
<p>So we can just put a <tt>PREDEFINED = DOXYGEN</tt> and modify our code as follows:</p>
<pre>typedef struct {
    uint8_t foo;
    uint16_t bar;
}
#ifndef DOXYGEN
__attribute__((packed))
#endif
foo_t;</pre>
<h2>Notes</h2>
<p>I learned this trick by reading Alsa ASoundLib&#8217;s source code.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=111&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2011/06/01/attributes-doxygen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>
	</item>
		<item>
		<title>Python: default values in a dictionary</title>
		<link>http://dacavtricks.wordpress.com/2011/05/23/python-default-values-in-a-dictionary/</link>
		<comments>http://dacavtricks.wordpress.com/2011/05/23/python-default-values-in-a-dictionary/#comments</comments>
		<pubDate>Mon, 23 May 2011 12:26:41 +0000</pubDate>
		<dc:creator>knight5</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Zen coding]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://dacavtricks.wordpress.com/?p=97</guid>
		<description><![CDATA[Sometimes it&#8217;s useful to define an associative array which maps a certain key to a list of elements. This is used basically everywhere you need to achieve some categorization. Just to give an example, suppose we want to group a bunch of people by name. Each person has an unique id, while there may be <a href="http://dacavtricks.wordpress.com/2011/05/23/python-default-values-in-a-dictionary/" class="excerpt-more-link">[&#8230;]</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=97&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes it&#8217;s useful to define an associative array which maps a certain key to a list of elements. This is used basically everywhere you need to achieve some categorization. Just to give an example, suppose we want to group a bunch of people by name. Each person has an unique id, while there may be multiple guys with the same name.</p>
<p><span id="more-97"></span>In a situation like this we need a dictionary mapping <em>strings</em> to <em>lists of identifiers</em>, and if we see a name for the first time we want an empty list.</p>
<p><strong>Simple and stupid way:</strong></p>
<pre>def insert (D, name, id):
    if not name in D:
        l = []
        D[name] = l
    else:
        l = D[name]
    l.append(id)

foo = dict()
insert(foo, "Jack", 2)
insert(foo, "John", 3)
insert(foo, "John", 4)</pre>
<p>What we get:</p>
<pre>&gt;&gt;&gt; foo
{'John': [3, 4], 'Jack': [2]}</pre>
<p><strong>A shorter way:</strong></p>
<pre>def insert (D, name, id):
    l = D.get(name, [])
    l.append(id)
    D[name] = l</pre>
<p>Advantage: less rows of code;<br />
Drawback: we <em>always</em> spawn a new list as default value.</p>
<p><strong>Best way: <em>Proudly Found Elsewhere</em></strong></p>
<p>Python library provides <a href="http://docs.python.org/library/collections.html?highlight=collections#collections.defaultdict">this</a>, so we get this functionality for free:</p>
<pre>from collections import defaultdict
...
D = defaultdict(list)
D["jack"].append(2)
D["john"].append(3)
D["john"].append(4)</pre>
<p>What we get:</p>
<pre>&gt;&gt;&gt; D
defaultdict(, {'john': [3, 4], 'jack': [2]})</pre>
<p><strong>Bonus trick:</strong></p>
<p>What if we need to associate a single incremental identifier to each element of a certain set of names? Check this out:</p>
<pre>import itertools
from collections import defaultdict
...
class Cnt :
    def __init__ (self):
        self.__call__ = itertools.count().next

idgen = defaultdict(Cnt())</pre>
<p>We obtain this:</p>
<pre>&gt;&gt;&gt; idgen['foo']
0
&gt;&gt;&gt; idgen['bar']
1
&gt;&gt;&gt; idgen['foo']
0
&gt;&gt;&gt; idgen['fubar']
2</pre>
<p>Oh! Actually we can obtain the same result in a shorter (and maybe cleaner) way:</p>
<pre>idgen = defaultdict(itertools.count().next)
&gt;&gt;&gt; idgen['foo']
0
&gt;&gt;&gt; idgen['bar']
1
&gt;&gt;&gt; idgen['foo']
0
&gt;&gt;&gt; idgen['fubar']
2
&gt;&gt;&gt; idgen
defaultdict(&lt;method-wrapper 'next' of itertools.count object at 0xa815a8&gt;,
{'foo': 0, 'bar': 1, 'fubar': 2})</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dacavtricks.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dacavtricks.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dacavtricks.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dacavtricks.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dacavtricks.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dacavtricks.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dacavtricks.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dacavtricks.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dacavtricks.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dacavtricks.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dacavtricks.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dacavtricks.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dacavtricks.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dacavtricks.wordpress.com/97/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dacavtricks.wordpress.com&amp;blog=19947543&amp;post=97&amp;subd=dacavtricks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dacavtricks.wordpress.com/2011/05/23/python-default-values-in-a-dictionary/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9cd35c711e222946f9503628827a32ce?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">knight5</media:title>
		</media:content>
	</item>
	</channel>
</rss>
