简体   繁体   中英

How to verify outputs of line code in a python function

I have test a function below and how can I verify the output of specific line in the function below.

def data():
   dev = Device(host="1.1.1.1", user="test", password="test123", port=22, 
   normalize=True)
   dev.open()
   resp = dev.rpc.get_interface_information(extensive=True)
   dev.close()

   for x in resp.findall(xxxxxxxxx): 
      if aaa
      else:

without function data() i can just type print(resp) and print(x) to check an verify the output of rpc send command or for loop above. But when i add the command inside the function im not able to do the same..in the function there is also few other line code of for loop and other rpc send command that i need to verify the output..Thus..how can i check specific output of code line inside a python function below....i have tried add data() and return below but it doesn't return anything

def data():
   dev = Device(host="1.1.1.1", user="test", password="test123", port=22, 
   normalize=True)
   dev.open()
   resp = dev.rpc.get_interface_information(extensive=True)
   return resp
   dev.close()

   for x in resp.findall(xxxxxxxxx): 
      if aaa
      else:
   return x 
data()

This is because i need to ensure the line code used in the function is correct and the output is verify ok. Please help and show me the correct way of doing it. Thank you.

Got error NameError: name 'request' is not defined

def data(request):
   dev = Device(host="1.1.1.1", user="test", password="test123", port=22, 
   normalize=True)
   dev.open()
   resp = dev.rpc.get_interface_information(extensive=True)
   print(resp)
   dev.close()

   for x in resp.findall(xxxxxxxxx): 
      if aaa
      else:
   return resp 
data(request)

Print statement should work inside a function. I am guessing you are putting print statement after return. In the code you have provided nothing after the return statement is going to be executed since the return statement will exit out of the function. Make sure you put return at the end when you are done with all the tasks you want your function to perform.

Also, you can only have one return statement in a function. If you want to return multiple variables put them in a list or use something like

return a, b

and then catch them like this

val_a, val_b = data()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM