Parsing Print output from 4 data source - python

EDIT:
Sorry for the delay.... This is still giving me issues -
Traceback (most recent call last):
File "snmp1.py", line 18, in <module>
print_snmp_event(varBindTableRow)
File "snmp1.py", line 7, in print_snmp_event
print '{1} = {3}'.format(*data)
IndexError: tuple index out of range
That is the output I get when I try and run your latest suggestion.
I am having issues printing this correctly I am running SNMP on a router and I am getting the following as an output on print. I am using pysnmp to send this data to the router.
from pysnmp.entity.rfc3413.oneliner import cmdgen
cmdGen = cmdgen.CommandGenerator()
varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData('STRING'),
cmdgen.UdpTransportTarget(('router', 161)),
'1.3.6.1.2.1.2.2.1.2','1.3.6.1.2.1.2.2.1.14'
)
for varBindTableRow in varBindTable:
print varBindTableRow
My output is
[(ObjectName(1.3.6.1.2.1.2.2.1.2.222), OctetString('Gi4/2')), (ObjectName(1.3.6.1.2.1.2.2.1.15.215), Counter32(0))]
What I want to get is
Gi4/2 = 0
I would think i could printout 1,3 but its not working. Anyone have any tips?
Sorry here is the output:
print type(varBindTableRow[0])
TypeError: 'NoneType' object has no attribute '__getitem__'
print type(varBindTableRow[0][0])
TypeError: 'NoneType' object has no attribute '__getitem__'
print repr(varBindTableRow)
[[(ObjectName(1.3.6.1.2.1.2.2.1.2.1), OctetString('FastEthernet0/0')), (ObjectName(1.3.6.1.2.1.2.2.1.14.1), Counter32(0))],
print repr(varBindTableRow[0])
TypeError: 'NoneType' object has no attribute '__getitem__'
print repr(varBindTableRow[0][0])
TypeError: 'NoneType' object has no attribute '__getitem__'
When I run the script you gave, I get the following:
Traceback (most recent call last):
File "snmp_pull.py", line 18, in <module>
print_snmp_event(varBindTableRow)
File "snmp_pull.py", line 7, in print_snmp_event
print '{1} = {3}'.format(*data)
IndexError: tuple index out of range
Thanks again for all the help!

I'm assuming that the SNMP event stuff is a string that your program is trying to parse. If so, you can extract what you want use a regex pattern, like this.
#! /usr/bin/env python
import re
snmp_event = "[(ObjectName(1.3.6.1.2.1.2.2.1.2.222), OctetString('Gi4/2')), (ObjectName(1.3.6.1.2.1.2.2.1.15.215), Counter32(0))]"
print snmp_event
pat = re.compile(r'\w+\((.*?)\)')
data = pat.findall(snmp_event)
print '{1} = {3}'.format(*data)
Output:
[(ObjectName(1.3.6.1.2.1.2.2.1.2.222), OctetString('Gi4/2')), (ObjectName(1.3.6.1.2.1.2.2.1.15.215), Counter32(0))]
'Gi4/2' = 0
Edit
Aladine, I asked you to paste that info into your original question because it's very hard to read stuff like that in comments.
But I realise now that I made a bit of a mistake: I should've asked you to do this:
print type(varBindTableRow[0])
print type(varBindTableRow[0][0])
print repr(varBindTableRow)
print repr(varBindTableRow[0])
print repr(varBindTableRow[0][0])
which would have to go inside your for loop .
Sorry about that. I did get a little bit of info from what you put into the comments. So try this. I can't guarantee that it'll work, and I can't test it because I don't have the SNMP stuff.
import re
from pysnmp.entity.rfc3413.oneliner import cmdgen
def print_snmp_event(row):
pat = re.compile(r'\w+\((.*?)\)')
data = pat.findall(str(row))
print '{1} = {3}'.format(*data)
cmdGen = cmdgen.CommandGenerator()
varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData('STRING'),
cmdgen.UdpTransportTarget(('router', 161)),
'1.3.6.1.2.1.2.2.1.2','1.3.6.1.2.1.2.2.1.14'
)
for varBindTableRow in varBindTable:
print_snmp_event(varBindTableRow)
Please let me know if it works, and if it doesn't work please paste the results into your question (with indenting to preserve the lines, just like you paste code), not into a comment.

Related

Stuck with pytube

Oh my python guys, please help me.
One of my python projects suddenly stopped working for no reason.
I'm using pytube module and when i try to run the code i get this error:
Traceback (most recent call last):
File "C:\Users\giova\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\contrib\search.py", line 94, in fetch_and_parse
sections = raw_results['contents']['twoColumnSearchResultsRenderer'][
KeyError: 'twoColumnSearchResultsRenderer' fetch_and_parse
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\giova\OneDrive\Desktop\Coding\Python\youtubeapp.py", line 38, in <module>
videoSearch()
File "C:\Users\giova\OneDrive\Desktop\Coding\Python\youtubeapp.py", line 21, in videoSearch
availableResults = len(vid.results)
File "C:\Users\giova\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\contrib\search.py", line 62, in results
videos, continuation = self.fetch_and_parse() results
File "C:\Users\giova\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\contrib\search.py", line 97, in fetch_and_parse fetch_and_parse
sections = raw_results['onResponseReceivedCommands'][0][
KeyError: 'onResponseReceivedCommands'
This is not even the only error i get, sometimes i got "http error 410: gone" error or some like this. I haven't changed the code for about two weeks (it was working two weeks ago) and it stopped working. I don't know what is happening to my code.
This is the full code:
from pytube import Search, YouTube
print("================================\n What do you want to do?: ")
availableChoose = [
'1 Search videos',
'...',
'================================'
]
for choose in availableChoose:
print(choose)
userChoose = input()
userChoose = userChoose.lower()
def videoSearch():
userSearch = input("Enter the title of the video you want to search: ")
vid = Search(userSearch)
availableResults = len(vid.results)
strAvailableResults = str(availableResults)
print("The available results are " + strAvailableResults)
vidResultsList = vid.results
vidResultsList = str(vidResultsList)
vidResultsList = vidResultsList.replace("<pytube.__main__.YouTube object: videoId=", "")
vidResultsList = vidResultsList.replace(">", "")
vidResultsList = vidResultsList.replace("[", "")
vidResultsList = vidResultsList.replace("]", "")
vidResultsList = vidResultsList.replace(" ", "")
vidResultsList = vidResultsList.split(',')
for vidResultsObject in vidResultsList:
vidLink = ("https://www.youtube.com/watch?v=" + vidResultsObject)
vidTempObject = YouTube(vidLink)
print(vidTempObject.title + " - " + vidLink)
if(userChoose == "search" or userChoose == "search video" or userChoose == "search videos" or userChoose == "1"):
videoSearch()
pytube's 11.0.0 API docs list the Search.fetch_and_parse() method.
The corresponding source-code shows that it internally handles a KeyError for onResponseReceivedCommands:
# Initial result is handled by try block, continuations by except block
try:
sections = raw_results['contents']['twoColumnSearchResultsRenderer'][
'primaryContents']['sectionListRenderer']['contents']
except KeyError:
sections = raw_results['onResponseReceivedCommands'][0][
'appendContinuationItemsAction']['continuationItems']
This method is an inner one, that is used by your vid.results call.
Might be, that the Youtube API has changed there response and your version of pytube is not fitting anymore.
Bug already filed
See pytube issue #1082 and issue #1106.
Meanwhile use another branch
tfdahlin's fork has a bugfixed version. It was already proposed as Pull-Request to the project:
PR #1090, opened on 2021-08-13 (currently waiting for approval).
I am also struggling to get around the search results provided by pytube Search module. Since search results are looking like objects, I was thinking (lazily) that I cannot convert the object list in to strings.
After modifying your function as below, the results are printed as youtube links.
'''
def videoSearch(search_list): #provide the search.results list as input
for result in search_list: #Begin video id extraction
result = str(result)
temp = result.replace("<pytube.__main__.YouTube object: videoId=", "")
temp = temp.split(',')[0] #get the correct video id
vidLink = ("https://www.youtube.com/watch?v=" + temp)
print(vidLink)
'''
Try it out.

Python TypeError: '_sre.SRE_Match' object has no attribute '__getitem__'

Im brand new to python and coding, im trying to get below working.
this is test code and if I can get this working I should be able to build on it.
Like I said im new to this so sorry if its a silly mistake.
# coding=utf-8
import ops # Import the OPS module.
import sys # Import the sys module.
import re
# Subscription processing function
def ops_condition (o):
enter code herestatus, err_str = o.timer.relative("tag",10)
return status
def ops_execute (o):
handle, err_desp = o.cli.open()
print("OPS opens the process of command:",err_desp)
result, n11, n21 = o.cli.execute(handle,"return")
result, n11, n21 = o.cli.execute(handle,"display interface brief | include Ethernet0/0/1")
match = re.search(r"Ethernet0/0/1\s*(\S+)\s*", result)
if not match:
print("Could not determine the state.")
return 0
physical_state = match[1] # Gets the first group from the match.
print (physical_state)
if physical_state == "down":
print("down")
result = o.cli.close(handle)
else :
print("up")
return 0
Error
<setup>('OPS opens the process of command:', 'success')
Oct 17 2018 11:53:39+00:00 setup %%01OPSA/3/OPS_RESULT_EXCEPTION(l)[4]:Script is test.py, current event is tag, instance is 1515334652, exception reason is Trac eback (most recent call last):
File ".lib/frame.py", line 114, in <module>
ret = m.ops_execute(o)
File "flash:$_user/test.py", line 22, in ops_execute
physical_state = match[1] # Gets the first group from the match.
TypeError: '_sre.SRE_Match' object has no attribute '__getitem__'
The __getitem__ method for the regex match objects was only added since Python 3.6. If you're using an earlier version, you can use the group method instead.
Change:
physical_state = match[1]
to:
physical_state = match.group(1)
Please refer to the documentation for details.

Interpolating R objects into R code strings

I am using rpy2 to run some R commands. Dont ask why. It's necessary at this moment. So here's a part of the code.
import pandas.rpy.common as com
from rpy2.robjects import r
#Load emotionsCART decision tree. Successful.
r_dataframe = com.convert_to_r_dataframe(data)
print type(r_dataframe)
(<class 'rpy2.robjects.vectors.DataFrame'>)
r('pred = predict(emotionsCART, newdata = %s)') %(r_dataframe)
Here what I want to do is pass this r_dataframe into the calculation. I'm using the decision tree that I'd loaded earlier to predict the values. But the last line gives me an error. It says
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
r('pred = predict(emotionsCART, newdata = %s)') %(r_dataframe)
File "C:\Python27\lib\site-packages\rpy2\robjects\__init__.py", line 245, in __call__
p = rinterface.parse(string)
ValueError: Error while parsing the string.
Ideas why this is happening?
I think that:
r('pred = predict(emotionsCART, newdata = %s)') %(r_dataframe)
SHould be:
r('pred = predict(emotionsCART, newdata = %s)' % (r_dataframe) )
The %(r_dataframe) was associated with the r() part, while it should be associated with the '' (string).
But it is hard to check without a reproducible example.

Weird TypeError in jython/python?

I'm bukkit jython/python plugin programmer. Last few days, I'm struggling with this problem. I have to add an potion effect to an user, it's not problem if I enter effect name, duration and amplifier manually in code, but when I want to get them from config, I get this error:
13:38:20 [SEVERE] Could not pass event PlayerInteractEvent to ItemEffect v1.0
Traceback (most recent call last):
File "<iostream>", line 126, in onPlayerInteractEvent
TypeError: addPotionEffect(): 1st arg can't be coerced to org.bukkit.potion.Poti
onEffect
Here's that part of code:
effectname = section.getString("%s.effect"%currentKey)
duration = section.getInt("%s.duration"%currentKey)
durationinticks = duration * 20
geteffectname = "PotionEffectType.%s"%effectname
getpotioneffect = "PotionEffect(%s, %i, 1)"%(geteffectname, durationinticks)
geteffectname = "PotionEffectType.%s"%effectname
if iteminhand == currentKey:
event.getPlayer().addPotionEffect(getpotioneffect)
When I print getpotioneffect out, I get:
13:38:20 [INFO] PotionEffect(PotionEffectType.SPEED, 600, 1)
which is okay, and should work. I tested it without getting informations from config, and it works perfectly... To sum up, code above is not working, but below one works:
getpotioneffect = PotionEffect(PotionEffectType.SPEED, 600, 1)
if iteminhand == currentKey:
event.getPlayer().addPotionEffect(getpotioneffect)
Link to javadocs of this event!
http://jd.bukkit.org/rb/apidocs/org/bukkit/entity/LivingEntity.html#addPotionEffect(org.bukkit.potion.PotionEffect)
Thanks!
In your first snippet, getpotioneffect is a string. You can check it adding print type(getpotioneffect) somewhere.
What you want is to replace this :
geteffectname = "PotionEffectType.%s"%effectname
getpotioneffect = "PotionEffect(%s, %i, 1)"%(geteffectname, durationinticks)
with this:
effect_type = getattr(PotionEffectType, effectname)
potion_effect = PotionEffect(effect_type, durationinticks, 1)

traceback.format_exc/print_exc returns None when expecting traceback

I can't figure out why traceback.format_exc() is returning "None" in the following example:
#!/usr/bin/env python
import sys
import traceback
def my_excepthook(type, value, tb):
print type.__name__
print value
# the problem: why does this return "None"?
print traceback.format_exc(tb) # see http://docs.python.org/library/traceback.html#traceback.format_exc
sys.excepthook = my_excepthook # see http://docs.python.org/library/sys.html#sys.excepthook
# some code to generate a naturalistic exception
a = "text"
b = 5
error = a + b
Using Python 2.7.1, I get the following output:
TypeError
cannot concatenate 'str' and 'int' objects
None
Instead of "None" on the 3rd line, I'd expect to get what happens when I comment out the sys.excepthook line:
Traceback (most recent call last):
File "log-test.py", line 17, in <module>
error = a+b
Try to change like this in my_excepthook:
print "".join(traceback.format_exception(type, value, tb))

Categories