来自条件的矛盾控制台消息和其他奇怪的行为

Contradictory console messages and other weird behavouirs from conditionals

提问人:Don Cheadle 提问时间:11/4/2023 最后编辑:Don Cheadle 更新时间:11/5/2023 访问量:62

问:

我的 Notepad++ 脚本遍历每个文件,并检查可用于相应地调整文件的可能正则表达式模式。

# encoding=utf-8
from Npp import *
import re
import os

history_files = 'C:/Program Files (x86)/Steam/steamapps/common/Europa Universalis IV/history/countries'
base_tech = 3

def replace(percentage):
    amount = float((100 - percentage) * 0.01)
    editor.rereplace(r'(?<=adm_tech = )(\d+)|(?<=dip_tech = )(\d+)|(?<=mil_tech = )(\d+)', str(int(round(base_tech * ((((float(value.group(0)) / base_tech) - 1) * amount) + 1)))));
    notepad.save()

def adjust():
    notepad.open(history_files+'\\'+country_file)
    colonial = re.search(r'(?<=.)(is_former_colonial_nation = yes)|(?<=.)(is_colonial_nation = yes)', editor.getText())
    tech_group = re.search(r'(?<=technology_group = )(\w+)', editor.getText())
    notepad.close()
    
    if tech_group.group(0) == 'western' or colonial:
        notepad.close()
        console.write('\nClosed: "' + filename + '" because of "western" or "colonial"')
    elif tech_group.group(0) == 'eastern':
        replace(20)

for (filename, bufferID, index, view) in notepad.getFiles():  
    notepad.activateBufferID(bufferID)

    pattern = re.compile(r'(?<=adm_tech = )(\d+)|(?<=dip_tech = )(\d+)|(?<=mil_tech = )(\d+)')
    value = pattern.search(editor.getText())

    mission_tag = re.search(r'\s*potential = \{[\s\S]*(?<=tag = )(\w+)', editor.getText())
    religion_group = re.search(r'\s*potential = \{[\s\S]*(?<=religion_group = )(\w+)', editor.getText())
    
    for country_file in os.listdir(history_files):
        if mission_tag and re.match(mission_tag.group(1), country_file):
            adjust()
            console.write('\nMatched mission_tag and country_file for: "' + filename + '"')
    if religion_group and not mission_tag:
        if religion_group.group(1) == 'christian':
            notepad.close()
            console.write('\nClosed: "' + filename + '" because of "christian"')
        elif religion_group.group(1) == 'dharmic':
            replace(40)
    elif mission_tag:
            console.write('\nCould not match "' + filename + '" to any country_file')   
    elif not religion_group and not mission_tag:
        console.write('\nNo identifiers found in: ' + filename)

在其他奇怪的行为中,为什么我在控制台中收到这样的矛盾消息?

Closed: "C:\Users\JB452503\Documents\files test\missions\DOM_French_Missions.txt" because of "western" or "colonial"
Matched mission_tag and country_file for: "C:\Users\JB452503\Documents\files test\missions\DOM_French_Missions.txt"
Could not match "C:\Users\JB452503\Documents\files test\missions\DOM_French_Missions.txt" to any country_file

如果它只是关闭文件,它如何匹配文件的内容?它怎么可能有匹配,也没有匹配?

每次出现竞争条件后,我都尝试过,但没有任何作用。我几乎可以肯定我的逻辑有问题。time.sleep(0.5)notepad.close()

python-2.7 条件语句 记事本++

评论

0赞 Ulrich Eckhardt 11/4/2023
有一件事可能会引起麻烦:行的开头有行尾。试着把 放在最后。\n
0赞 Toto 11/4/2023
消息从何而来?Matched mission_tag and country_file for:
0赞 AdrianHHH 11/4/2023
文件 npppythonscript.sourceforge.net/docs/latest/notepad.html 显示“关闭当前活动的文档”。这意味着任何其他文档都保持打开状态。因此,Python 代码将继续在另一个缓冲区上执行。您要做什么?也许调用不同的例程。Notepad.close()Notepad.close()
0赞 Don Cheadle 11/4/2023
@UlrichEckhardt感谢您的建议,但它似乎仍然不起作用。
0赞 Don Cheadle 11/4/2023
@Toto 很抱歉 - 我一定是在简化此处的脚本时不小心删除了它。现在又加回来了。

答: 暂无答案