在Python中使用序列匹配器查找最长公共字符串

在Python中使用序列匹配器查找最长公共字符串,python,sequence,Python,Sequence,我试图在Python中使用difflib.SequenceMatcher返回最大的公共字符串 string1="""ERROR agave_util.py:64 Timed out waiting for HA alert generated CRITICAL ha_test_util.py:44 HA alert generated, Stack:File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 909, in &l

我试图在Python中使用
difflib.SequenceMatcher
返回最大的公共字符串

string1="""ERROR agave_util.py:64 Timed out waiting for HA alert generated CRITICAL ha_test_util.py:44 HA alert generated, Stack:File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 909, in <module>    main(FLAGS, sync_state)  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 878, in main    worker.run(sync_state)  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 326, in run    if not self.__test_phase_wrapper(test_method):  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 502, in __test_phase_wrapper    func()  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 87, in test_stargate_master_power_off    self._host_power_off_test_cycle(host_of_stargate_master)  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 27, in _host_power_off_test_cycle    self.ha_util.power_off_and_check_ha(host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 469, in power_off_and_check_ha    self.wait_for_ha_alert(cutoff_usecs=latest_alert_start, **kwargs)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 418, in wait_for_ha_alert    interval=interval,  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 44, in wait_for_true    CHECK(result, message) ERROR nutanix_test_runner_worker.py:595 Test failed: 1exc_type: <type 'exceptions.SystemExit'>exc_value: 1stack:   File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 502, in __test_phase_wrapper    func()  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 87, in test_stargate_master_power_off    self._host_power_off_test_cycle(host_of_stargate_master)  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 27, in _host_power_off_test_cycle    self.ha_util.power_off_and_check_ha(host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 469, in power_off_and_check_ha    self.wait_for_ha_alert(cutoff_usecs=latest_alert_start, **kwargs)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 418, in wait_for_ha_alert    interval=interval,  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 44, in wait_for_true    CHECK(result, message)  File "/main/.python/util/base/log.py", line 204, in CHECK    FATAL(log_msg, **kwargs)  File "/main/.python/util/base/log.py", line 185, in FATAL    sys.exit(1) ERROR nutanix_test.py:696 Failed to get gflags from 10.5.132.157. ERROR nutanix_test.py:696 Failed to get gflags from 10.5.132.157. ERROR nutanix_test.py:1699 Failed to save cluster configuration"""

string2="""ERROR agave_util.py:64 Timed out waiting for VMs [u'vm_353ca5', u'vm_e02d7f'] power on CRITICAL ha_test_util.py:44 VMs [u'vm_353ca5', u'vm_e02d7f'] power on, Stack:File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 909, in <module>    main(FLAGS, sync_state)  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 878, in main    worker.run(sync_state)  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 326, in run    if not self.__test_phase_wrapper(test_method):  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 502, in __test_phase_wrapper    func()  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 67, in test_zoo_keeper_leader_power_off    self._host_power_off_test_cycle(leader_host)  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 27, in _host_power_off_test_cycle    self.ha_util.power_off_and_check_ha(host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 468, in power_off_and_check_ha    self.verify_vms_not_on_host(host_vms, host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 617, in verify_vms_not_on_host    self.wait_for_vms_power_on(vm_names, per_vm_timeout)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 599, in wait_for_vms_power_on    interval=15)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 44, in wait_for_true    CHECK(result, message) ERROR nutanix_test_runner_worker.py:595 Test failed: 1exc_type: <type 'exceptions.SystemExit'>exc_value: 1stack:   File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 502, in __test_phase_wrapper    func()  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 67, in test_zoo_keeper_leader_power_off    self._host_power_off_test_cycle(leader_host)  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 27, in _host_power_off_test_cycle    self.ha_util.power_off_and_check_ha(host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 468, in power_off_and_check_ha    self.verify_vms_not_on_host(host_vms, host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 617, in verify_vms_not_on_host    self.wait_for_vms_power_on(vm_names, per_vm_timeout)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 599, in wait_for_vms_power_on    interval=15)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 44, in wait_for_true    CHECK(result, message)  File "/main/.python/util/base/log.py", line 204, in CHECK    FATAL(log_msg, **kwargs)  File "/main/.python/util/base/log.py", line 185, in FATAL    sys.exit(1) ERROR nutanix_test.py:696 Failed to get gflags from 10.5.132.156. ERROR nutanix_test.py:696 Failed to get gflags from 10.5.132.156. ERROR nutanix_test.py:1699 Failed to save cluster configuration"""

match = SequenceMatcher(None, string1, string2).find_longest_match(0, len(string1), 0, len(string2))
print match
print(string1[match.a: match.a + match.size])

string1="""ERROR agave_util.py:64 Timed out waiting for HA alert generated CRITICAL ha_test_util.py:44,"""
string2="""ERROR agave_util.py:64 Timed out waiting for VMs [u'vm_353ca5', u'vm_e02d7f'] power on CRITICAL ha_test_util.py:44"""
match = SequenceMatcher(None, string1, string2).find_longest_match(0,    len(string1), 0, len(string2))
print(string1[match.a: match.a + match.size])
string1=“”错误agave_util.py:64等待HA警报生成的关键HA_测试_util.py:44 HA警报生成,堆栈:文件“/main/qa/py/qa/agave/nutanix_测试_runner_worker.py”,第909行,主(标志,同步状态)文件“/main/qa/py/qa/agave/nutanix_test_runner_worker.py”,第878行,主worker.run(同步状态)文件”/main/qa/py/qa/agave/nutanix_-test_-runner_-worker.py”,第326行,运行中,如果不是自己的话。测试阶段包装(测试方法):文件“/main/qa/py/qa/agave/nutanix_-test_-runner_-worker.py”,第502行,在测试阶段包装函数()文件“/main/qa/test/agave/acropolis_-tests/ha/best-efforce_-off_-test.py”“,第87行,在test\u stargate\u master\u power\u off self.\u host\u power\u off\u test\u cycle(host\u of stargate\u master)文件/main/qa/test/agave/acropolis\u tests/ha/best\u efforce\u power\u off\u test.py”中,第27行,在测试周期self.ha\u-util.power\u off和检查\u-ha(host)文件/main/.py/py/qa/util/agave\u-tools/ha-test.py中,在power\u off\u和\u check\u self中。等待\u ha\u警报(截止\u usecs=最新警报\u开始,**kwargs)文件“/main/.python/qa/util/agave\u tools/ha\u test\u util.py”,第418行,等待\u ha\u警报间隔=间隔,文件“/main/.python/qa/util/agave\u tools/ha\u test\u util.py”,第44行,等待真正的检查(结果,消息)错误nutanix_test_runner_worker.py:595测试失败:1exc_type:exc_value:1stack:File“/main/qa/py/qa/agave/nutanix_test_runner_worker.py”,第502行,在测试阶段包装函数中“/main/qa/test/agave/acropolis_tests/ha/best-efforce_power_off_test_.py”,第87行,测试开始时主电源关闭(host_of_stargate_master)文件“/main/qa/test/agave/acropolis_tests/ha/best_power_off_test.py”,第27行,in_host_power_off_test_cycle self.ha_util.power_off_和_check_ha(host)文件“/main/.python/qa/util/agave_tools/ha_test.py”,第469行,在power_off和_check_self.wait等待_警报(cutoff\u usecs=latest\u alert\u start,**kwargs)文件“/main/.python/qa/util/agave\u tools/ha\u test\u util.py”,第418行,在等待\u alert interval=interval,文件“/main/.python/qa/util/agave\u tools/ha\u test util.py”,第44行,在等待真正的检查(结果、消息)文件“/main/.python/util/base/log.py”,第204行,在检查中(log_msg,**kwargs)File“/main/.python/util/base/log.py”,致命系统退出(1)错误nutanix_test.py:696未能从10.5.132.157获取gflags。错误nutanix_test.py:696未能从10.5.132.157获取gflags。错误nutanix_test.py:1699未能保存群集配置”“”
string2=“”错误agave_util.py:64等待vm[u'vm_353ca5',u'vm_e02d7f']打开关键ha_测试_util.py:44个vm[u'vm_353ca5',u'vm_e02d7f']打开电源,堆栈:文件“/main/qa/py/qa/agavave/nutanix_test_runner_worker.py”,第909行,主(标志,同步状态)文件“/main/qa/py/qa/agave/nutanix_test runner.py”,第878行,在main worker.run(sync_state)文件“/main/qa/py/qa/agave/nutanix_test_runner_worker.py”中,第326行,如果不是self,则运行。uuu test_phase_wrapper(test_方法):文件“/main/qa/py/qa/agave/nutanix_test_runner_worker.py”,第502行,在u test_uphase_uwrapper func()文件中“/main/qa/test/agave/acropolis\u tests/ha/best\u efforce\u power\u off\u test.py”,第67行,在test\u zoo\u keeper\u leader\u power\u off self.\u host\u power\u off\u test\u cycle(leader\u host)文件/main/qa/test/agavave/acropolis\u tests/ha/best\u power\u off\u tests.py,第27行,在主机\u power\u off\u test\u cycle\u self.ha和主机文件中”/main/.python/qa/util/agave_tools/ha_test_util.py”,第468行,在power_off_和check_self.verify_vms_not_on_host(host_vms,host)文件/main/.python/qa/util/agave_tools/ha_test_util.py,第617行,在verify_vms_not_on_host self中。等待_vms_power_on(vms_名称,每_vm/)超时)文件/main/.qa/util/agave_测试工具/python“,第599行,在等待检查(结果,消息)错误nutanix测试运行程序运行程序工作人员。py:595测试失败:1exc类型:exc值:1stack:File”/main/qa/py/qa/agave/nutanix测试运行程序工作人员。py”,第502行,在“测试阶段”包装函数()文件“/main/qa/test/agave/acropolis\u tests/ha/best\u efforce\u power\u off\u test.py”中,第67行,在“测试动物园”管理者\u leader\u power\u off self中。主机\u power\u test\u测试周期(leader\u host)文件“/main/qa/test/agave/acropolis\u tests/ha/best\u power\u off\u tests\u off\u tests.py”,第27行,在“主机关闭测试”循环self.ha\u util.power\u off\u和\u check\u ha(主机)文件“/main/.python/qa/util/agave\u tools/ha\u test\u util.py”中,第468行,在“关机”和“检查self”中。验证主机(主机、主机)文件“/main/.python/qa/util/agave\u tools/ha\u test\u util.py”,第617行,在主机自身上验证虚拟机。等待虚拟机开机(虚拟机名称,每虚拟机超时)文件“/main/.python/qa/util/agave\u tools/ha\u test\u util.py”,第599行,等待虚拟机开机时间间隔=15)文件“/main/.python/qa/util/agave\u tools/ha\u test\u util.py”,第44行,等待真实检查(结果、消息)文件“/main/.python/util/base/log.py”,第204行,在CHECK FATAL(log_msg,**kwargs)文件“/main/.python/util/base/log.py”,第185行,在FATAL sys.exit(1)中错误nutanix_test.py:696未能从10.5.132.156获取gflags。错误nutanix_test.py:696未能从10.5.132.156获取gflags。错误nutanix_test.py:1699未能保存群集配置“”
match=SequenceMatcher(无,string1,string2)。查找最长的匹配(0,len(string1),0,len(string2))
打印匹配
打印(string1[match.a:match.a+match.size])
string1=“”错误agave_util.py:64在等待HA警报生成关键HA_测试_util.py:44时超时。“”
string2=“”错误agave_util.py:64等待虚拟机[u'vm_353ca5',u'vm_e02d7f']上电关键ha_测试超时\u util.py:44”
from difflib import SequenceMatcher

string1 = """ERROR agave_util.py:64 Timed out waiting for HA alert generated CRITICAL ha_test_util.py:44 HA alert generated, Stack:File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 909, in <module>    main(FLAGS, sync_state)  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 878, in main    worker.run(sync_state)  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 326, in run    if not self.__test_phase_wrapper(test_method):  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 502, in __test_phase_wrapper    func()  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 87, in test_stargate_master_power_off    self._host_power_off_test_cycle(host_of_stargate_master)  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 27, in _host_power_off_test_cycle    self.ha_util.power_off_and_check_ha(host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 469, in power_off_and_check_ha    self.wait_for_ha_alert(cutoff_usecs=latest_alert_start, **kwargs)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 418, in wait_for_ha_alert    interval=interval,  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 44, in wait_for_true    CHECK(result, message) ERROR nutanix_test_runner_worker.py:595 Test failed: 1exc_type: <type 'exceptions.SystemExit'>exc_value: 1stack:   File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 502, in __test_phase_wrapper    func()  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 87, in test_stargate_master_power_off    self._host_power_off_test_cycle(host_of_stargate_master)  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 27, in _host_power_off_test_cycle    self.ha_util.power_off_and_check_ha(host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 469, in power_off_and_check_ha    self.wait_for_ha_alert(cutoff_usecs=latest_alert_start, **kwargs)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 418, in wait_for_ha_alert    interval=interval,  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 44, in wait_for_true    CHECK(result, message)  File "/main/.python/util/base/log.py", line 204, in CHECK    FATAL(log_msg, **kwargs)  File "/main/.python/util/base/log.py", line 185, in FATAL    sys.exit(1) ERROR nutanix_test.py:696 Failed to get gflags from 10.5.132.157. ERROR nutanix_test.py:696 Failed to get gflags from 10.5.132.157. ERROR nutanix_test.py:1699 Failed to save cluster configuration"""
string2 = """ERROR agave_util.py:64 Timed out waiting for VMs [u'vm_353ca5', u'vm_e02d7f'] power on CRITICAL ha_test_util.py:44 VMs [u'vm_353ca5', u'vm_e02d7f'] power on, Stack:File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 909, in <module>    main(FLAGS, sync_state)  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 878, in main    worker.run(sync_state)  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 326, in run    if not self.__test_phase_wrapper(test_method):  File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 502, in __test_phase_wrapper    func()  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 67, in test_zoo_keeper_leader_power_off    self._host_power_off_test_cycle(leader_host)  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 27, in _host_power_off_test_cycle    self.ha_util.power_off_and_check_ha(host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 468, in power_off_and_check_ha    self.verify_vms_not_on_host(host_vms, host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 617, in verify_vms_not_on_host    self.wait_for_vms_power_on(vm_names, per_vm_timeout)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 599, in wait_for_vms_power_on    interval=15)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 44, in wait_for_true    CHECK(result, message) ERROR nutanix_test_runner_worker.py:595 Test failed: 1exc_type: <type 'exceptions.SystemExit'>exc_value: 1stack:   File "/main/qa/py/qa/agave/nutanix_test_runner_worker.py", line 502, in __test_phase_wrapper    func()  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 67, in test_zoo_keeper_leader_power_off    self._host_power_off_test_cycle(leader_host)  File "/main/qa/test/agave/acropolis_tests/ha/best_effort_power_off_test.py", line 27, in _host_power_off_test_cycle    self.ha_util.power_off_and_check_ha(host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 468, in power_off_and_check_ha    self.verify_vms_not_on_host(host_vms, host)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 617, in verify_vms_not_on_host    self.wait_for_vms_power_on(vm_names, per_vm_timeout)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 599, in wait_for_vms_power_on    interval=15)  File "/main/.python/qa/util/agave_tools/ha_test_util.py", line 44, in wait_for_true    CHECK(result, message)  File "/main/.python/util/base/log.py", line 204, in CHECK    FATAL(log_msg, **kwargs)  File "/main/.python/util/base/log.py", line 185, in FATAL    sys.exit(1) ERROR nutanix_test.py:696 Failed to get gflags from 10.5.132.156. ERROR nutanix_test.py:696 Failed to get gflags from 10.5.132.156. ERROR nutanix_test.py:1699 Failed to save cluster configuration"""

match = SequenceMatcher(None, string1, string2, autojunk=False).find_longest_match(0, len(string1), 0, len(string2))
print(match)
Match(a=110, b=156, size=534)
>>> max(SequenceMatcher(None, string1, string2, autojunk=False).get_matching_blocks(),
...     key=lambda m: m.size)
Match(a=110, b=156, size=534)
>>> a = "aa:bb:cc" + ":"*200
>>> b = "aa:bb" + ":"*200
>>> SequenceMatcher(None, a, b).find_longest_match(0, len(a), 0, len(b))
Match(a=0, b=0, size=6)     # : is classified as junk
>>> SequenceMatcher(None, a, b, autojunk=False).find_longest_match(0, len(a), 0, len(b))
Match(a=8, b=5, size=200)   # : is NOT classified as junk