jeudi 27 avril 2017

Scope in python in nested if statements

I am writing a small piece of code which parses latex files and gives me the newcommands defined. A test case would be this simple latex file:

% +--------------------------------------------------------------------+
% |                                                                    |
% |  New particle stuff                                                |
% |                                                                    |
% +--------------------------------------------------------------------+
\newcommand*{\Hmp}{\ensuremath{H^{\mp}}\xspace}
\newcommand*{\susy}[1]{\ensuremath{\tilde{#1}}\xspace}
\newcommand*{\susy2}[1,2]{\ensuremath{\tilde{#1}\tilde{#2}}\xspace}

There might be a more complicated case where the command expands several lines so I need to keep track of different steps like if the command needs to be incremented with more lines or when the command has finished and is ready to be finished.

The thing is that, within a couple of nested if/else statements the scope of the variables seems lost and the variables are not updated anymore. Here is what am I am doing:

    macros = []
    for line in open(file).readlines():
        line = line.strip()
        keep = False
        newcommand = {"key":"","command":"","args":[]}
        added = False
        if "newcommand" in line:
            if line[-1] == "%":
                clean_line = line[0:-1]
                keep = True
                # Command with arguments
                warg = re.compile("newcommand\*\{(.*)\}\[(.*)\]\{(.*)")
                # Command without arguments
                woarg = re.compile("newcommand\*\{(.*)\}\{(.*)")
                newcommand = get_cmd_from_line(warg,woarg,clean_line)
            else:
                warg = re.compile("newcommand\*\{(.*)\}\[(.*)\]\{(.*)\}")
                # Command without arguments
                woarg = re.compile("newcommand\*\{(.*)\}\{(.*)\}")
                newcommand = get_cmd_from_line(warg, woarg, line)
                added = True
        elif keep:
            # Now it dos not matter how it ends, the command will always be added the line without the
            # last character, it can be either % or } but it shouldn't be added
            newcommand["command"] += line[0:-1]
            # End the keep
            if line[-1] != "%":
                keep = False
                added = True
        elif added:
            macros.append(newcommand)

The issue is when I assign the newcommand variable the value I get from the get_cmg_from_line function (which I have tested works perfectly) it doesn't update the newcommand variable but if I move it over the previous if then it recognizes it and updates it. The same thing happens with the keep and added variables.

I have searched for this and found a lot of things about scopes/if/functions etc. which I alrady knew and since ifs shouldn't define scope I don't know why this is happening... am I missing something stupid? How should I update the value of the newcommand variable? Since it might get updated with new lines coming. The only solution I see is to flatten the code but I would like to maintain it like this.

Aucun commentaire:

Enregistrer un commentaire