for loop is not printing all defined enum

45 views Asked by At

Defined an Enum, where I want to verify mapping of the key from db1 with db2. While iterating using for loop it doesn't print all the defined enums..

from enum import Enum

class SecData(Enum):
    secId = "sectoId"
    anteName = "anteName"
    rAzimuth = "rAzimuth"
    agl = "AboveGroundLevel"
    actualAzimuth = "AntennaHeight"
    actualLatitude = "SectorLat"
    technology = "technology"
    antennaType = "sectorAntennaType"
    actualElectricalTilt = "sectorElecTilt"
    actualMechanicalTilt = "sectorMechTilt"
    sectorNumber = "sectorNum"
    rtsTilt = "rtsTilt"
    band = "sectorBand"
    actualLongitude = "SectorLong"
    actualAntennaheight = "sectorAntennaHeight"
    siteReferenceId = "null"
    retiid4 = "null"
    retiid2 = "null"
    retiid3 = "null"
    NominalId = "null"
    retiid1 = "null"
    status = "null"



for sector in (SecData):
    print(sector.name, "-" , sector.value)

Output

secId - sectoId
anteName - anteName
rAzimuth - rAzimuth
agl - AboveGroundLevel
actualAzimuth - AntennaHeight
actualLatitude - SectorLat
technology - technology
antennaType - sectorAntennaType
actualElectricalTilt - sectorElecTilt
actualMechanicalTilt - sectorMechTilt
sectorNumber - sectorNum
rtsTilt - rtsTilt
band - sectorBand
actualLongitude - SectorLong
actualAntennaheight - sectorAntennaHeight
siteReferenceId - null

Can someone help to identify what's the issue with the script?

2

There are 2 answers

0
LF-DevJourney On

From the doc

Enum is a set of symbolic names (members) bound to unique values

The enum value should have different value. In you case you have more than one null value.

So other null value has no effect in your code.

    siteReferenceId = "null"
    retiid4 = "null"
    retiid2 = "null"
    retiid3 = "null"
    NominalId = "null"
    retiid1 = "null"
    status = "null"
0
9769953 On

All the members with a "null" value are aliases of each other.

As stated at the start of the documentation:

An enumeration:

  • [...]

  • can be iterated over to return its canonical (i.e. non-alias) members in definition order

So aliased members are essentially folded into one item for iteration purposes. Which is why you only see siteReferenceId being printed (the first "null" member).

If wanted, you can avoid aliases with the verify dectorator and the UNIQUE argument.