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.To check whether the string entered is a valid URL or not we use the validators module in Python. When we pass the string to the method url() present in the module it returns true(if the string is URL) and ValidationFailure(func=url, …) if URL is invalid.
Source code: Lib/urllib/parse.py. This module defines a standard interface to break Uniform Resource Locator (URL) strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a “relative URL” to an absolute URL given a “base URL.”
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.
You might want consider having a look at furl because it might be an answer to your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With