0

Whilst debugging a downloaded example script the following extract kept returning an error from the 3rd line which I could not resolve. I must confess I do not even understand how the construction between the curly brackets is supposed to work. Googling proved rather difficult.

    if_stats = psutil.net_if_stats()
    #remove loopback
    if_stats_filtered = {key: if_stats[key] for key, stat in if_stats.items() if "loopback" not in stat.flags}

if _stats contains the following:

{'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536), 'wlan0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1460), 'eth0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_HALF: 1>, speed=10, mtu=1500)}

Error generated is:

if_stats_filtered = {key: if_stats[key] for key, stat in if_stats.items() if "loopback" not in stat.flags}

AttributeError: 'snicstats' object has no attribute 'flags'

Can anyone shed some light on what is happening please?

Digging deeper I think I understand why I am getting the error:

AttributeError: 'snicstats' object has no attribute 'flags'

snicstats is a namedtuple which is defined in an imported file named _common.py which forms part of psutil. My version of that script defines snicstats as follows:

snicstats = namedtuple('snicstats',
                   ['isup', 'duplex', 'speed', 'mtu',])

It seems a newer version of _common.py at github/psutil

redefines snicstats as follows:

snicstats = namedtuple('snicstats',
                   ['isup', 'duplex', 'speed', 'mtu', 'flags'])

I tried to upgrade psutil to the newer version but my system keeps returning that i have the most recent version.

What can I do?

4
  • 1
    {} is a dictionary. No one can say what the "problem" is from the scant code fragments you posted but this seems to be a programming question, NOT Pi specific. It might be clear if you used print(if_stats)
    – Milliways
    Commented Aug 2 at 8:44
  • I am not sure if the full code will help but one can see it all at: github.com/rm-hull/luma.examples/tree/master/examples The script is called "sys_info_extended.py". the if_stats content that I reproduced is actually the output of print(if_stat). I understand that if_stats_filtered is yet another dictionary, but how is its content being build up?
    – xuraax
    Commented Aug 2 at 12:55
  • 1
    The construct is a "dictionary comprehension" - like a "list comprehension" but producing a dictionary - see stackoverflow.com/a/14507637/2836621 Commented Aug 2 at 13:41
  • NOTE the code fragment appears to be a complicated way of deleting a dictionary entry.
    – Milliways
    Commented Aug 2 at 23:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.