<?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>Perfect Storm &#187; metaprogramming</title>
	<atom:link href="http://tiago.org/ps/category/metaprogramming/feed/" rel="self" type="application/rss+xml" />
	<link>http://tiago.org/ps</link>
	<description>Computational Biology, Epidemiology, Infectious diseases, Open Science</description>
	<lastBuildDate>Mon, 26 Jul 2010 09:31:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Holy Grail: The quest for THE programming language</title>
		<link>http://tiago.org/ps/2008/02/16/holy-grail-the-quest-for-the-programming-language/</link>
		<comments>http://tiago.org/ps/2008/02/16/holy-grail-the-quest-for-the-programming-language/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 21:43:21 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[bioinformatics]]></category>
		<category><![CDATA[declarative programming]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[science]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://tiago.org/ps/2008/02/16/holy-grail-the-quest-for-the-programming-language/</guid>
		<description><![CDATA[Being a computer scientist with a strong interest in languages (languages in the broadest sense possible: programming, natural and cognition related issues), I am in an holy grail quest for a programming language that: First and foremost allows me to express my computations in a way that is close to the problem domain (as opposed [...]]]></description>
			<content:encoded><![CDATA[<p>Being a computer scientist with a strong interest in languages (languages in the broadest sense possible: programming, natural and cognition related issues), I am in an holy grail quest for a programming language that:</p>
<p>First and foremost allows me to express my computations in a way that is close to the problem domain (as opposed to close to the machine). As I am working in a biology setting that means being able to talk about concepts around genes, epidemics and pharmacology in my programs. I don&#8217;t want to think about CPUs, memories and things like that when I am coding. Prolog and Lisp are good examples here. I also need programs that can evolve over time as knowledge changes, I need strong metaprogramming and Domain Specific Language facilities.</p>
<p>Unfortunately I have a couple more requirements coming from the day to day reality&#8230;</p>
<p>Real world: I want a language that interacts with existing libraries and that I can easily make available to other people to use, inspect and change. I need Bio* libraries, graphics plotting libraries. I my personal case I decided that I want to work inside the JVM, so I need a language that works in the Java world (Jython, JRuby, Scala, Groovy, &#8230; Java).</p>
<p>Software engineering: Programs have to be easy to maintain and debug. I guess there is no way around explicit typing on the debug and tool construction front.</p>
<p>Ridiculous religious fanatic quest? Yes, it might be, but I am pursing it.</p>
<p>The truth is that we are not far away from this grail.</p>
<p>Scala is almost there. Lacks metaprogramming and things like type inference are a bit amateurish (compare it with CAML).</p>
<p>JRuby is maybe there, I could live with it, I guess. The lack of explicit typing will make things difficult in the long run on the software engineering front.</p>
<p>I decided to give a final try to yet another language: Groovy, and up to now it is going very OK. Seems to nail all the fundamental points. I especially love the effort on good metaprogramming facilities.</p>
<p>I decided, for pragmatic reasons, that after this one I will stop my pursuit for the grail. If Groovy proves a blunder of some sorts I will revert to JRuby and carry on.</p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Share and Enjoy:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F&amp;title=Holy%20Grail%3A%20The%20quest%20for%20THE%20programming%20language&amp;bodytext=Being%20a%20computer%20scientist%20with%20a%20strong%20interest%20in%20languages%20%28languages%20in%20the%20broadest%20sense%20possible%3A%20programming%2C%20natural%20and%20cognition%20related%20issues%29%2C%20I%20am%20in%20an%20holy%20grail%20quest%20for%20a%20programming%20language%20that%3A%0D%0A%0D%0AFirst%20and%20foremost%20allows%20me" title="Digg"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F&amp;title=Holy%20Grail%3A%20The%20quest%20for%20THE%20programming%20language&amp;notes=Being%20a%20computer%20scientist%20with%20a%20strong%20interest%20in%20languages%20%28languages%20in%20the%20broadest%20sense%20possible%3A%20programming%2C%20natural%20and%20cognition%20related%20issues%29%2C%20I%20am%20in%20an%20holy%20grail%20quest%20for%20a%20programming%20language%20that%3A%0D%0A%0D%0AFirst%20and%20foremost%20allows%20me" title="del.icio.us"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F&amp;t=Holy%20Grail%3A%20The%20quest%20for%20THE%20programming%20language" title="Facebook"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F&amp;title=Holy%20Grail%3A%20The%20quest%20for%20THE%20programming%20language&amp;annotation=Being%20a%20computer%20scientist%20with%20a%20strong%20interest%20in%20languages%20%28languages%20in%20the%20broadest%20sense%20possible%3A%20programming%2C%20natural%20and%20cognition%20related%20issues%29%2C%20I%20am%20in%20an%20holy%20grail%20quest%20for%20a%20programming%20language%20that%3A%0D%0A%0D%0AFirst%20and%20foremost%20allows%20me" title="Google Bookmarks"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.connotea.org/addpopup?continue=confirm&amp;uri=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F&amp;title=Holy%20Grail%3A%20The%20quest%20for%20THE%20programming%20language&amp;description=Being%20a%20computer%20scientist%20with%20a%20strong%20interest%20in%20languages%20%28languages%20in%20the%20broadest%20sense%20possible%3A%20programming%2C%20natural%20and%20cognition%20related%20issues%29%2C%20I%20am%20in%20an%20holy%20grail%20quest%20for%20a%20programming%20language%20that%3A%0D%0A%0D%0AFirst%20and%20foremost%20allows%20me" title="connotea"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/connotea.png" title="connotea" alt="connotea" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F&amp;title=Holy%20Grail%3A%20The%20quest%20for%20THE%20programming%20language" title="DZone"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F&amp;title=Holy%20Grail%3A%20The%20quest%20for%20THE%20programming%20language" title="Reddit"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Holy%20Grail%3A%20The%20quest%20for%20THE%20programming%20language&amp;url=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F" title="Slashdot"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F&amp;title=Holy%20Grail%3A%20The%20quest%20for%20THE%20programming%20language" title="StumbleUpon"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftiago.org%2Fps%2F2008%2F02%2F16%2Fholy-grail-the-quest-for-the-programming-language%2F" title="Technorati"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://tiago.org/ps/2008/02/16/holy-grail-the-quest-for-the-programming-language/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Modeling drugs in Scala</title>
		<link>http://tiago.org/ps/2007/11/01/modeling-drugs-in-scala/</link>
		<comments>http://tiago.org/ps/2007/11/01/modeling-drugs-in-scala/#comments</comments>
		<pubDate>Thu, 01 Nov 2007 13:40:17 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[bioinformatics]]></category>
		<category><![CDATA[declarative programming]]></category>
		<category><![CDATA[malaria]]></category>
		<category><![CDATA[metaprogramming]]></category>

		<guid isPermaLink="false">http://tiago.org/ps/2007/11/01/modeling-drugs-in-scala/</guid>
		<description><![CDATA[I am currently trying to model antimalarial drug behavior in order to understand the spread of drug resistant malaria. Generally speaking, malaria strains are more or less tolerant to a drug depending on the quantity of drug that is necessary to kill an infection. In theory, a totally resistant infection will survive any treatment, a [...]]]></description>
			<content:encoded><![CDATA[<p>I am currently trying to model antimalarial drug behavior in order to understand the spread of drug resistant malaria. Generally speaking, malaria strains are more or less tolerant to a drug depending on the quantity of drug that is necessary to kill an infection. In theory, a totally resistant infection will survive any treatment, a totally susceptible one will only require small levels of drug to be cleaned.</p>
<p>I see the word drug used in two different ways (for the readers of this blog that are specialists, in some form, on issues regarding drugs, particularly pharmacokinetics, if you see any thing particularly wrong, please do inform me): For instance, SP (Fansidar) is a drug, composed of two drugs (Sulfadoxine and Pyrimethamine). I will use drug for SP and compound for S and P (as active compound seems to be used).</p>
<p>Antimalarial drugs work mainly in the blood stream against asexual parasite forms.</p>
<p>In the blood, compounds have a certain concentration. With time, the body gets rid of compounds (thus the concentration of a compound goes down with time). The concentration of compounds is normally (but not always) modeled using an exponential decay function, being the fundamental parameter the half-life, i.e, the time that it takes for the concentration of a compound to drop to half.</p>
<p>Two other important concepts for drugs that are not taken intravenously (like cheap antimalarials which are oral),  are</p>
<ol>
<li>Bioavailability, i.e. the fraction of the compound that actually reaches the circulation. It seems that one of the problems with counterfeit drugs is low bioavailability. Bioavailability is normally discussed in terms of AUC (Area Under the Curve. Being the curve related to the plot of drug concentration against time). I will model it in terms of maximum concentration, half-life and the time it takes to reach maximum concentration in the blood, which by the way is the next concept&#8230;
<li>The time it takes to reach maximum concentration in the blood, i.e. the time from ingestion to circulation in the blood at maximum concentration. I suppose this time frame has a technical name, but I don&#8217;t know it (if you know, drop me an email our comment, please).</li>
</ol>
<p>Now, back to computational modeling:</p>
<p>A big objective is declarative programming. Preferably a program that can be read by domain specialists (biologists, MDs, biostatisticians, &#8230;), with that in mind&#8230;</p>
<p>Currently, a computer program in Scala to model drugs look like this.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">Compound create <span style="color: #6666FF;">&quot;Sulfadoxine&quot;</span>
Compound abbreviation <span style="color: #6666FF;">&quot;S&quot;</span>
Compound half<span style="color: #000080;">_</span>life <span style="color: #F78811;">116</span> <span style="color: #008000; font-style: italic;">//hours</span>
Compound bio<span style="color: #000080;">_</span>availability <span style="color: #F78811;">408</span> <span style="color: #008000; font-style: italic;">//1mg to nanoM</span>
<span style="color: #0000ff; font-weight: bold;">val</span> Sulfadoxine <span style="color: #000080;">=</span> Compound prepare
&nbsp;
Compound create <span style="color: #6666FF;">&quot;Pyrimethamine&quot;</span>
Compound abbreviation <span style="color: #6666FF;">&quot;P&quot;</span>
Compound half<span style="color: #000080;">_</span>life <span style="color: #F78811;">83</span> <span style="color: #008000; font-style: italic;">//hours</span>
Compound bio<span style="color: #000080;">_</span>availability <span style="color: #F78811;">34</span> <span style="color: #008000; font-style: italic;">//1mg to nanoM</span>
<span style="color: #0000ff; font-weight: bold;">val</span> Pyrimethamine <span style="color: #000080;">=</span> Compound prepare
&nbsp;
Drug create <span style="color: #6666FF;">&quot;SP&quot;</span>
Drug includes Sulfadoxine quantity <span style="color: #F78811;">500</span>
Drug includes Pyrimethamine quantity <span style="color: #F78811;">25</span>
<span style="color: #0000ff; font-weight: bold;">val</span> SP <span style="color: #000080;">=</span> Drug prepare</pre></div></div>

<p>Discussion:</p>
<ul>
<li>I am using the &#8220;object companion&#8221; pattern a lot. The idea is that all &#8220;stateful&#8221; mess is stored &#8220;prepared&#8221; in the object (which is the DSL source). When the prepare method is invoked in the object a class (with only immutable vals, very lovely for those of you who are functional programming enthusiasts) is created.
<li>Notice the dependence on operator precedence on <b>Drug includes quantity</b> (there is not really one, strictly speaking, but assume there is). I would really like to have, per class the ability to define operator precedence, other than not based on dictionary order (à la Prolog).
<li>I don&#8217;t like the <b>val SP = Drug prepare</b>. It is too verbose and too geeky. I would prefer just <b>Drug prepare</b>. I believe that this is possible in Scala as at least at the interpreter level (as the Scala interpreter does it), but I still don&#8217;t know how. The idea would be that a val named SP would be added to the local scope in some way. For those computer inclined readers that think that I am being too pedantic and nit-picking, I just have one thing too say: I am really trying to make the system the most pleasant possible to non programmer types, and I think my proposal does not sacrifice elegance and generality (although I would recognize the non-explicit name creation is &#8220;strange&#8221; &#8211; but, hey, the Scala interpreter already does it!)
</ul>
<p>Caveat emptor, big one: Although drugs (compounds) are discussed in terms of half-lives, bioavailability, etc&#8230; these properties are actually not of the drug but of the interaction between the drug and the individual. Making them drug properties only is a &#8220;cognitive abuse&#8221;, although it has its uses. For instance, my advisor, after looking at the language, was talking about bioavailability for counterfeit drugs, for children between 2 and 5 years. A great example that they are not properties <i>only</i> of the drugs but also, at least, of individuals (and not only that, for instance many drugs are more bioavailable if there are taken in conjunction with, say, fatty foods).</p>
<p>A proper, precise, computational modeling of drugs would be a gigantic undertaking. I have a different approach: Modeling as close as possible to the average domain discourse and hook, in some way, the necessary precision, should the need arise. It is worth noting that &#8220;incorrect&#8221;, &#8220;imprecise&#8221; modeling is enough for many tasks.</p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Share and Enjoy:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F&amp;title=Modeling%20drugs%20in%20Scala&amp;bodytext=I%20am%20currently%20trying%20to%20model%20antimalarial%20drug%20behavior%20in%20order%20to%20understand%20the%20spread%20of%20drug%20resistant%20malaria.%20Generally%20speaking%2C%20malaria%20strains%20are%20more%20or%20less%20tolerant%20to%20a%20drug%20depending%20on%20the%20quantity%20of%20drug%20that%20is%20necessary%20to%20kill" title="Digg"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F&amp;title=Modeling%20drugs%20in%20Scala&amp;notes=I%20am%20currently%20trying%20to%20model%20antimalarial%20drug%20behavior%20in%20order%20to%20understand%20the%20spread%20of%20drug%20resistant%20malaria.%20Generally%20speaking%2C%20malaria%20strains%20are%20more%20or%20less%20tolerant%20to%20a%20drug%20depending%20on%20the%20quantity%20of%20drug%20that%20is%20necessary%20to%20kill" title="del.icio.us"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F&amp;t=Modeling%20drugs%20in%20Scala" title="Facebook"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F&amp;title=Modeling%20drugs%20in%20Scala&amp;annotation=I%20am%20currently%20trying%20to%20model%20antimalarial%20drug%20behavior%20in%20order%20to%20understand%20the%20spread%20of%20drug%20resistant%20malaria.%20Generally%20speaking%2C%20malaria%20strains%20are%20more%20or%20less%20tolerant%20to%20a%20drug%20depending%20on%20the%20quantity%20of%20drug%20that%20is%20necessary%20to%20kill" title="Google Bookmarks"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.connotea.org/addpopup?continue=confirm&amp;uri=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F&amp;title=Modeling%20drugs%20in%20Scala&amp;description=I%20am%20currently%20trying%20to%20model%20antimalarial%20drug%20behavior%20in%20order%20to%20understand%20the%20spread%20of%20drug%20resistant%20malaria.%20Generally%20speaking%2C%20malaria%20strains%20are%20more%20or%20less%20tolerant%20to%20a%20drug%20depending%20on%20the%20quantity%20of%20drug%20that%20is%20necessary%20to%20kill" title="connotea"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/connotea.png" title="connotea" alt="connotea" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F&amp;title=Modeling%20drugs%20in%20Scala" title="DZone"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F&amp;title=Modeling%20drugs%20in%20Scala" title="Reddit"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Modeling%20drugs%20in%20Scala&amp;url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F" title="Slashdot"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F&amp;title=Modeling%20drugs%20in%20Scala" title="StumbleUpon"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F11%2F01%2Fmodeling-drugs-in-scala%2F" title="Technorati"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://tiago.org/ps/2007/11/01/modeling-drugs-in-scala/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>GUI metaprogramming example</title>
		<link>http://tiago.org/ps/2007/08/23/gui-metaprogramming-example/</link>
		<comments>http://tiago.org/ps/2007/08/23/gui-metaprogramming-example/#comments</comments>
		<pubDate>Thu, 23 Aug 2007 00:09:06 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Jython]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[bioinformatics]]></category>
		<category><![CDATA[metaprogramming]]></category>

		<guid isPermaLink="false">http://tiago.org/ps/2007/08/23/gui-metaprogramming-example/</guid>
		<description><![CDATA[Preamble This is an example of metaprogramming in Jython. I would really like to have a simpler example (either in just Python or Java), but this is directly taken from what I am doing. The idiom that I am using, Pythonwise, is a bit strange and old (I am using eval instead of __getattribute__), that [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Preamble</strong></p>
<p>This is an example of metaprogramming in Jython. I would really like to have a simpler example (either in just Python or Java), but this is directly taken from what I am doing. The idiom that I am using, Pythonwise, is a bit strange and old (I am using eval instead of __getattribute__), that is because of Jython&#8217;s limitations. This can be seen as a more advanced programming technique (If you are starting to learn programming, you might want to skip this for now, just to avoid excessive entropy in your learning process). Although this example is in Jython, it applies to many programming languages (Python, Java, Ruby, Prolog, &#8230;) but not C or C++ (or Caml, unfortunately).</p>
<p><strong>The problem at hand</strong></p>
<p>I am doing a selection detection workbench (to detect loci under selection). At certain points in time, I need to disallow the user to input data to a lot of entry fields, like these:</p>
<p><img border="1" src='http://tiago.org/ps/wp-content/uploads/2007/08/disabled.png' alt='Disabled fields' /></p>
<p>As you can see, they are all disabled.</p>
<p>How to do this? Option 1, go to all entry fields, one by one (more than 10, and changing) and call the method setEnabled(False). Lots of repeated code, and when there are changes I would have to add/remove a setEnabled.</p>
<p>Option 2. Do a piece of code to inspect my panel (a panel is what contains all the fields) object, check all object attributes that are entry fields and disable them. The point here is doing code that operates on the code itself. In this case, if one adds a new entry field to a panel, the code would automatically detect the field and disable it. How to code this?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> java.<span style="color: black;">awt</span>.<span style="color: black;">Component</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> disablePanel<span style="color: black;">&#40;</span>panel<span style="color: black;">&#41;</span>:
    attrs = <span style="color: #008000;">dir</span><span style="color: black;">&#40;</span>panel<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> attr <span style="color: #ff7700;font-weight:bold;">in</span> attrs:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'isinstance(panel.'</span> + attr + <span style="color: #483d8b;">', Component)'</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'panel.'</span> + attr + <span style="color: #483d8b;">'.setEnabled(False)'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">TypeError</span>: <span style="color: #808080; font-style: italic;">#Some attributes are write only</span>
            <span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>A small piece&#8230;<br />
Line 4 (function dir) gets all attributes for the panel object.<br />
Lines 7 and 8 do all the interesting work (eval, isinstance).<br />
First, eval takes a string and executes it, so if you have</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">i = <span style="color: #ff4500;">1</span>
i = <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'i+5'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> i</pre></div></div>

<p>Will print 6. eval is very powerful (think about the possibilities of changing code in runtime). It is also quite dangerous, but I will not discuss that here&#8230;</p>
<p>isinstance checks to see if a certain object is an instance of a certain class, so</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">i = <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>i, <span style="color: #008000;">int</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># Will print True</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>i, <span style="color: #008000;">str</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># Will print False</span></pre></div></div>

<p>So, back to our code<br />
if eval(&#8216;isinstance(panel.&#8217; + attr + &#8216;, Component)&#8217;):<br />
is evaluating if panel.&#8217;attribute name&#8217; is an instance of Component. For instance, my panel has a attribute, called core (storing the number of cores), which is a drop down list, so, when the code checks for isinstance(panel.core, Component), it will eval to True and execute the next line which is:</p>
<p>eval(&#8216;panel.&#8217; + attr + &#8216;.setEnabled(False)&#8217;)<br />
It will evaluate panel.&#8217;attribute name&#8217;.setEnabled(False), i.e., disable the field, in our previous example, it will do panel.core.setEnabled(False).</p>
<p>I will not explain the exception code as it is not important here.</p>
<p>So, a few lines now make it automatic to disable new entry fields, this without changing the code every time a field is added or removed (other than adding the field itself). Less code to maintain and less possibility of bugs.</p>
<p>I wanted just to illustrate the principle (the language used is not really important), but I need to stress out a fundamental point about this particular example in Python: Because of some Jython particularities I am using an old dialect to do this (Python gurus might be horrified), if you are using Python I recommend you to check __getattribute__ (to replace eval).</p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Share and Enjoy:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F&amp;title=GUI%20metaprogramming%20example&amp;bodytext=Preamble%0D%0A%0D%0AThis%20is%20an%20example%20of%20metaprogramming%20in%20Jython.%20I%20would%20really%20like%20to%20have%20a%20simpler%20example%20%28either%20in%20just%20Python%20or%20Java%29%2C%20but%20this%20is%20directly%20taken%20from%20what%20I%20am%20doing.%20The%20idiom%20that%20I%20am%20using%2C%20Pythonwise%2C%20is%20a%20bit%20strange%20and%20o" title="Digg"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F&amp;title=GUI%20metaprogramming%20example&amp;notes=Preamble%0D%0A%0D%0AThis%20is%20an%20example%20of%20metaprogramming%20in%20Jython.%20I%20would%20really%20like%20to%20have%20a%20simpler%20example%20%28either%20in%20just%20Python%20or%20Java%29%2C%20but%20this%20is%20directly%20taken%20from%20what%20I%20am%20doing.%20The%20idiom%20that%20I%20am%20using%2C%20Pythonwise%2C%20is%20a%20bit%20strange%20and%20o" title="del.icio.us"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F&amp;t=GUI%20metaprogramming%20example" title="Facebook"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F&amp;title=GUI%20metaprogramming%20example&amp;annotation=Preamble%0D%0A%0D%0AThis%20is%20an%20example%20of%20metaprogramming%20in%20Jython.%20I%20would%20really%20like%20to%20have%20a%20simpler%20example%20%28either%20in%20just%20Python%20or%20Java%29%2C%20but%20this%20is%20directly%20taken%20from%20what%20I%20am%20doing.%20The%20idiom%20that%20I%20am%20using%2C%20Pythonwise%2C%20is%20a%20bit%20strange%20and%20o" title="Google Bookmarks"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.connotea.org/addpopup?continue=confirm&amp;uri=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F&amp;title=GUI%20metaprogramming%20example&amp;description=Preamble%0D%0A%0D%0AThis%20is%20an%20example%20of%20metaprogramming%20in%20Jython.%20I%20would%20really%20like%20to%20have%20a%20simpler%20example%20%28either%20in%20just%20Python%20or%20Java%29%2C%20but%20this%20is%20directly%20taken%20from%20what%20I%20am%20doing.%20The%20idiom%20that%20I%20am%20using%2C%20Pythonwise%2C%20is%20a%20bit%20strange%20and%20o" title="connotea"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/connotea.png" title="connotea" alt="connotea" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F&amp;title=GUI%20metaprogramming%20example" title="DZone"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F&amp;title=GUI%20metaprogramming%20example" title="Reddit"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=GUI%20metaprogramming%20example&amp;url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F" title="Slashdot"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F&amp;title=GUI%20metaprogramming%20example" title="StumbleUpon"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftiago.org%2Fps%2F2007%2F08%2F23%2Fgui-metaprogramming-example%2F" title="Technorati"><img src="http://tiago.org/ps/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://tiago.org/ps/2007/08/23/gui-metaprogramming-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
