<?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>Alexandre Marinho &#187; metaprogramacao</title>
	<atom:link href="http://alexandre.cuboestudioweb.com/tag/metaprogramacao/feed/" rel="self" type="application/rss+xml" />
	<link>http://alexandre.cuboestudioweb.com</link>
	<description>Tecnologia, Programação, Gadgets, etc...</description>
	<lastBuildDate>Fri, 05 Aug 2011 12:51:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Um pouco de metaprogramação</title>
		<link>http://alexandre.cuboestudioweb.com/2009/10/um-pouco-de-metaprogramacao/</link>
		<comments>http://alexandre.cuboestudioweb.com/2009/10/um-pouco-de-metaprogramacao/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 13:39:32 +0000</pubDate>
		<dc:creator>alexandre</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[metaprogramacao]]></category>
		<category><![CDATA[software livre]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://alexandre.cuboestudioweb.com/?p=99</guid>
		<description><![CDATA[Quem assina a lista django-brasil recebeu essa mensagem do Luciano Ramalho sobre metaprogramação. Achei muito interressante e demonstra perfeitamente o quão poderosa é a linguagem Python. Estou reproduzindo o texto sem alterações: Em geral não é tedioso programar com o &#8230; <a href="http://alexandre.cuboestudioweb.com/2009/10/um-pouco-de-metaprogramacao/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Quem assina a lista <a href="http://groups.google.com.br/group/django-brasil" target="_blank">django-brasil</a> recebeu essa mensagem do <a href="http://blog.ramgarlic.com/" target="_blank">Luciano Ramalho</a> sobre metaprogramação. Achei muito interressante e demonstra perfeitamente o quão poderosa é a linguagem Python.</p>
<p>Estou reproduzindo o texto sem alterações:</p>
<p>Em geral não é tedioso programar com o Django, mas às vezes é, veja só quanta repetição:</p>
<p># <a href="http://pastebin.com/f2fc7b238" target="_blank">http://pastebin.com/f2fc7b238</a></p>
<pre>class DescriptorInline(admin.TabularInline):
   model = Descriptor

class RecruitmentCountryInline(admin.TabularInline):
   model = RecruitmentCountry

class OutcomeInline(admin.StackedInline):

   model = Outcome

class TrialInterventionCodeInline(admin.TabularInline):

   model = TrialInterventionCode

class SecondaryNumberInline(admin.TabularInline):

   model = TrialNumber

class TrialContactInline(admin.TabularInline):

   model = TrialContact

class TrialInstitutionInline(admin.TabularInline):

   model = TrialInstitution

class ClinicalTrialAdmin(admin.ModelAdmin):

   inlines = [SecondaryNumberInline, RecruitmentCountryInline,

              OutcomeInline, TrialContactInline, TrialInstitutionInline,

              DescriptorInline, TrialInterventionCodeInline]</pre>
<p>No código acima, cada nome de modelo aparece três vezes&#8230; (uma vez ao<br />
declarar a classe XXXInline, no atributo model do inline, e mais uma<br />
vez no atributo inlines).</p>
<p>Além disso, quase todas as declarações de inlines são idênticas&#8230; E o<br />
princípio DRY, como fica?</p>
<p>Felizmente Django é Python. Então aquilo pode ser reescrito de uma<br />
maneira mais legal assim:</p>
<p># <a href="http://pastebin.com/f5af2bd28" target="_blank">http://pastebin.com/f5af2bd28</a></p>
<pre>tabular_inline_models = [Descriptor, RecruitmentCountry, TrialInterventionCode,

                        TrialNumber, TrialContact, TrialInstitution]

tabular_inlines = []

for model in tabular_inline_models:

   cls_name = model.__name__+'Inline'

   cls = type(cls_name, (admin.TabularInline,), {'model':model})

   tabular_inlines.append(cls)

class OutcomeInline(admin.StackedInline):

   model = Outcome

class ClinicalTrialAdmin(admin.ModelAdmin):

   inlines = tabular_inlines + [OutcomeInline]</pre>
<p>O truque aqui foi usar a função type(), que pode ser usada para criar<br />
classes dinamicamente. Para isso a gente passa três argumentos para a<br />
função type():</p>
<p>- uma string que será o __name__ da classe<br />
- uma tupla de super-classes (importante: a vírgula dentro do<br />
parêntesis indica que é uma tupla de um elemento)<br />
- um dicionário com os atributos da classe (métodos e variáveis)</p>
<p>Não tente fazer isso em Java <img src='http://alexandre.cuboestudioweb.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>[ ]s<br />
Luciano</p>
]]></content:encoded>
			<wfw:commentRss>http://alexandre.cuboestudioweb.com/2009/10/um-pouco-de-metaprogramacao/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

