0

I'm trying to write a program in python that reads the values in a remote terminal unit (RTU) by snmp, using pysnmp.

The thing is: in the RTU every interface has 2 sensors: Temperature and Humidity.

I'm able to to the print but in different times. What I need to do is print both values in the same line.

This is what I have so far.

list = []

list1 = []

OID = "iso.3.6.1.4.1.3699.1.1.2.1.5.1.1.3."

UAD = "iso.3.6.1.4.1.3699.1.1.2.1.5.1.1.7."

for omega in range(1,33):
    UAD1 = UAD + str(omega)
    list.append(UAD1)

for altarf in range(1,33):
        OID1 = OID + str(altarf)
        list1.append(OID1)

# this value gives me the interface description
for auriga in lista1:
    sistema = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(( hostname, 161)),auriga
    )
    interfaz = sistema[3][0][1]
    print("Estado interfaz ") + str(interfaz)

# this value return the (temp / humidity) of sensors in  every interface
for Sculptor in lista:
    Sextans = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(( hostname, 161)),Sculptor
    )
    entrega = Sextans[3][0][1]
    entrega = float(entrega) * 0.1
    print "sensor " + str(entrega)
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
  • So you'd like the two print statements to be combined into one? –  Jul 06 '15 at 03:42
  • Yes thats what i need but those print came from 2 diferent loops as you can see – Eduardo Aranguiz Jul 06 '15 at 04:24
  • @Evert I have re-opened it. It was very unclear from the OP's question. @​Eduardo Please do edit your question to add the relevant details. Thank you. – Bhargav Rao Jul 06 '15 at 04:46
  • @BhargavRao That's what comments are for: ask for clarification, which is now enough for me to understand and answer the post. –  Jul 06 '15 at 04:49
  • Eduardo, a few tips: 1/ never use a builtin function for variable names. In this case, don't use `list`, but use for example `mylist`. Otherwise it'll bite you at some point. 2/ Use all English for variable names and comments; it mostly makes sense once other people look at your code, like here on StackOverflow, and English is still the de-facto programming language (of course, this practice goes together with good variable naming, so `mylist` would be an example of an English but otherwise bad variable name). Enjoy programming! –  Jul 06 '15 at 04:56
  • Since you're just starting out, I'd also recommend you use Python 3 instead. PySNMP claims to be Python 3 compatible, so I don't think you have a reason to use Python 2. –  Jul 06 '15 at 05:02

1 Answers1

1

You can, more or less, combine the two loops (and lists) using for example the zip function.

for auriga, Sculptor in zip(lista1, list):
    sistema = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(( hostname, 161)),auriga
    )
    interfaz = sistema[3][0][1]
    Sextans = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(( hostname, 161)),Sculptor
    )
    entrega = Sextans[3][0][1]
    entrega = float(entrega) * 0.1

    print "Estado interfaz " + str(interfaz),
    print "sensor " + str(entrega)

(Note the trailing comma after the first print statement. You can also combine it all in one print statement if you like:

    print "Estado interfaz " + str(interfaz) +  "sensor " + str(entrega)
  • Forgive me if I am too drunk. But I can count a missing parenthesis in the last print statement `print "Estado interfaz ... ` – Bhargav Rao Jul 06 '15 at 05:02
  • @BhargavRao Neat catch; fixed. (I've actually removed them; I felt there were, and still are, too many parentheses in the original). –  Jul 06 '15 at 05:09
  • I'd also advice you to introduce the OP to `str.format` which is the best way for the concatenation of strings as it avoids the unnecessary type conversion. – Bhargav Rao Jul 06 '15 at 05:10
  • I will if the OP asks what `str.format` is, but I figured I'd one thing at a time. I've already posted a few longish comments with tips under the original question; I might integrate those and the formatting into this answer at some point. –  Jul 06 '15 at 05:29