Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
namedtuple help
#1
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')>]
Reply
#2
There is no other output from your script except lines 27 and 28, unless the except clauses get executed.
Reply
#3
thanks Larz60+

one oid's output is not as per expectation, working on this.
Reply
#4
and what is the purpose to create list of namedtuples and immediately to create a list of dicts?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,841 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  AttributeError: module 'collections' has no attribute 'namedtuple' epgs1975 2 10,320 May-04-2020, 08:10 PM
Last Post: epgs1975
  Newbie question to show nested namedtuple list zydjohn 4 4,901 Dec-19-2017, 12:55 PM
Last Post: mpd
  Newbie question for complicated namedtuple generation zydjohn 0 2,172 Dec-16-2017, 09:50 AM
Last Post: zydjohn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020