2

I am new to Python and trying things out with psutil. After I get the IO counters from the function , it says that it returns a tuple but then why the returned result looks more like a dictionary ? I can get the data extracted as tuples but whats with the "Keys " listed for each value in this tuple ? How do I extract them ? for example what if I want to extract the string "bytes_sent" from the tuple? Also is this really a Tuple? I can see data enclosed in ( ).

 psutil.net_io_counters()
    snetio(bytes_sent=201965381, bytes_recv=571417944, packets_sent=1972681, packets_recv=3515998, errin=0, errout=0, dropin=0, dropout=0)

type (psutil.net_io_counters())
psutil._common.snetio

print result[0]
202031899
Fenomatik
  • 457
  • 2
  • 8
  • 22
  • its a namedtuple (in python's collection lib), so use snetio.bytes_sent to get bytes sent – labheshr Nov 04 '15 at 04:26
  • @JTurk My original question was , how to get the Key value in this namedtuple ? for eg: "bytes_sent" . I can extract its value already. – Fenomatik Nov 04 '15 at 05:15
  • I was commenting on your question: "why the returned result looks more like a dictionary" and " but whats with the "Keys " listed for each value in this tuple"...to which the answer is collection's namedtuple which is what is given by another user below... – labheshr Nov 04 '15 at 17:13

1 Answers1

2

This will be a collections.namedtuple. You can interact with it either as tuple, or by using its attributes. Best of both worlds and all that.

donkopotamus
  • 22,114
  • 2
  • 48
  • 60