验证没有正则表达式的URL

时间:2017-04-09 15:50:01

标签: python wxpython

我需要帮助在我的应用程序中实现某种形式的验证。我有一个wx.TextCtrl控件,它接受来自用户的有效URL。我希望验证URL,然后再将其传递到Requests库和bs4库。

那么,这样做的方法是什么?如果可能的话,我想避免使用任何正则表达式。

我目前的实施如下......

"""
The module validatortest is intended for validating a URL given by the
user in a text control window/widget.
"""

import wx


class ValidateURL(wx.Validator):
    """
    Create the validator object.
    """

    def __init__(self):
        """
        Initialize the validator.
        """

        super().__init__()

    def Clone(self):
        """
        Clone the validator.
        """

        return ValidateURL()

    def Validate(self, window):
        """
        Do URL validation.
        """

        pass

        # First implementation
        #
        # textField = self.GetWindow()
        # textFieldValue = textField.GetValue()

        # Alternative?
        #
        # textField = window.Get()
        # textFieldValue = textField.GetValue()

        # Perhaps even this?
        #
        # textFieldValue = window.GetValue()
        # import validators
        # try:
        #     if validators.url(textFieldValue):
        #         return True
        # except ValidationFailure:
        #     wx.MessageBox(caption="Validation Failure",
        #                   message="Please input a valid URL address form.",
        #                   style=wx.OK | wx.ICON_ERROR)
        #     return False

    def TransferToWindow(self):
        """
        This method is called when the value associated with the
        validator must be transferred to the window/widget.
        """

        return True

    def TransferFromWindow(self):
        """
        This method is called when the value in the window/widget must
        be transferred to the validator.
        """

        return True

0 个答案:

没有答案