Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 `在Python2`\u sre.sre\u模式上设置attr()``_Python 2.7_Setattr - Fatal编程技术网

Python 2.7 `在Python2`\u sre.sre\u模式上设置attr()``

Python 2.7 `在Python2`\u sre.sre\u模式上设置attr()``,python-2.7,setattr,Python 2.7,Setattr,我基本上是通过尝试向Python2re编译模式添加一个fullmatch方法来尝试py2/3兼容性 我已经了解了如何向现有实例添加属性。 我正在创建中给出的方法 示例代码 import re import six regex = re.compile('some pattern') # Better if we keep this same if six.PY2: def fullmatch(self_regex, string, flags=0): return

我基本上是通过尝试向Python2
re
编译模式添加一个
fullmatch
方法来尝试py2/3兼容性

我已经了解了如何向现有实例添加属性。 我正在创建中给出的方法

示例代码

import re
import six


regex = re.compile('some pattern')  # Better if we keep this same

if six.PY2:
    def fullmatch(self_regex, string, flags=0):
        return self_regex.match(string, flags=flags)

    regex = re.compile(r'(?:' + regex.pattern + r')\Z', flags=regex.flags)

    import types

    bound = types.MethodType(fullmatch, regex)
    # AttributeError here. None of the following three lines work
    regex.fullmatch = bound
    regex.__setattr__('fullmatch', bound)
    setattr(regex, 'fullmatch', bound)

这是行不通的——正则表达式对象是在C端创建的,它们不代表您的常规实例,因此您无法从Python中修改它们的签名。例如,如果您尝试扩展
\u sre.sre\u模式

import _sre

class CustomPattern(_sre.SRE_Pattern): pass
您将得到一个
AttributeError
抱怨
\u sre
模块中不存在这样的对象。如果你试图用以下方式欺骗它:

import re

tmp_pattern = re.compile("")
sre_pat = type(tmp_pattern)

class CustomPattern(sre_pat): pass
它会给你一个
类型错误
抱怨
\u sre.sre\u模式
(现在“临时”存在,因为它是临时创建的)不是可接受的基本类型

相反,您可以做的是围绕
re
模块创建一个完整的包装器(或者至少向其添加缺少的结构),并在Python方面处理Python版本差异,尽管我认为这不值得


另外,如果您没有在其他任何地方使用
six
,没有理由只检查您的Python版本就产生开销-您可以使用
sys.version\u info.major<3

这行不通-regex对象是在C端创建的,它们不代表您的常规实例,因此您无法从Python中修改它们的签名。例如,如果您尝试扩展
\u sre.sre\u模式

import _sre

class CustomPattern(_sre.SRE_Pattern): pass
您将得到一个
AttributeError
抱怨
\u sre
模块中不存在这样的对象。如果你试图用以下方式欺骗它:

import re

tmp_pattern = re.compile("")
sre_pat = type(tmp_pattern)

class CustomPattern(sre_pat): pass
它会给你一个
类型错误
抱怨
\u sre.sre\u模式
(现在“临时”存在,因为它是临时创建的)不是可接受的基本类型

相反,您可以做的是围绕
re
模块创建一个完整的包装器(或者至少向其添加缺少的结构),并在Python方面处理Python版本差异,尽管我认为这不值得

另外,如果你没有在其他任何地方使用
six
,那么没有理由只检查你的Python版本,你可以使用
sys.version\u info.major<3

查看与你想要的类似的东西--
\u sre.Pattern
fullmatch()
方法的弗兰肯斯坦混搭。这种猴子修补“继承”方法在Python2和Python3中工作

import re
import regex

class Pattern:
    """ "Inherits" _sre.SRE_Pattern and adds .fullmatch() method

    >>> pattern = Pattern('Aaron[ ]Swartz')
    >>> pattern.match('Aaron Swartz')
    <_sre.SRE_Match object; span=(0, 12), match='Aaron Swartz'>
    >>> pattern.fullmatch('Aaron Swartz!!')
    >>> pattern.match('Aaron Swartz!!')
    <_sre.SRE_Match object; span=(0, 12), match='Aaron Swartz'>
    """
    def __init__(self, pattern):
        self._compiled_pattern = re.compile(pattern)
        for name in dir(self._compiled_pattern):
            if not name in set(('__class__', '__init__', 'fullmatch')):
                attr = getattr(self._compiled_pattern, name)
                setattr(self, name, attr)

def fullmatch(self, *args, **kwargs):
    return regex.fullmatch(self._compiled_pattern.pattern, *args, **kwargs)
重新导入
导入正则表达式
班级模式:
“”继承“\u sre.sre\u模式并添加.fullmatch()方法
>>>图案=图案('Aaron[]Swartz')
>>>模式匹配('Aaron Swartz')
>>>pattern.fullmatch('Aaron Swartz!!')
>>>模式匹配('Aaron Swartz!!')
"""
定义初始(自我,模式):
自编译模式=重新编译(模式)
对于目录中的名称(自编译模式):
如果集合中没有名称(“\uuuuu class\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
attr=getattr(自编译模式,名称)
setattr(self、name、attr)
def完全匹配(自身、*args、**kwargs):
返回regex.fullmatch(自编译的模式,*args,**kwargs)
请参阅,以获得类似于您所需的内容的内容--使用
fullmatch()
方法的
\u sre.Pattern
的弗兰肯斯坦混搭。这种猴子修补“继承”方法在Python2和Python3中工作

import re
import regex

class Pattern:
    """ "Inherits" _sre.SRE_Pattern and adds .fullmatch() method

    >>> pattern = Pattern('Aaron[ ]Swartz')
    >>> pattern.match('Aaron Swartz')
    <_sre.SRE_Match object; span=(0, 12), match='Aaron Swartz'>
    >>> pattern.fullmatch('Aaron Swartz!!')
    >>> pattern.match('Aaron Swartz!!')
    <_sre.SRE_Match object; span=(0, 12), match='Aaron Swartz'>
    """
    def __init__(self, pattern):
        self._compiled_pattern = re.compile(pattern)
        for name in dir(self._compiled_pattern):
            if not name in set(('__class__', '__init__', 'fullmatch')):
                attr = getattr(self._compiled_pattern, name)
                setattr(self, name, attr)

def fullmatch(self, *args, **kwargs):
    return regex.fullmatch(self._compiled_pattern.pattern, *args, **kwargs)
重新导入
导入正则表达式
班级模式:
“”继承“\u sre.sre\u模式并添加.fullmatch()方法
>>>图案=图案('Aaron[]Swartz')
>>>模式匹配('Aaron Swartz')
>>>pattern.fullmatch('Aaron Swartz!!')
>>>模式匹配('Aaron Swartz!!')
"""
定义初始(自我,模式):
自编译模式=重新编译(模式)
对于目录中的名称(自编译模式):
如果集合中没有名称(“\uuuuu class\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
attr=getattr(自编译模式,名称)
setattr(self、name、attr)
def完全匹配(自身、*args、**kwargs):
返回regex.fullmatch(自编译的模式,*args,**kwargs)

@zwer你能回答一下吗。我现在做的是
re.fullmatch=fullmatch
,现在用
re.fullmatch(regex,string)
代替
regex.fullmatch(string)
。@zwer你能回答吗。我现在做的是
re.fullmatch=fullmatch
,现在用
re.fullmatch(regex,string)
代替
regex.fullmatch(string)