I am looking for something like java.net.URL in python-modules, Django, Zope or wherever in Python. I want it preferably from the semantics reason, because the result of analysis of concerned program implies that the URL plays an essential role in it. The consequence is that such URL class also will have great practical usage in that program.
Of course I could write such class on my own, but I'd like to look around before I start to reinvent the wheel.
I did look at urllib2 and urlparse. The urlparse
basically has the functionality I need, but it doesn't encapsulate it into a class like java.net.URL
. Regarding my analysis of my program it works upside-down.
I looked also into the source code of urlparse
at the classes SplitResult
and ParseResult
. They have some basic functionality and they can be used for subclassing. But I'll have to rewrite rest of the urlparse functions as the subclass methods.
I found also mxURL - Flexible URL Datatype for Python. It is very close to what I really want. Only it seems to be quite an overkill for my purpose.
Can anyone suggest another option? Should I proceed with reinventing the wheel?
My solution:
To get my URL class I did basically two things:
urlparse.ResultMixin
. urlparse.urlparse()
and transforms results to
parameters of URL instance.urlparse
does encapsulate URLs into a class, called ParseResult
, so it can be considered a factory function for these. Straight from the Python docs:
>>> urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
params='', query='', fragment='')
If you desperately want a class called URL
to encapsulate your URLs, use an alias (URL = urlparse.ParseResult
) or create an adapter.