D&#39;oh.<div><br></div><div>Finally found how to do it. I can use the decorator to attach additional route-specific attributes to the function</div><div>and then the class instance can scan for it from the constructor, auto-registering each of its own methods</div>
<div>that have route info attached to them.</div><div><br>Sorry for previous question</div><div>Jacek</div><div><br><div class="gmail_quote">On Mon, Sep 5, 2011 at 12:14 PM, Jacek Furmankiewicz <span dir="ltr">&lt;<a href="mailto:jacek99@gmail.com">jacek99@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi Glyph,<div><br></div><div>I looked at your suggestion, but unfortunately the implementation is very complex, if not impossible.</div>
<div><br></div><div>The main problem is that</div><div>a) a class method with a decorator &quot;forgets&quot; its class, so it&#39;s impossible from the decorator which class it belongs to. </div>
<div>The function has not been bound to a class yet when the decorator is called for the first time, so there is no way for it to notify the containing class that this function defines a route for it</div><div><br></div>
<div>
b) is is next to impossible for a class to scan its own function and find their decorators. I&#39;ve seen some hacks on StackOverflow</div><div>where it actually parses the source code, but that is an ugly hack to say the least (and probably prone to many bugs)</div>

<div><br></div><div>In general, it seems decorators on class methods are missing such crucial functionality as finding out which class the method belongs to.</div><div>Sort of a key requirement, if you ask me (at least after lots of experience with Java or .Net reflection, where getting this sort of info is trivial).</div>

<div><br></div><div>if you have any suggestions on how to accomplish your recommendation, I would greatly appreciate it.</div><div><br></div><div>The decorator in question that I would need to take out of the CorePost class and make it a standalone function looks like this:</div>

<div><br></div><div><div>    def route(self,url,methods=(Http.GET,),accepts=MediaType.WILDCARD,produces=None,cache=True):</div><div>        &quot;&quot;&quot;Main decorator for registering REST functions &quot;&quot;&quot;</div>

<div>        def wrap(f,*args,**kwargs):</div><div>            self.__registerFunction(f, url, methods, accepts, produces,cache)</div><div>            return f</div><div>        return wrap</div></div><div><br></div><div>

it&#39;s obtaining the reference to &#39;self&#39; when it is not a class method any more is the problem. Not sure how to get around it.</div><div><br></div><div>Cheers,</div><div>Jacek<font color="#888888"><br><br></font><div class="gmail_quote">
<div><div></div><div class="h5">On Sun, Sep 4, 2011 at 12:01 AM, Glyph Lefkowitz <span dir="ltr">&lt;<a href="mailto:glyph@twistedmatrix.com" target="_blank">glyph@twistedmatrix.com</a>&gt;</span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div class="h5"><div style="word-wrap:break-word"><br><div><div>On Sep 3, 2011, at 8:28 PM, Jacek Furmankiewicz wrote:</div>
<br><blockquote type="cite">
<span style="border-collapse:separate;font-family:&#39;Bitstream Vera Sans Mono&#39;;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;font-size:medium"><div>

Any feedback is welcome</div></span></blockquote><br></div><div>Hi Jacek,</div><div><br></div><div>Great to see more development going into Twisted-based web stuff! :)</div><div><br></div><div>However, I do have one question.  Maybe I&#39;m missing something about the way Flask does things, but it seems very odd to me that the decorators you&#39;re using are applied to global functions, rather than instances of an object.  For example, instead of:</div>

<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>app = CorePost()</div><div>...</div><div><div>@app.route(&quot;/validate/&lt;int:rootId&gt;/schema&quot;,Http.POST)</div></div>
</div><div><div><div>@validate(schema=TestSchema)</div></div><div><div>def postValidateSchema(request,rootId,childId,**kwargs):</div></div><div><div>    &#39;&#39;&#39;Validate using a common schema&#39;&#39;&#39;</div>
</div><div><div>    return &quot;%s - %s - %s&quot; % (rootId,childId,kwargs)</div></div></div></blockquote><div><br>You could do:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>

class MyPost(CorePost):</div><div><div>    @route(&quot;/validate/&lt;int:rootId&gt;/schema&quot;,Http.POST)</div></div></div><div><div>    @validate(schema=TestSchema)</div></div><div><div>    def postValidateSchema(self,request,rootId,childId,**kwargs):</div>

</div><div><div><div>        &#39;&#39;&#39;Validate using a common schema&#39;&#39;&#39;</div></div><div><div>        return &quot;%s - %s - %s&quot; % (rootId,childId,kwargs)</div></div></div></blockquote><div>
<br>This would allow for re-usable objects; for example, rather than having a &quot;blog article create&quot; API (sorry for the uninspired example, it&#39;s late) for your entire site, you would have a &quot;article create&quot; API on a &quot;Blog&quot;, which would enable you to have multiple Blog objects (perhaps with different authors, in different permission domains, etc).  This would also make re-using the relevant objects between different applications easier.</div>

<div><br></div><div>In other words, global variables are bad, and this looks like it depends rather heavily on them.</div><div><br></div><div>Any thoughts on this?  Am I missing the point?</div><div><br></div><div>Thanks,</div>

<div><br></div><font color="#888888"><div>-glyph</div><div><br></div></font></div><br></div></div><div class="im">_______________________________________________<br>
Twisted-web mailing list<br>
<a href="mailto:Twisted-web@twistedmatrix.com" target="_blank">Twisted-web@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web</a><br>
<br></div></blockquote></div><br></div>
</blockquote></div><br></div>