<?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>Ståle&#039;s weblog &#187; Decorators</title>
	<atom:link href="http://blog.staale.org/tag/decorators/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.staale.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 18 Oct 2010 07:43:50 +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>Python decorators</title>
		<link>http://blog.staale.org/2009/03/python-decorators.html</link>
		<comments>http://blog.staale.org/2009/03/python-decorators.html#comments</comments>
		<pubDate>Fri, 20 Mar 2009 15:08:15 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Decorators]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=48</guid>
		<description><![CDATA[How to create class decorators in Python that are relatively transparent as decorators for the outside world.]]></description>
			<content:encoded><![CDATA[<p>I have a project at work where in Python, where I use decorators to wrap functionality for views. I hit on a snag when I used 2 decorators on 1 function. I like using class decorators, I read some recommendations regarding that, and it fits better with my Java upbringing <img src='http://blog.staale.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . The 2 decorators I needed to apply to the same function looked like this (__call__ method omitted for brevity):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> AutoTemplate<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func
        pathElems = func.__module__.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;%s.html&quot;</span><span style="color: #66cc66;">%</span>func.__name__<span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">template</span> = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span> pathElems<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> AccessRequired<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func</pre></td></tr></table></div>

<p>In addition, I used the functions actual name in the urls.py file to generate the url tuple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> createUrl<span style="color: black;">&#40;</span>method<span style="color: black;">&#41;</span>:
    name = method.<span style="color: black;">func</span>.<span style="color: black;">func_name</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^%s.html$'</span><span style="color: #66cc66;">%</span>name, method, <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>, name<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>So the finished product after decorators where applied needed to work for that. Because the decorators resulted in a class rather than a function being assigned to the the view, this didn&#8217;t work properly.</p>
<p>With a little help from <a href="http://stackoverflow.com/questions/666216/decorator-classes-in-python">Stackoverflow</a> however, I came upon a good solution, that makes it really transparent that a function is actually decorated:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> DecoratorBase<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func
        <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">dir</span><span style="color: black;">&#40;</span>func<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> - <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">dir</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
            <span style="color: #008000;">setattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, n, <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>func, n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> AutoTemplate<span style="color: black;">&#40;</span>DecoratorBase<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
<span style="color: #ff7700;font-weight:bold;">class</span> AccessRequired<span style="color: black;">&#40;</span>DecoratorBase<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>This does not work for decorators requiring parameters, I will see if I can find a solution for that if I need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/03/python-decorators.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

