subclassing Exceptions

phawkins at spamnotconnact.com phawkins at spamnotconnact.com
Sun Jul 22 23:07:32 EDT 2001


Oh, fer crying out loud, Sheila just wants an example of exceptions in
use; she said so quite specifically and unambiguously.  The hard part
about exceptions is that they're way simpler than you think.

Here you go, Sheila, a nice little camera interface, uses exceptions.
It uses an argument instead of defining __str__, which I hadn't realized
was a possibility.  Hmmm, interesting (playing in Idle), defining
__str__ as you do causes an exception to not take an argument.  See
next post...


import string
import commands
import exceptions
import re
import os.path
from glob import glob

class Camera:
    """A class abstracting the camera interface; designed to call a command-line interface tool"""

    def __init__(self, camera = "/dev/kodak"):
        if not os.path.exists(camera):
            raise NoDevice, "device does not exist" + camera
        else:
            self.__camera = camera
            result = commands.getoutput("/usr/local/bin/ks -d " + self.__camera + " settime")

    def download_images(self):
        if self.is_on():
            result = commands.getoutput("/usr/local/bin/ks -d " + self.__camera + " download")
        else:
            raise NotResponding, "Camera not responding"

    def download_thumbs(self):
        if self.is_on():
            result = commands.getoutput("/usr/local/bin/ks -d " + self.__camera + " thumbs")
        else:
            raise NotResponding, "Camera not responding"
        self.convert_thumbnails()
        
    def convert_thumbnails(self):
        for thumb in glob('*.ppm'):
            thumb_path, discard = os.path.splitext(thumb)
            if not os.path.exists(thumb_path + 'thmb.jpg'):
                commands.getoutput('convert ' + thumb + " " + thumb_path + 'thmb.jpg')

    def is_on(self):
        result = commands.getoutput("/usr/local/bin/ks -d " + self.__camera + " getpowermode")
        if re.search("camera is on", result):
            return 1
        elif re.search("camera is off", result):
            return 0
        else:
            raise IOError, "Bad output from camera check -- " + result 

    def delete_all_images(self):
        if self.is_on():
            result = commands.getoutput("/usr/local/bin/ks -d " + self.__camera + " delall")
        else:
            raise NotResponding, "Camera not responding"
        
    def n_images(self):
        if self.is_on():
            result = commands.getoutput("/usr/local/bin/ks -d " + self.__camera + " status")
            match = re.search("(\d+)", result)
            return int(match.group(1))
        else:
            raise NotResponding, "Camera not responding"
        
    def shoot(self):
# Todo: add a timestamp every time this is called; check and wait 30 seconds
# before shooting again so camera can finish processing.
        
        if self.is_on():
            result = commands.getoutput("/usr/local/bin/ks -d " + self.__camera + " shoot")
        else:
            raise NotResponding, "Camera not responding"
        
class CameraError(exceptions.Exception):
    def __init__ (self, args = None):
        self.args = args

class NoDevice(CameraError):
    pass

class NotResponding(CameraError):
    pass

-- 
Patricia J. Hawkins
Hawkins Internet Applications, LLC





More information about the Python-list mailing list