When I run this Python code:
import io, wx
with open("imgs/ad.png", "rb") as fid:
img_stream = io.BytesIO(fid.read())
try:
img = wx.Image(img_stream, type=wx.BITMAP_TYPE_PNM)
except:
pass
it results in a message box saying "Error: This is not a PNM file." instead of raising an exception. Is it possible to have wx raise an exception instead?
Test the result within the try ... except ... statement, raising an error if required, manually.
The wx.Image is handling its own exception to be able to report the error in a wx.Message
import io, wx
app = wx.App()
with open("a.png", "rb") as fid:
img_stream = io.BytesIO(fid.read())
try:
img = wx.Image(img_stream, type=wx.BITMAP_TYPE_PNM)
if img.IsOk():
conv_result = True
else:
raise Exception("Image conversion error!")
except Exception as e:
conv_result = False
print(conv_result)
As #Rolf-of-Saxony pointed out, the message box is due to wxPython logging. Two basic options for suppressing it are temporary creation of the wx.LogNull() object
import io
import wx
with open("imgs/Olympic_flag.svg", "rb") as fid:
img_stream = io.BytesIO(fid.read())
noLog = wx.LogNull()
img = wx.Image(img_stream)
if img.IsOk():
pass
else:
pass
del noLog
or calling wx.Log.EnableLogging()
import io
import wx
wx.Log.EnableLogging(False)
img = wx.Image(img_stream)
if img.IsOk():
pass
else:
pass
wx.Log.EnableLogging(True)
Related
I have an Ansible hosts.ini with an error
[linux]
server01 pr_ip_address = 10.0.0.1
I wrote following function in python
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
def check_inventory():
loader = DataLoader()
InventoryManager(loader=loader, sources='hosts.ini')
check_inventory()
I'm trying to use the following messages as stderr :
[WARNING]: * Failed to parse
/inventories/hosts.ini
with script plugin: problem running
/hosts.ini
--list ([Errno 8] Exec format error: '/hosts.ini')
[WARNING]: * Failed to parse
/hosts.ini
with ini plugin: /hosts.ini:913: Expected key=value host variable assignment, got:
pr_ip_address
[WARNING]: Unable to parse
/hosts.ini
as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
My problem, I don't know to grab it and can't write it to stderr or stdout.
When my ini-file is right like:
[linux]
server01 pr_ip_address=10.0.0.1
I get nothing back.... I thought I can use it for a try except or if else condition, but I don't know how.
I found a solution for my problem. I Use ansible_runner and capture the Output of it. this works well for me :). Maybe it helps someone else too!
import os
import sys
import ansible_runner
from io import StringIO
from termcolor import colored, cprint
def verify(inipath):
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout
with Capturing() as output:
r = ansible_runner.run(private_data_dir=inipath,
inventory='hosts.ini',
host_pattern='localhost',
module='shell',
module_args='whoami')
print(r.status, str)
def words_in_string(word_list, a_string):
return set(word_list).intersection(a_string.split())
error_handling = ['error', 'Failed', 'Errno']
if words_in_string(error_handling, str(output)):
print(output)
cprint('something went wrong with your hosts.ini :(', 'green')
sys.exit(0)
else:
cprint('hosts.ini verify was successfully!!!', 'green')
pass
if __name__ == "__main__":
verify(inipath='./')
An Alternate Approach to the same:-
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
def check_inventory():
loader = DataLoader()
inventory_obj = InventoryManager(loader=loader, sources=None, parse=True)
valid_inventory = inventory_obj.parse_source(source='hosts.ini')
if valid_inventory:
return True
return False
validate_inventory = check_inventory()
if validate_inventory:
print('hosts.ini verification was successful!')
else:
print('something went wrong with your hosts.ini inventory')
This question already has answers here:
Catching boto3 ClientError subclass
(3 answers)
Closed 3 years ago.
I'm trying to create a custom exception based on an error I am getting. This is my code:
import boto3
from botocore.exceptions import ClientError, ProfileNotFound
try:
login_profile = client.get_login_profile(UserName=user_name)
login_profile = (login_profile['LoginProfile']['UserName'])
except Exception as e:
print("Exception", e)
This gives me the following output:
Exception An error occurred (NoSuchEntity) when calling the GetLoginProfile operation: Login Profile for User tdunphy cannot be found.
I tried adding that error as an exception, but it didn't work:
try:
login_profile = client.get_login_profile(UserName=user_name)
login_profile = (login_profile['LoginProfile']['UserName'])
except NoSuchEntity:
print("The login profile does not exist.")
except Exception as e:
print("Exception", e)
Gives me these errors:
botocore.errorfactory.NoSuchEntityException: An error occurred (NoSuchEntity) when calling the GetLoginProfile operation: Login Profile for User tdunphy cannot be found.
During handling of the above exception, another exception occurred:
NameError: name 'NoSuchEntity' is not defined
And using this exception instead:
except NoSuchEntityException:
Gives me this error:
NameError: name 'NoSuchEntityException' is not defined
How can I create an exception for this error that works?
Import the name of this exception in addition to ClientError and ProfileNotFound:
import boto3
from botocore.exceptions import ClientError, ProfileNotFound, NoSuchEntityException
try:
login_profile = client.get_login_profile(UserName=user_name)
login_profile = (login_profile['LoginProfile']['UserName'])
except NoSuchEntityException:
print("The login profile does not exist.")
except Exception as e:
print("Exception", e)
You just need to define NoSuchEntityException. Example:
class NoSuchEntityException(Exception):
def __init__(self):
self.message = "No Such Entity Exception."
Or maybe NoSuchEntityException is defined in other module that you did not import.
edit:
I think this might be what you are after:
import boto3
client = boto3.client('sampleText')
try:
pass
except client.exceptions.NoSuchEntityException:
pass
except will catch a specific Exception class. This means that the exception you handle with except needs to be defined. The exceptions module in goto also holds the NoSuchEntityException as you noted, so import and use accordingly:
from goto.exceptions import ClientError, ProfileNotFound, NoSuchEntityException
try:
login_profile = client.get_login_profile(UserName=user_name)
login_profile = (login_profile['LoginProfile']['UserName'])
except NoSuchEntityException as e:
print("Profile isn't found!")
# do something or exit
except Exception as e:
raise # Don't know what to do yet, so don't handle this
If you must subclass, you can with the base Exception class:
class NoSuchEntitiyException(Exception):
def __init__(self):
self.message = "No such entity"
try:
#code
except NoSuchEntityException:
# do something
As a note, using print in the except clause can cause problems as the program will still progress forward:
try:
raise ValueError("test error")
x = 5
except ValueError:
print("Got a value error!")
print(x)
# NameError: x is not defined
When I am calling the .read() method then my program crashes (with .avi format). My program runs with other formats (.ogv, .mkv) but in that case type(last_frame) == None. Is there any solution to fix these errors?
import cv2
import numpy as np
in_file = r'C:\Users\Johnny\Desktop\zola\animation.avi'
print(in_file)
cap = cv2.VideoCapture(in_file)
print(cap)
ret, last_frame = cap.read() # the program works without this line
try:
if not cap.isOpened() : raise("damn!")
except TypeError as err:
print(err)
Output:
C:\Users\Johnny\Desktop\zola\animation.ogv
<VideoCapture 02843110>
exceptions must be old-style classes or derived from BaseException, not str
I am having an issue with the else statement of this program... I have checked my spacing and it seems to be correct. I keep getting syntax error on the else statement. The program creates and file then attempts to upload it to a ftp server but if it fails to not say anything to the user and just continue It will try again when the program loops. Any help you could provide would be greatly appreciated.
#IMPORTS
import ConfigParser
import os
import random
import ftplib
from ftplib import FTP
#LOOP PART 1
from time import sleep
while True:
#READ THE CONFIG FILE SETUP.INI
config = ConfigParser.ConfigParser()
config.readfp(open(r'setup.ini'))
path = config.get('config', 'path')
name = config.get('config', 'name')
#CREATE THE KEYFILE
filepath = os.path.join((path), (name))
if not os.path.exists((path)):
os.makedirs((path))
file = open(filepath,'w')
file.write('text here')
file.close()
#Create Full Path
fullpath = path + name
#Random Sleep to Accomidate FTP Server
sleeptimer = random.randrange(1,30+1)
sleep((sleeptimer))
#Upload File to FTP Server
try:
host = '0.0.0.0'
port = 3700
ftp = FTP()
ftp.connect(host, port)
ftp.login('user', 'pass')
file = open(fullpath, "rb")
ftp.cwd('/')
ftp.storbinary('STOR ' + name, file)
ftp.quit()
file.close()
else:
print 'Something is Wrong'
#LOOP PART 2
sleep(180.00)
else is valid as part of an exception block, but it is only run if an exception is not raised and there must be a except defined before it.
(edit) Most people skip the else clause and just write code after exiting (dedenting) from the try/except clauses.
The quick tutorial is:
try:
# some statements that are executed until an exception is raised
...
except SomeExceptionType, e:
# if some type of exception is raised
...
except SomeOtherExceptionType, e:
# if another type of exception is raised
...
except Exception, e:
# if *any* exception is raised - but this is usually evil because it hides
# programming errors as well as the errors you want to handle. You can get
# a feel for what went wrong with:
traceback.print_exc()
...
else:
# if no exception is raised
...
finally:
# run regardless of whether exception was raised
...
Given a file of unknown file type, I'd like to open that file with one of a number of handlers. Each of the handlers raises an exception if it cannot open the file.
I would like to try them all and if none succeeds, raise an exception.
The design I came up with is
filename = 'something.something'
try:
content = open_data_file(filename)
handle_data_content(content)
except IOError:
try:
content = open_sound_file(filename)
handle_sound_content(content)
except IOError:
try:
content = open_image_file(filename)
handle_image_content(content)
except IOError:
...
This cascade doesn't seem to be the right way to do it.
Any suggestions?
Maybe you can group all the handlers and evaluate them in a for loop raising an exception at the end if none succeeded. You can also hang on to the raised exception to get some of the information back out of it as shown here:
filename = 'something.something'
handlers = [(open_data_file, handle_data_context),
(open_sound_file, handle_sound_content),
(open_image_file, handle_image_content)
]
for o, h in handlers:
try:
o(filename)
h(filename)
break
except IOError as e:
pass
else:
# Raise the exception we got within. Also saves sub-class information.
raise e
Is checking entirely out of the question?
>>> import urllib
>>> from mimetypes import MimeTypes
>>> guess = MimeTypes()
>>> path = urllib.pathname2url(target_file)
>>> opener = guess.guess_type(path)
>>> opener
('audio/ogg', None)
I know try/except and eafp is really popular in Python, but there are times when a foolish consistency will only interfere with the task at hand.
Additionally, IMO a try/except loop may not necessarily break for the reasons you expect, and as others have pointed out you're going to need to report the errors in a meaningful way if you want to see what's really happening as you try to iterate over file openers until you either succeed or fail. Either way, there's introspective code being written: to dive into the try/excepts and get a meaningful one, or reading the file path and using a type checker, or even just splitting the file name to get the extension... in the face of ambiguity, refuse the temptation to guess.
Like the others, I also recommend using a loop, but with tighter try/except scopes.
Plus, it's always better to re-raise the original exception, in order to preserve extra info about the failure, including traceback.
openers_handlers = [ (open_data_file, handle_data_context) ]
def open_and_handle(filename):
for i, (opener, handler) in enumerate(openers_handlers):
try:
f = opener(filename)
except IOError:
if i >= len(openers_handlers) - 1:
# all failed. re-raise the original exception
raise
else:
# try next
continue
else:
# successfully opened. handle:
return handler(f)
You can use Context Managers:
class ContextManager(object):
def __init__(self, x, failure_handling):
self.x = x
self.failure_handling = failure_handling
def __enter__(self):
return self.x
def __exit__(self, exctype, excinst, exctb):
if exctype == IOError:
if self.failure_handling:
fn = self.failure_handling.pop(0)
with ContextManager(fn(filename), self.failure_handling) as context:
handle_data_content(context)
return True
filename = 'something.something'
with ContextManager(open_data_file(filename), [open_sound_file, open_image_file]) as content:
handle_data_content(content)