Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there predefined class for URL in Python?

Tags:

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:

  1. Inherit from urlparse.ResultMixin.
  2. Define function which only calls urlparse.urlparse() and transforms results to parameters of URL instance.
like image 328
sumid Avatar asked May 29 '11 20:05

sumid


People also ask

How do you validate a URL in Python?

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.

What is urllib Parse?

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.”


Video Answer


2 Answers

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.

like image 138
Fred Foo Avatar answered Sep 21 '22 05:09

Fred Foo


You might want consider having a look at furl because it might be an answer to your needs.

like image 44
neutrinus Avatar answered Sep 21 '22 05:09

neutrinus