{"id":9,"date":"2006-01-07T01:39:57","date_gmt":"2006-01-07T01:29:22","guid":{"rendered":""},"modified":"2017-03-07T20:08:45","modified_gmt":"2017-03-07T20:08:45","slug":"of_urls_and_methods","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/port001\/2006\/01\/07\/of_urls_and_methods\/","title":{"rendered":"Of URLs and Methods"},"content":{"rendered":"<p>For a long time now I&#8217;ve been hacking around on a web framework I started from scratch (mainly because they&#8217;re fun to code), but recently I started from scratch again using all I&#8217;d learnt from trying things out in the earlier prototype and what I&#8217;d learnt from other frameworks. So suddenly it&#8217;s all got a bit serious and I&#8217;m slowly coming together with something that&#8217;s looking very promising and for which I have big ambitions for.<\/p>\n<p>Yeah, I know what you&#8217;re all thinking, but I think I understand the framework &#8220;scene&#8221; pretty well now and what it takes for a framework to stand out from the crowd. Time will tell \ud83d\ude09<\/p>\n<p>Anyway, today I&#8217;m going to show you how the URL to method resolver works and what you can do with it.<\/p>\n<p>First of all a little example:<\/p>\n<p>class MyTestController:<\/p>\n<p>    @public<br \/>\n    def index(self, a, b, c):<\/p>\n<p>        return &#8220;I like twigs&#8221;<\/p>\n<p>    def page1(self, a, b, c=&#8221;w00t&#8221;):<\/p>\n<p>        return &#8220;PHIL COLLINS IS GOD&#8221; # I actually got an anonymous letter in the post containing a badge with this on it! o_O<\/p>\n<p>OK, pretty basic stuff and you can probably tell already what the URLs are going to look like for such mappings:<\/p>\n<p>www.mysite.co.uk\/MyTest\/arg_a\/arg_b\/arg_c\/ maps to the index method<br \/>\nwww.mysite.co.uk\/MyTest\/page1\/arg_a\/arg_b\/arg_c\/ maps to the page1 method, so does<br \/>\nwww.mysite.co.uk\/MyTest\/page1\/arg_a\/arg_b\/<\/p>\n<p>Joy.<\/p>\n<p>Time for full complexity mode:<\/p>\n<p>class MyTestController:<\/p>\n<p>    @public<br \/>\n    def index(self, a, b, *varargs):<\/p>\n<p>        return &#8220;Hi mum!&#8221;<\/p>\n<p>    @public<br \/>\n    def page1(self, a, b, c=&#8221;t00t&#8221;, *varargs, **kwargs):<\/p>\n<p>        return &#8220;burzum&#8221;<\/p>\n<p>    @public<br \/>\n    @constraint(&#8216;a&#8217;, r&#8217;^[a-zA-Z]*$&#8217;)<br \/>\n    @constraint(&#8216;c&#8217;, r'(some|thing|other)&#8217;)<br \/>\n    def page2(self, a, b, c=&#8221;123&#8243;, **kwargs):<\/p>\n<p>        return &#8221;     &#8221;<\/p>\n<p>Look! you can accept variable arguments(an unknown number of) and keyword arguments(key\/value pairs); groovy.<\/p>\n<p>and some URL examples for those:<br \/>\nwww.mysite.co.uk\/MyTest\/arg_a\/arg_b\/on\/and\/on\/with\/ariston maps to the index method<br \/>\nwww.mysite.co.uk\/MyTest\/page1\/arg_a\/arg_b\/arg_c\/blah\/a_key\/a_value\/another_key\/another_value maps to the page1 method<\/p>\n<p>(I&#8217;ll come to page2 in just a moment)<\/p>\n<p>Well done if you noticed that for the page1 URL we&#8217;ll only ever have 1 or no variable in varargs as we need the unmapped URL parts (i.e not arg_a, b or c) to be a multiple of 2 so that we can satisfy the key\/value pair requirement of the **kwargs. So, chances are you probably wont want to use *varargs and **kwargs in the same method.<\/p>\n<p>page2&#8230;. I hope the decorator name (the @foo thing) and the regular expressions gave away what&#8217;s going on here. You can choose to constrain some of your arguments if you so whish, it&#8217;ll save you having to do some input validation at least.<\/p>\n<p>So there you have it, you can use Python to its full extent as you would in any normal method and the framework does the leg work for you. I _am_ aware that using regular expressions to constrain URL mappings is standard stuff for frameworks these days, yet I think my using them in decorators is far nicer then having a separate messy mappings file.<\/p>\n<p>Oh and by the way&#8230; using **kwargs effectively allows you to never create a URL containing ?foo=bar&amp;ugly=yes again! I&#8217;m not saying something like www.mysite.co.uk\/blog\/06\/01\/2006\/sessionid\/df7d7sa5hf7g\/size\/100\/ makes much sense to the user (maybe it&#8217;s less confusing for the average Joe?) but however you design your URLs is _totally_ up to you \ud83d\ude42<\/p>\n<p>p.s Please don&#8217;t kill me for any grammar\/spelling mistakes &#8212; I&#8217;ve literally been looking at code all day long (the C apps at work were coded by the devil himself, I&#8217;m positive)<\/p>\n<p>p.p.s b2evo&#8217;s <code> tags don't seem to work too great and I'm too tired to play around with it.<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a long time now I&#8217;ve been hacking around on a web framework I started from scratch (mainly because they&#8217;re fun to code), but recently I started from scratch again using all I&#8217;d learnt from trying things out in the earlier prototype and what I&#8217;d learnt from other frameworks. So suddenly it&#8217;s all got a &hellip; <a href=\"https:\/\/blogs.gentoo.org\/port001\/2006\/01\/07\/of_urls_and_methods\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Of URLs and Methods<\/span><\/a><\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,4],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/posts\/9"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":1,"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":16,"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/posts\/9\/revisions\/16"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/port001\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}