Error :
File "C:\Users\hp\Anaconda2\lib\site-packages\pyhdf\SD.py", line 1623,
in select
raise HDF4Error("select: non-existent dataset")
HDF4Error: select: non-existent dataset
Why this error is there i am not getting and as i am not a programmer so not getting the point where the problem is defined.
if isinstance(name_or_index, type(1)):
idx = name_or_index
else:
try:
idx = self.nametoindex(name_or_index)
except HDF4Error:
raise HDF4Error("select: non-existent dataset")
id = _C.SDselect(self._id, idx)
_checkErr('select', id, "cannot execute")
return SDS(self, id)
Be careful with indents(4 character spaces or tab) in python. Python uses indents to identify a block of code instead of any enclosures.
Try running this:
if isinstance(name_or_index, type(1)):
idx = name_or_index
else:
try:
idx = self.nametoindex(name_or_index)
except HDF4Error:
raise HDF4Error("select: non-existent dataset")
id = _C.SDselect(self._id, idx)
_checkErr('select', id, "cannot execute")
return SDS(self, id)
I just put an indent for except statement. Check if it works :)
I corrected all indents too after you said you got error again
Related
For example, I am parsing an xml file element, and all 4 elements are required. My code this like this with minidom library:
id = pattern.getElementsByTagName("id")[0].firstChild.data
name = pattern.getElementsByTagName("name")[0].firstChild.data
trigger = pattern.getElementsByTagName("trigger")[0].firstChild.data
test = pattern.getElementsByTagName("test")[0].firstChild.data
If the xml document lack any of the 4 tags, I want to throw an IndexError exception. Should I use 4 try ... except blocks to capture each element exception, or I should just capture all the 4 similar exceptions in one big block? The benefit of capturing individual errors is that I can print out more explicit error message regarding the lack of a specific xml element, but it looks verbose. Is there a good practice here?
try:
id = pattern.getElementsByTagName("id")[0].firstChild.data
except IndexError:
raise IndexError('id must exists in the xml file!')
try:
name = pattern.getElementsByTagName("name")[0].firstChild.data
except IndexError:
raise IndexError('name must exists in the xml file!')
try:
test = pattern.getElementsByTagName("test")[0].firstChild.data
except IndexError:
raise IndexError('test must exists in the xml file!')
try:
trigger = pattern.getElementsByTagName("trigger")[0].firstChild.data
except IndexError:
raise IndexError('trigger must exists in the xml file!')
OR
try:
id = pattern.getElementsByTagName("id")[0].firstChild.data
name = pattern.getElementsByTagName("name")[0].firstChild.data
trigger = pattern.getElementsByTagName("trigger")[0].firstChild.data
test = pattern.getElementsByTagName("test")[0].firstChild.data
except IndexError:
raise IndexError('id, name, trigger and test must exist in the xml file!')
Which one is better or both are not great?
Consider using a loop over the field names and packing the results into a dict instead!
results = {}
for field_name in ("id", "name", "trigger", "test"):
try:
results[field_name] = xml.getElementsByTagName(field_name)[0].firstChild.data
except IndexError as ex:
raise IndexError(f"failed to read '{field_name}' from xml {repr(ex)}")
Hi I am using a python 3 script to do snmp set operation to one of switch.
The script is as follows.
import pysnmp
from pysnmp import hlapi
def cast(value):
try:
return int(value)
except (ValueError, TypeError):
try:
return float(value)
except (ValueError, TypeError):
try:
return str(value)
except (ValueError, TypeError):
pass
return value
def fetch(handler, count):
result = []
for i in range(count):
try:
error_indication, error_status, error_index, var_binds = next(handler)
if not error_indication and not error_status:
items = {}
for var_bind in var_binds:
items[str(var_bind[0])] = cast(var_bind[1])
result.append(items)
else:
raise RuntimeError('Got SNMP error: {0}'.format(error_indication))
except StopIteration:
break
return result
def construct_value_pairs(list_of_pairs):
pairs = []
for key, value in list_of_pairs.items():
pairs.append(hlapi.ObjectType(hlapi.ObjectIdentity(key), value))
return pairs
def set(target, value_pairs, credentials, port=161, engine=hlapi.SnmpEngine(), context=hlapi.ContextData()):
handler = hlapi.setCmd(
engine,
credentials,
hlapi.UdpTransportTarget((target, port)),
context,
*construct_value_pairs(value_pairs)
)
return fetch(handler, 1)[0]
If i run set('10.23.193.153', {'1.3.6.1.2.1.1.5.0': 'Test1'}, hlapi.CommunityData('public'))
The script is executed and the hostname on switch changes to Test1.
However if i do another set operation using
set('10.23.193.153', {'1.3.6.1.4.1.11.2.14.11.5.1.16.19.1.2.1': 'admin'}, hlapi.CommunityData('public'))
I am getting the following error.
The above oid changes username of the switch.
> ============ RESTART: C:/Users/regop/Desktop/SNMP Password Reset.py ============ Traceback (most recent call last): File "C:/Users/regop/Desktop/SNMP Password Reset.py", line 53, in <module>
> set('10.23.193.153', {'1.3.6.1.4.1.11.2.14.11.5.1.16.19.1.2.1': 'admin'}, hlapi.CommunityData('public')) File
> "C:/Users/regop/Desktop/SNMP Password Reset.py", line 49, in set
> return fetch(handler, 1)[0] File "C:/Users/regop/Desktop/SNMP Password Reset.py", line 22, in fetch
> error_indication, error_status, error_index, var_binds = next(handler) File
> "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\hlapi\asyncore\sync\cmdgen.py",
> line 214, in setCmd
> cmdgen.setCmd(snmpEngine, authData, transportTarget, File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\hlapi\asyncore\cmdgen.py",
> line 239, in setCmd
> return cmdgen.SetCommandGenerator().sendVarBinds( File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\entity\rfc3413\cmdgen.py",
> line 249, in sendVarBinds
> v2c.apiPDU.setVarBinds(reqPDU, varBinds) File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\proto\api\v1.py",
> line 131, in setVarBinds
> apiVarBind.setOIDVal( File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\proto\api\v1.py",
> line 42, in setOIDVal
> varBind.setComponentByPosition(1).getComponentByPosition(1).setComponentByType(val.getTagSet(),
> val, verifyConstraints=False, matchTags=False, matchConstraints=False,
> innerFlag=True) AttributeError: 'str' object has no attribute
> 'getTagSet'
Not sure what different I am doing here.
This unhandled exception feels like a bug in pysnmp, but that's irrelevant to your question.
My guess is that the problem is caused by your second OID not being resolved at a MIB and therefore the value (admin) not automatically casted into some SNMP type object.
The solution is one of:
Refer to your SNMP objects by their symbolic names (MIB, symbol, instance ID)
Keep using OIDs, but pre-load the MIB where that OID is defined
Keep using OIDs, but pass your values as SNMP objects so that no MIBs would be necessary
I have the same error, Because of my SNMP Device doesn't support 'integer' to write, just 'String'.
I suggest to check your OID address whether it supports 'String' to write or not.
I have
def manufacturer(request, manufacturer_title):
#try:
# m = Manufacturer.objects.filter(title__iexact=manufacturer_title)
#except Manufacturer.DoesNotExist:
# raise Http404("No such manufacturer")
return HttpResponse("Manufacturer: %s" % manufacturer_title)
#return HttpResponse("Manufacturer: %s" % m.title)
You can see what I'm trying to do here. With only that second to last line not commented out, this works for any value entered for manufacturer_title, and prints it however it's typed, no matter whether it's valid or not. However clearly I need it to match an existing manufacturer. If I un-comment the above commented lines (excluding the last) the server is unable to connect.
Django filter does not raise DoesNotExists error.
You can rewrite your code to this:
def manufacturer(request, manufacturer_title):
m = Manufacturer.objects.filter(title__iexact=manufacturer_title)
if not m.exists():
raise Http404("No such manufacturer")
return HttpResponse("Manufacturer: %s" % manufacturer_title)
or if title is uqique field you can just use get():
try:
m = Manufacturer.objects.get(title__iexact=manufacturer_title)
except Manufacturer.DoesNotExist:
raise Http404("No such manufacturer")
return HttpResponse("Manufacturer: %s" % m.title)
filter does not raise an exception. May be you are trying to get a unique object, It can be done with get like this:
Manufacturer.objects.get(title__iexact=manufacturer_title)
Now get will raise an exception if the object does not exist.
What you require in your case can be done with a shortcut function too. Like this:
If you require a single object:
from django.shortcuts import get_object_or_404
def manufacturer(request, manufacturer_title):
m = get_object_or_404(Manufacturer, title__iexact=manufacturer_title)
return HttpResponse("Manufacturer: %s" % manufacturer_title)
If you require a queryset:
from django.shortcuts import get_list_or_404
def manufacturer(request, manufacturer_title):
m = get_list_or_404(Manufacturer, title__contains=manufacturer_title)
return HttpResponse("Manufacturer: %s" % manufacturer_title)
I have the following function:
def request( url, type, headers, simulate = False, data = {}):
I want to be able to load the parameters from a text file and pass them to the function, I tried using evil eval below:
if execute_recovery:
for command in content:
logger.debug("Executing: "+command)
try:
result = eval(utilities.request("{0}").format(command))
if not result["Success"]:
continue_recovery = utilities.query_yes_no("Warning: Previous recovery command failed, attempt to continue recovery?\n")
if not continue_recovery:
break
else:
logger.debug("Command executed successfully...")
except Exception, e:
logger.debug( "Recovery: Eval Error, %s" % str(e) )
Where command would be a line in a text file like:
"http://192.168.1.1/accounts/1/users/1",delete,headers,simulate=False,data={}
This throws me the following error:
'request() takes at least 3 arguments (1 given)'
So presumably this means that it is interpreting the command as a single string instead of different parameters.
Does anybody know how to solve this?
I can't understand what you are trying to do there with eval or format. For one thing, you've put eval around the call to request itself, so it will evaluate the return value rather than call it with some dynamic value.
But you don't need eval at all. You just need to pass the arguments using the * and ** operators:
args = []
kwargs = {}
for arg in command.split(','):
if '=' in arg:
k, v = arg.split('=')
kwargs[k] = ast.literal_eval(v)
else:
args.append(arg)
result = utilities.request(*args, **kwargs)
Using #BurhanKhalid's suggestion, I decided to store the parameters as a json object and load them at run time like so:
Store parameters here:
def request( url, type, headers, simulate = False, data = {}):
if simulate:
recovery_command = {"url":url, "type" : type, "data" : data}
recovery.add_command(json.dumps(recovery_command))
...
Load parameters here:
def recovery():
...
if execute_recovery:
for command in content:
logger.debug("Executing: "+command)
try:
recovery_command = json.loads(command)
result = utilities.request(url = recovery_command["url"], type = recovery_command["type"], headers = headers, simulate = False, data = recovery_command["data"])
if not result["Success"]:
continue_recovery = utilities.query_yes_no("Warning: Previous recovery command failed, attempt to continue recovery?\n")
if not continue_recovery:
break
else:
logger.debug("Command executed successfully...")
except Exception, e:
logger.debug( "Recovery: Eval Error, %s" % str(e) )
I am using ConfigObj and Validator to parse a configuration file in python. While I like this tool a lot, I am having trouble with validation using a configSpec file. I am using the option() configSpec type that forces the value to be chosen from a controlled vocabulary:
output_mode = option("Verbose", "Terse", "Silent")
I want my code to know when the user enters an option that's not in the CV. From what I have fond, Validator only seems to say which config key failed validation, but not why it failed:
from configobj import ConfigObj, flatten_errors
from validate import Validator
config = ConfigObj('config.ini', configspec='configspec.ini')
validator = Validator()
results = config.validate(validator)
if results != True:
for (section_list, key, _) in flatten_errors(config, results):
if key is not None:
print 'The "%s" key in the section "%s" failed validation' % (key, ', '.join(section_list))
else:
print 'The following section was missing:%s ' % ', '.join(section_list)
That code snippet works but there are any number of reasons why a key might have failed validation, from not being in an integer range to not being in the CV. I don't want to have to interrogate the key name and raise a different kind of exception depending on the failure cases for that key. Is there a cleaner way to handle specific types of validation errors?
Long time stackoverflow reader, first time poster :-)
Update: I think this does what I want to do. The key is that config obj stores errors as Exceptions which can then be checked against those that subclass ValidateError. Then you just have to do one check per subclass rather than one check per parameter value. It might be nicer if validate just threw an exception if validation failed but maybe you would lose other functionality.
self.config = configobj.ConfigObj(configFile, configspec=self.getConfigSpecFile())
validator = Validator()
results = self.config.validate(validator, preserve_errors=True)
for entry in flatten_errors(self.config, results):
[sectionList, key, error] = entry
if error == False:
msg = "The parameter %s was not in the config file\n" % key
msg += "Please check to make sure this parameter is present and there are no mis-spellings."
raise ConfigException(msg)
if key is not None:
if isinstance(error, VdtValueError):
optionString = self.config.configspec[key]
msg = "The parameter %s was set to %s which is not one of the allowed values\n" % (key, self.config[key])
msg += "Please set the value to be in %s" % optionString
raise ConfigException(msg)
OptionString is just a string of the form option("option 1", "option 2") rather than a list so to get this to look nice, you need to grab the substring in the ()'s.
For future reference for anyone interested, you could also check for extraneous data. This can be handled with the get_extra_values function. The complete example shown below hence does:
load the configuration with validator
look for all the validated errors
verify extra values
from configobj import ConfigObj, ConfigObjError, flatten_errors, get_extra_values
from validate import Validator, VdtValueError
def load_config(configfile, configspec, raise_exception=True):
"Load and check configvale acccording to spec"
config = ConfigObj(configfile, file_error=True, configspec=configspec)
validator = Validator()
results = config.validate(validator, preserve_errors=True)
msg = ""
fatalerr = False
for entry in flatten_errors(config, results):
[sectionList, key, error] = entry
if error is False:
msg += f"\n{key:>30s} missing in section [{']['.join(sectionList)}]"
fatalerr = True
if key is not None:
if isinstance(error, VdtValueError):
optionString = config.configspec[key]
msg += f"\nThe parameter {key} was set to {[config[s][key] for s in sectionList]} which is not one of the allowed values\n"
msg += " Please set the value to be in %s" % optionString
fatalerr = True
# verifying extra values below
wmsg = ""
for sections, name in get_extra_values(config):
# this code gets the extra values themselves
the_section = config
for section in sections:
the_section = the_section[section]
# the_value may be a section or a value
the_value = the_section[name]
section_or_value = 'value'
if isinstance(the_value, dict):
# Sections are subclasses of dict
section_or_value = 'section'
section_string = '[' + (']['.join(sections) or "TOP LEVEL") + ']'
wmsg += f"\n{name:>30s}: Extra {section_or_value} on section {section_string}"
if wmsg != "":
print(f"\nWARNINGS found in configuration file {configfile}")
print(wmsg)
if fatalerr:
print(f"\nERRORS found in configuration file {configfile}")
if raise_exception:
raise RuntimeError(msg)
else:
print("Fatal errors found, but no exception raised, as requested")
print(msg)
print(f'Configuration {configfile} validated successfully')
return config
if __name__ == "__main__":
configfile="xt_default.cfg"
configspec="xt_default_spec.cfg"
config = load_config(configfile, configspec)