I need help understanding/modifying the code.

Hello, I’m using diagnostic software for my vehicle, which is written in Python. I’d like to understand why the software isn’t displaying the long version of the status text and what needs to be changed to make it work. I hope it’s okay to link the code directly: pyren3/mod_ecu_state.py · pyren3 · PyRen / pyren · GitLab

Here is an example of two states from a required XML file. Some states include Helps text. I would like the terminal to display the long status text “Vacuum pressure below Safety Threshold” instead of “STATUS1”.

      <State name="E105" agcdRef="ET194" codeMR="ET194">
        <Label codetext="56693" defaultText="SPEED LIMITER INHIBITED BY EVC"/>
        <Interpretation>
          <Correspondance value="0" codetext="16800" defaultText="INACTIVE"/>
          <Correspondance value="1" codetext="16801" defaultText="ACTIVE"/>
          <Correspondance value="2" codetext="16869" defaultText="YES"/>
          <Correspondance value="3" codetext="16870" defaultText="NO"/>
        </Interpretation>
      </State>
      <State name="E137" agcdRef="ET001_FF" codeMR="ET001">
        <Label codetext="56669" defaultText="BRAKE SERVO PRESSURE"/>
        <Interpretation>
          <Correspondance value="0" codetext="16800" defaultText="INACTIVE"/>
          <Correspondance value="1" codetext="16801" defaultText="ACTIVE"/>
          <Correspondance value="2" codetext="9422" defaultText="UNAVAILABLE"/>
          <Correspondance value="3" codetext="16851" defaultText="STATUS1"/>
          <Correspondance value="4" codetext="16852" defaultText="STATUS2"/>
          <Correspondance value="5" codetext="16853" defaultText="STATUS3"/>
        </Interpretation>
        <Helps>
          <Line>
            <Label codetext="16851" defaultText="STATUS1"/>
            <Label codetext="599" defaultText=" : "/>
            <Label codetext="57007" defaultText="Vacuum pressure below Safety Threshold"/>
          </Line>
          <Line>
            <Label codetext="16852" defaultText="STATUS2"/>
            <Label codetext="599" defaultText=" : "/>
            <Label codetext="57008" defaultText="Vacuum pressure between Safety Threshold and Pump On Threshold"/>
          </Line>
          <Line>
            <Label codetext="16853" defaultText="STATUS3"/>
            <Label codetext="599" defaultText=" : "/>
            <Label codetext="57009" defaultText="Vacuum pressure between Pump On Threshold and Pump Off Threshold"/>
          </Line>
        </Helps>
      </State>

The relevant part of the code is this I think:

    self.helps = []
    Helps = st.getElementsByTagName("Helps")
    if Helps:
      for hl in Helps:
        Lines = hl.getElementsByTagName("Line")
        if Lines:
          for ln in Lines:
            line = ""
            Label = ln.getElementsByTagName("Label")
            if Label:
              for la in Label:
                codetext = la.getAttribute("codetext")
                defaultText = la.getAttribute("defaultText")
                if codetext:
                  if codetext in list(tran.keys()):
                    line = line + tran[codetext]
                  elif defaultText:
                    line = line + defaultText
            self.helps.append(line+'\n')

This block parses the help section.

As I understand it, this should set the .helps attribute to ["STATUS1: Vacuum pressure below Safety Threshold\n", "STATUS2: Vacuum pressure between Safety Threshold and Pump On Threshold\n", "STATUS3: Vacuum pressure between Pump On Threshold and Pump Off Threshold\n] which is presumably later printed.

One extremely cursed way this could be broken is if the translation dictionary that is being used is broken. The code uses tran[codetext] if it exists, and defaulttext otherwise. If something set tran["599"] = "" and tran["57009"] = "", you would see only the status code without the explanation.
A way to test for this is to remove the tran lookup logic, and have the program always use the defaulttext. Another way to test for this is to print trans or part of trans (perhaps just f"{trans["599"]=}, {trans["57007"]=}) either to the terminal or to a file that you can read.

There are ways to make STATUS1: not print, but I’d recommend against it. Seeing STATUS1: Vacuum pressure below Safety Threshold is almost strictly more useful than seeing Vacuum pressure below Safety Threshold

1 Like

Hi Peter, thanks for your quick response. What needs to be changed so that the terminal displays STATUS1: Vacuum pressure below Safety Threshold or Vacuum pressure below Safety Threshold?

The longer I look at this program, the weirder it is.
There’s all kind of things here which don’t quite make sense. Like

if codetext in list(tran.keys()):
    itext = tran[codetext]
if codetext in tran.keys():
    itext = tran[codetext]

would be totally valid, and faster, and that’s hardly the most optimised version of this code.

There’s also inits that have variables passed in that are modified but not used, so that initialising the class changes the args the initializer was called with.


If it’s viable I would change your XML file.

change this

          <Correspondance value="3" codetext="16851" defaultText="STATUS1"/>
          <Correspondance value="4" codetext="16852" defaultText="STATUS2"/>
          <Correspondance value="5" codetext="16853" defaultText="STATUS3"/>

to

          <Correspondance value="3" codetext="16851" defaultText="STATUS1: Vacuum pressure below Safety Threshold"/>
          <Correspondance value="4" codetext="16852" defaultText="STATUS2: Vacuum pressure between Safety Threshold and Pump On Threshold"/>
          <Correspondance value="5" codetext="16853" defaultText="STATUS3: Vacuum pressure between Pump On Threshold and Pump Off Threshold"/>

There’s ways to automate it, but that’s likely not worth doing. All you need to do this task well is a text search tool and a good-enough understanding of the structure of the XML.


You’d want to make sure tran is not being used. Presuming you’ve downloaded the git repo and are running it from your laptop, you can modify the code to add some print(tran) statements. Or assert not tran. If tran is being used, track were it comes from and disable it at the source. Overwriting the XML in the manner I described won’t do anything as long as tran is being used.

Hi Peter, Before asking about a change here, I had started manually modifying the XML. But there are almost 1200 files. That’s why I was hoping there was an easier way to display the long version of the states.

Unfortunately, I am only a user of the software and do not understand the functionality of the code.

I’ve replaced the correspondence - codetext and defaulttext with the long version in some XML files. This way, the long version of all language files is displayed .

 <Correspondance value="3" codetext="16851" defaultText="STATUS1"/>
to
 <Correspondance value="3" codetext="57007" defaultText="Vacuum pressure below Safety Threshold"/>

I’ll probably have to invest many more hours in XML editing. I use the XML Copy Editor on Ubuntu, which also checks the XML form and converts the original 1-line XML into a readable structure .

I can see that would work. Might be the most robust approach.

I think you might well be able to write a python program to automate the process for you in the same amount of time that it would take you to do this by hand. (You can parse the help sections to build search-replace dictionaries, and then apply them to the files, or more tidily, create modified copies of the files.) The additional benefit is that after you’ve achieved that, you might be able to largely understand the original program.

But I can’t see a way to achieve what you want without spending a few hours at it.