– Python Issue: Working with an Array key-value inside a While statement, is overwritting the content even if different names are detected

Question:

Explanation:

I’m trying to do a Progress bar, where you can control it by updating some values on an Excel, and it shows the changes live in a .svg file.

Issue:

I’ve a loop that can repeat itself sometimes, but the names of the Listed key-value inside the Array are changed between 2 options.

The issue is that, even if I’ve those 2 options separately, and even with the Print showing the values correctly, the data stored inside is in the same key-value as the 1st time the Loop entered the method.

Here is where I declare the ArrayList, among some others properties:

class RangeBar(GraphicsPort):
    _opacity = {
        "firstPhase": 1,
        "thirdPhase": 1
    }

Some code of the main() where the While statement is working:

        if (rb._empty["firstPhase"] == False):
            print("Start Phase 1 Loop")
            rb.partialPhase(False, "firstPhase")
            print("Exit Phase 1 Loop")
        if (rb._empty["fillPhase"] == False):
            print("Start Phase 2 Loop")
            rb.fillPhase(False, "fillPhase")
            print("Exit Phase 2 Loop")
        if (rb._empty["thirdPhase"] == False):
            print("Start Phase 3 Loop")
            rb.partialPhase(False, "thirdPhase")
            print("Exit Phase 3 Loop")

Code of the partialPhase method (the one where this happens):

def partialPhase(self, baseCellModified, phase):
    if (baseCellModified != False):
        self.setValue(baseCellModified)
        self._opacity[phase] = baseCellModified / 100
        self.sheet_action("update_sheet_data",cell="C3",number=100)
    if (phase == "firstPhase"):
        self.setValue(86.9)
    if (phase == "thirdPhase"):
        self.setValue(12.9)
    print("New opacity for 'phase':",phase,"from: 'self._opacity[phase]':", self._opacity[phase])
    self.sheet_action("opacity_update", opacity=str(self._opacity[phase]))
    self.sheet_action("update_sheet_data",cell="D3",number=self.getValue())
    svg = str(self.__utils)
    text_file = open("./1.svg", "w")
    text_file.write(svg)
    text_file.close()
    timeToAction = self.sheet_action("get_sheet_data", type="numb", cell="F3") * 60
    time.sleep(timeToAction)

    while self._opacity[phase] > 0:
        timeToAction = self.sheet_action("get_sheet_data", type="numb", cell="F3") * 60
        changeCell = self.sheet_action("get_sheet_data", type="numb", cell="E3")
        action = self.identifyAction("G3")
        if (action == "decrement"):
            self._opacity[phase] -= (changeCell / 8)
            print("'phase' is showing opacity of:", phase)
            print("'self._opacity[phase]' is showing:", self._opacity[phase])
        if (action == "increment"):
            self._opacity[phase] += (changeCell / 8)
        if self._opacity[phase] < 0:
            print("Entering opacity minor to 0")
            print("'phase' is showing opacity of:", phase)
            print("'self._opacity[phase]' is showing:", self._opacity[phase])
            self._opacity[phase] = 0
            self._empty[phase] = True
            if (phase == "thirdPhase"):
                self._critical = True 
        self.sheet_action("opacity_update", opacity=str(self._opacity[phase]))
        self.sheet_action("update_sheet_data",cell="D3",number=self.getValue())
        svg = str(self.__utils)
        text_file = open("./1.svg", "w")
        text_file.write(str(self.__utils))
        text_file.close()
        time.sleep(timeToAction)

.svg to start from:

enter image description here

some prints from 1st phase:

enter image description here

After setting it to 0

enter image description here

After exiting Phase 2 (fill empty)

enter image description here

Now it happens the error, the Opacity of "firstPhase" turned 1, and it looks like this:

enter image description here

And now the code interacts with the ‘firstPhase’ all time, even if ‘thirdPhase’ appears in the prints, as the ‘phase’ it’s getting this value.

enter image description here

Until it disappears again:

enter image description here

Thanks for take your time to read me, and sorry for the large text .

Asked By: Last

||

Answers:

At the end of the while loop declare _opacity = {}, we need to make that empty, else for each run it adds the value in it.

I did a Newbie miss that I didn’t even noticed.

The code I passed was good, but a method I was calling, was hardcoded from previous tests, and I didn’t noticed it.

The phase which I wanted to change, was "thirdPhase" all the time, so it was constantly changing the opacity of the thirdPhase, even if I passed a different opacity, the phase remained the same.

I’m sorry if I wasted someone time on this and thanks for the attention.

Answered By: Last