[Tutor] how do you make this a better code?

Roy Khristopher Bayot roybayot at gmail.com
Fri Sep 5 03:00:43 CEST 2008


Hi. Good day. I am new to Python but I have had experience with C/C++ and
Matlab. I've been reading How to Think Like a Computer Scientist and skimmed
Core Python to learn. Now I havent done the exercises because I wanted to
get down to the project I'm doing in my spare time. The project involves
taking pictures. I found a program called gphoto2 that can do what i need.
But it was in command line. And i wanted to access it with a program. So I
made a class that calls gphoto2 to control a camera. Here's the code.

class CamHandle(object):
    def __init__(self):
        import os
        w = os.system('gphoto2 --auto-detect > CamHandleLog.txt')
        test = os.system('gphoto2 --capture-image >> CamHandleLog.txt')

    def captureImage(self):
        import os
        x = os.system('gphoto2 --capture-image >> CamHandleLog.txt')

    def getAllImages(self):
        import os
        y = os.system('gphoto2 --get-all-files >> CamHandleLog.txt')

    def deleteAllImages(self):
        import os
        z = os.system('gphoto2 -DR >> CamHandleLog.txt')

Now when I import the file to the interpreter and instantiate the class, it
works. Each function works. I havent done any error handling or exception
handling yet but it works for now. However, I am bothered with the import
statements in each function. Correct me if I am wrong but each "import os"
statement only exists in each method. Is there a way to only import the os
module once and use it in each method?

Thank you and have a nice day.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080905/72e61f5a/attachment.htm>


More information about the Tutor mailing list