Django: Ticket Queryhttps://code.djangoproject.com/query?status=!closed&reporter=fcurella&order=summary
The Web framework for perfectionists with deadlines.en-USDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/query?status=!closed&reporter=fcurella&order=summary
Trac 1.0.2https://code.djangoproject.com/ticket/20757
https://code.djangoproject.com/ticket/20757#20757: A more Object-Oriented URLResolverWed, 17 Jul 2013 17:58:09 GMTfcurella<p>
Currently, the <tt>RegexURLResolver</tt> is populated using tuples.
</p>
<p>
Because of that, code that tries to inspect the resolver will end up containg awkward indexing like this (semplified code for clarity's sake):
</p>
<pre class="wiki">def get_urlformat(urlname):
"""
Given a URL name, returns the URL as a string suitable for string.format.
Example::
urlpatterns = patterns('',
url(r'^extra/(?P&lt;extra&gt;\w+)/$', empty_view, name="named-url2"),
)
&gt;&gt;&gt; get_urlformat('named-url2')
'extra/%(extra)s/'
"""
resolver = get_resolver()
return resolver.reverse_dict[urlname][0][0][0]
</pre><p>
My proposal is to replace tuples with a tuple-like object whose elements can be accessed by using attribute names. That way, the above method could become:
</p>
<pre class="wiki">def get_urlformat(urlname):
"""
Given a URL name, returns the URL as a string suitable for string.format.
Example::
urlpatterns = patterns('',
url(r'^extra/(?P&lt;extra&gt;\w+)/$', empty_view, name="named-url2"),
)
&gt;&gt;&gt; get_urlformat('named-url2')
'extra/%(extra)s/'
"""
resolver = get_resolver()
urlbit = resolver.reverse_dict[urlname].urlbits[0]
return urlbit.format
</pre><p>
I realize this is mostly aesthetic, and there could be performance implications since the URLResolver is probably the most hit codepath, so I appreciate every kind of opinion.
</p>
<p>
The attached patch is still a draft, it definitely needs more extensive test coverage, but it's a start to get the idea.
</p>
Resultshttps://code.djangoproject.com/ticket/20757#changelog