JMFcode
JMFcode

Reputation: 1

How to I exclude a certain value from the output of this code?

So my code is basically scanning a notepad .txt document and showing the output of all P.U. voltage values below 0.8 and returns a message of "faliure" if one is found. The problem is, there is at least one value in each file at exactly 0.0. This is a nuisance value that I would like to exclude. The output file is saying the file fails if it finds this value because it is below the 0.8 threshold, but this is a null value.

timeForVoltageRecovery = 11.0    
acceptableVoltagePU = 0.8

for channel in data:
        if float(channel['time']) > timeForVoltageRecovery:
            isVoltageGood = True
            keys = channel.keys()
            keys.sort()
            volts = {}
            for key in keys:
                words = key.split(" ")
                if words[0] == "VOLT":
                        volts[float(channel[key])] = key.strip()

            voltsKeys = volts.keys()
            voltsKeys.sort()
            for k in range(10):
                if float(channel[volts[voltsKeys[k]]]) < acceptableVoltagePU:
                    isVoltageGood = False

            if isVoltageGood:
                outfile.write("\nAt time %s, this scenario passed voltage recovery criteria of 0.8 PU!\n" % (channel['time']))
            else:
                outfile.write("\n\t***** BAD: At Time %s, this scenario failed voltage recovery criteria of .8 PU :{ *****\n\n" % (channel['time']))
                outfile.write("the lowest 10 voltages in order are:\n")
                for k in range(10):
                    outfile.write(  "\t" + volts[voltsKeys[k]] + " -> " + channel[volts[voltsKeys[k]]] + "\n" )               

            outfile.write("\n")
            break

Does anyone know how I can still scan values below 0.8, but exclude 0.0 in my scan?

thank you.

I tried to make a lower voltage threshold value of 0.01. From here you could do an "if" statement where the acceptablevoltagePU has to be between 0.8 and 0.01. Something like this could definitely get the job done. I am not exactly sure how to code this.

Upvotes: 0

Views: 78

Answers (2)

LHY
LHY

Reputation: 695

Adding on to Kundan Krishna's answer, you can do like

# your other code ...

voltsKeys = volts.keys()
voltsKeys.sort()
for k in range(10):
  currentVoltage = float(channel[volts[voltsKeys[k]]]) # read and cast only once
  if currentVoltage < acceptableVoltagePU and currentVoltage != 0.0:
    isVoltageGood = False

# your other code ...

Doing this way reduces the number of time you are reading from the various dictionary (channel, volt, voltKeys) as well as the casting being done.


Additionally, I see that your code does not really follow Python's naming conventions. In general, they should be lowercase words separated by underscore.

So for example,

Your code Python Convention
timeForVoltageRecovery time_for_voltage_recovery
acceptableVoltagePU acceptable_voltage_PU
isVoltageGood is_voltage_good

Upvotes: 1

Kundan Krishna
Kundan Krishna

Reputation: 1

I am on phone so code editor doesn't work. But what you can do if 0.0 is always a false flag just use this

if float(channel[volts[voltsKeys[k]]]) < acceptableVoltagePU and float(channel[volts[voltsKeys[k]]]) != 0.0 in your if statement.

Upvotes: 0

Related Questions