Logo Questions Linux Laravel Mysql Ubuntu Git Menu

AttributeError: 'str' object has no attribute 'get'



Having an error I'm not quite sure how to solve, have a nested dictionary where the last item does not contain the asked get and gives an error, Not sure how to fix it.

Very basic code calling on

  for keys in interfaces:
      counters = interfaces[keys].get(u'interfaceCounters', {})
      print keys, "inOctets:", counters.get(u'inOctets', {}), "outOctets:", counters.get(u'outOctets',  {})

Here is the output:

  Management1 inOctets: 0 outOctets: 0
  Ethernet2 inOctets: 561926946 outOctets: 49932066
  Ethernet3 inOctets: 561926408 outOctets: 49932764
  Ethernet1 inOctets: 319314970 outOctets: 264840242
  Ethernet6 inOctets: 561897402 outOctets: 49930884
  Ethernet7 inOctets: 561678430 outOctets: 50180896
  Ethernet4 inOctets: 561927603 outOctets: 49931337
  Ethernet5 inOctets: 561926383 outOctets: 49932935
  Vlan1 inOctets:
  Traceback (most recent call last):
    File "ex1.py", line 36, in <module>
      print keys, "inOctets:", counters.get(u'inOctets', '0'), "outOctets:", counters.get(u'outOctets', '0')
  AttributeError: 'str' object has no attribute 'get'

Heres a tail end of interfaces:

u'Management1': {u'autoNegotiate': u'off',
                    u'bandwidth': 1000000000,
                    u'burnedInAddress': u'52:54:00:01:02:d3',
                    u'description': u'',
                    u'duplex': u'duplexFull',
                    u'forwardingModel': u'routed',
                    u'hardware': u'ethernet',
                    u'interfaceAddress': [],
                    u'interfaceCounters': {u'inBroadcastPkts': 0,
                                           u'inDiscards': 0,
                                           u'inMulticastPkts': 0,
                                           u'inOctets': 0,
                                           u'inUcastPkts': 0,
                                           u'inputErrorsDetail': {u'alignmentErrors': 0,
                                                                  u'fcsErrors': 0,
                                                                  u'giantFrames': 0,
                                                                  u'runtFrames': 0,
                                                                  u'rxPause': 0,
                                                                  u'symbolErrors': 0},
                                           u'linkStatusChanges': 2,
                                           u'outBroadcastPkts': 0,
                                           u'outDiscards': 0,
                                           u'outMulticastPkts': 0,
                                           u'outOctets': 0,
                                           u'outUcastPkts': 0,
                                           u'outputErrorsDetail': {u'collisions': 0,
                                                                   u'deferredTransmissions': 0,
                                                                   u'lateCollisions': 0,
                                                                   u'txPause': 0},
                                           u'totalInErrors': 0,
                                           u'totalOutErrors': 0},
                    u'interfaceStatistics': {u'inBitsRate': 0.0,
                                             u'inPktsRate': 0.0,
                                             u'outBitsRate': 0.0,
                                             u'outPktsRate': 0.0,
                                             u'updateInterval': 300.0},
                    u'interfaceStatus': u'disabled',
                    u'lastStatusChangeTimestamp': 1245724638.6762877,
                    u'lineProtocolStatus': u'down',
                    u'mtu': 1500,
                    u'name': u'Management1',
                    u'physicalAddress': u'52:54:00:01:02:d3'},
 u'Vlan1': {u'bandwidth': 0,
              u'burnedInAddress': u'52:54:00:01:37:37',
              u'description': u'',
              u'forwardingModel': u'routed',
              u'hardware': u'vlan',
              u'interfaceAddress': [{u'broadcastAddress': u'',
                                     u'primaryIp': {u'address': u'',
                                                    u'maskLen': 24},
                                     u'secondaryIps': {}}],
              u'interfaceStatus': u'connected',
              u'lastStatusChangeTimestamp': 1250568992.891673,
              u'lineProtocolStatus': u'up',
              u'mtu': 1500,
              u'name': u'Vlan1',
              u'physicalAddress': u'52:54:00:01:37:37'}}
like image 449
Wilken Avatar asked Mar 18 '23 04:03


1 Answers

If some of your interfaceCounters keys reference a string instead of a nested dictionary, just use exception handling to ignore those:

for keys in interfaces:
    counters = interfaces[keys].get(u'interfaceCounters', {})
        print keys, "inOctets:", counters.get(u'inOctets', {}), "outOctets:", counters.get(u'outOctets',  {})
    except AttributeError:
        # counters is not a dictionary, ignore and move on

This is the ask forgiveness principle; if most of your entries do have the .get() method this is simply faster than the look before you leap principle, where you test if the method is available.

like image 197
Martijn Pieters Avatar answered Mar 19 '23 17:03

Martijn Pieters