Python中是否有预定义的URL类?(Is there predefined class for URL in Python?)

编程入门 行业动态 更新时间:2024-10-28 03:18:43
Python中是否有预定义的URL类?(Is there predefined class for URL in Python?)

我在python-modules,Django,Zope或Python中的任何地方寻找类似java.net.URL的东西。 我最好从语义的角度来理解它,因为有关程序的分析结果意味着URL在其中扮演着重要的角色。 其结果是这样的URL类在该程序中也将具有很大的实际用途。

当然,我可以自己写这样的课,但在开始重新发明之前,我想环顾四周。

我确实看过urllib2和urlparse 。 urlparse基本上具有我需要的功能,但它不会将其封装到像java.net.URL这样的类中。 关于我对我的计划的分析,它是颠倒的。

我还urlparse了SplitResult和ParseResult类的SplitResult源代码。 它们具有一些基本功能,可以用于子类化。 但是我必须将其余的urlparse函数重写为子类方法。

我还发现了mxURL - Python的灵活URL数据类型 。 这与我真正想要的非常接近。 只是对我的目的来说,这似乎太过于夸张了。

任何人都可以提出另一种选择 我应该继续重塑车轮吗?

我的解决方案

为了获得我的URL类,我基本做了两件事:

从urlparse.ResultMixin继承。 定义只调用urlparse.urlparse()并将结果转换为URL实例参数的函数。

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:

Inherit from urlparse.ResultMixin. Define function which only calls urlparse.urlparse() and transforms results to parameters of URL instance.

最满意答案

您可能需要考虑查看furl,因为它可能是您的需求的答案。

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

更多推荐

本文发布于:2023-07-29 21:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319763.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:URL   Python   class   predefined

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!