Python Forum

Full Version: namedtuple help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

something is going wrong and not able to understand. below code is polling lag interface details. I am not able to print using namedtuple.

tLagOperationEntry_list = []
for host in my_devices:
    try:
        session = Session(hostname=host, community='cacti', version=2)
    except easysnmp.EasySNMPConnectionError as e:
        print('Error:- {}'.format(e))
        sys.exit(1)
    try:
        tLagIfIndex,tLagConfigLastChange,tLagLACPPrimaryPort\
        = (list(session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.15.3.1.'+c, \
                                               non_repeaters=0, max_repetitions=50))for c in ('1','2','5'))
    except easysnmp.EasySNMPNoSuchInstanceError as e:
        print('Error:- {}'.format(e))

    finally:

        tLagOperationEntry = namedtuple('tLagOperationEntry','LagIfIndex LagConfigLastChange LagLACPPrimaryPort')
        LagOperationEntry_inters = [tLagOperationEntry(*uple) for uple in zip(*[tLagIfIndex,tLagConfigLastChange,tLagLACPPrimaryPort])]

    for OperationEntry in LagOperationEntry_inters:
        tLagOperationEntry = {}
        tLagOperationEntry['host'] = host
        tLagOperationEntry['interface_index'] = OperationEntry.LagIfIndex.value
        tLagOperationEntry['LagConfigLastChange'] = OperationEntry.LagConfigLastChange.value
        tLagOperationEntry['LagLACPPrimaryPort'] = OperationEntry.LagLACPPrimaryPort.value
        tLagOperationEntry_list.append(tLags)
for data in tLagOperationEntry_list:
     print(data['host'],data['interface_index'],data['LagConfigLastChange'],data['LagLACPPrimaryPort'])
this code showing blank output, however I am able to print individual lists.

Output:
[<SNMPVariable value='1342177330' (oid='enterprises.6527.3.1.2.15.3.1.1.50', oid_index='', snmp_type='INTEGER')>, <SNMPVariable value='1342177331' (oid='enterprises.6527.3.1.2.15.3.1.1.51', oid_index='', snmp_type='INTEGER')>, <SNMPVariable value='1342177332' (oid='enterprises.6527.3.1.2.15.3.1.1.52', oid_index='', snmp_type='INTEGER')>]
There is no other output from your script except lines 27 and 28, unless the except clauses get executed.
thanks Larz60+

one oid's output is not as per expectation, working on this.
and what is the purpose to create list of namedtuples and immediately to create a list of dicts?
Hi Buran,

is there another way? I am following below post, assigning value to variable before print.
https://python-forum.io/Thread-how-to-co...5#pid41095

regards
Anna
(Oct-19-2019, 04:51 AM)anna Wrote: [ -> ]is there another way? I am following below post, assigning value to variable before print.
https://python-forum.io/Thread-how-to-co...5#pid41095
well, exactly what I mean - use directly the namedtuple in the print, without converting to dict