<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 13, 2013 at 4:52 AM, Lakshmi Vyas <span dir="ltr"><<a href="mailto:lakshmi.vyas@gmail.com" target="_blank">lakshmi.vyas@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div bgcolor="#FFFFFF" text="#000000">Here is a place where I really craved for blocks and resorted to using a
context manager + decorators:<br>
<br>
<a href="https://github.com/gitbot/gitbot/blob/master/gitbot/lib/s3.py#L140-L169" target="_blank">https://github.com/gitbot/gitbot/blob/master/gitbot/lib/s3.py#L140-L169</a></div></blockquote><div><br></div><div style>That is a VERY interesting pattern: </div>
</div><br><pre style="margin-top:0px;margin-bottom:0px;padding:0px;border:0px;font-size:12px;font-family:Consolas,'Liberation Mono',Courier,monospace;color:rgb(51,51,51);line-height:18px"><div class="" id="LC140" style="margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)">
<span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">with</span> <span class="" style="margin:0px;padding:0px;border:0px">source</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">.</span><span class="" style="margin:0px;padding:0px;border:0px">walker</span> <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">as</span> <span class="" style="margin:0px;padding:0px;border:0px">walker</span><span class="" style="margin:0px;padding:0px;border:0px">:</span></div>
<div class="" id="LC141" style="margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)"> <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">def</span> <span class="" style="margin:0px;padding:0px;border:0px;color:rgb(153,0,0);font-weight:bold">ignore</span><span class="" style="margin:0px;padding:0px;border:0px">(</span><span class="" style="margin:0px;padding:0px;border:0px">name</span><span class="" style="margin:0px;padding:0px;border:0px">):</span></div>
<div class="" id="LC142" style="margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)"> <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">return</span> <span class="" style="margin:0px;padding:0px;border:0px">match_pattern</span><span class="" style="margin:0px;padding:0px;border:0px">(</span><span class="" style="margin:0px;padding:0px;border:0px">ignore_patterns</span><span class="" style="margin:0px;padding:0px;border:0px">,</span> <span class="" style="margin:0px;padding:0px;border:0px">name</span><span class="" style="margin:0px;padding:0px;border:0px">)</span></div>
<div class="" id="LC143" style="margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)"><br></div><div class="" id="LC144" style="margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)">
<span class="" style="margin:0px;padding:0px;border:0px">@walker.folder_visitor</span></div><div class="" id="LC145" style="margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)"> <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">def</span> <span class="" style="margin:0px;padding:0px;border:0px;color:rgb(153,0,0);font-weight:bold">visit_folder</span><span class="" style="margin:0px;padding:0px;border:0px">(</span><span class="" style="margin:0px;padding:0px;border:0px">folder</span><span class="" style="margin:0px;padding:0px;border:0px">):</span></div>
</pre><br clear="all"><div style>Make the context be the source of the decorators, and do the iteration on __exit__.</div><div style><br></div><div style>This could work for me, but you must admit it is very much twisting context managers arms to the extreme.</div>
<div style><br></div><div style><div class="" id="LC140" style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;line-height:18px;white-space:pre;margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)">
<span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">with</span> self.closure() <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">as</span> <span class="" style="margin:0px;padding:0px;border:0px">c</span><span class="" style="margin:0px;padding:0px;border:0px">:</span></div>
<div class="" id="LC141" style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;line-height:18px;white-space:pre;margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)">
<span class="" style="margin:0px;padding:0px;border:0px">@c</span><br></div><div class="" id="LC145" style="font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;line-height:18px;white-space:pre;margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)">
<span style="color:rgb(51,51,51)"> <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">def</span> </span><font color="#990000"><b>_</b></font><span class="" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">(</span><span class="" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">):
match_this()
match_that()</span></div><div><br></div><div style>I'd like the above to be something like (warning:new keyword ahead):</div><div style><br></div><div style><div class="" id="LC140" style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;line-height:18px;white-space:pre;margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)">
<span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">within</span> self.closure()<span class="" style="margin:0px;padding:0px;border:0px">:</span></div><div class="" id="LC141" style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;line-height:18px;white-space:pre;margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)">
match_this()<br></div><div class="" id="LC145" style="font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;line-height:18px;white-space:pre;margin:0px;padding:0px 0px 0px 10px;border:0px;background-color:rgb(255,255,204)">
<span class="" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px"> match_that()</span></div><div><span class="" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px"><br></span></div></div><div style>
A clean, anonymous block.</div><div style><br></div></div>-- <br><span style="color:rgb(0,102,0)">Juancarlo </span><b style="color:rgb(0,102,0)">Añez</b>
</div></div>