Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Programming Talk
This forum is for all programming questions.
The questions do not have to be directly related to Ubuntu and any programming language is allowed.

 
Thread Tools Display Modes
Old December 17th, 2007   #1
verb3k
A Carafe of Ubuntu
 
verb3k's Avatar
 
Join Date: Mar 2007
My beans are hidden!
Need your comments on a PyGTK PlayStation CD backup tool

Hi all,

This is my first post in this section and I hope I get something useful out of it.
I am new to programming and currently learning python, working on a GUI app to backup PSX games using "cdrdao" as a backend.

The program in its current state works just fine, but I can see lots of drawbacks:

1-The program is bloated and the code looks ugly.
2-No object-oriented programming was used. ( I know it's not necessary)
3-Many noobish habits and techniques were used to solve some problems.
4-The use of some shell commands instead of doing things in a pythonic way.

I want you to help me polish it and teach me how to do it the right way (and help me toss those bad techniques away)

I would also appreciate it if someone could tell me why I would want to do it in OOP, what's the benefits? what's object-oriented programming?(still struggling with this question)

Here's the code:
http://dpaste.com/hold/28221/

Note the use of the global variable avail, I am sure there is a better way, but can't come up with it.

Thanks in advance,
verb3k
__________________
Freedom is neither exclusive nor unlimited.
verb3k is offline   Reply With Quote
Old December 17th, 2007   #2
Quikee
Ubuntu Extra Shot
 
Quikee's Avatar
 
Join Date: Apr 2006
Location: Slovenia
Beans: 356
Ubuntu Karmic Koala (testing)
Re: Need your comments on a PyGTK PlayStation CD backup tool

insted of:

PHP Code:
def errors(number):
    if 
number == 1:
        
error_message("Missing Dependency","The program \"cdrdao\" was not found on your system. You can get it from:\nhttp:/cdrdao.sourceforge.net")
    
elif number == 2:
        
error_message("Error!","An error occurred!")
    
elif number == 3:
        
error_message("Error!","An error occurred!\n\n1-Did you choose the right CD drive?\n2-Are you sure the game's CD is in the specified drive?\n\nIf none of these solves your problem, contact the author.")
    
elif number == 4:
        
error_message("Error","The name can only contain letters and/or numbers")
    
elif number == 5:
        
error_message("Error","You must supply a game name.")
    
elif number == 6:
        
error_message("Error","The supplied directory does not exist.")
    
elif number == 7:
        
error_message("Error","You don't have \"write\" perrmissions to the specified directory.")
    
elif number == 8:
        
error_message("Error","You don't have \"execute\" perrmissions to the specified directory.")
    
elif number == 9:
        
error_message("Error","Files with the same name already exist."


PHP Code:
error_messages = (
    (
"Missing Dependency","The program \"cdrdao\" was not found on your system. You can get it from:\nhttp:/cdrdao.sourceforge.net"),
    (
"Error!","An error occurred!"),
    (
"Error!","An error occurred!\n\n1-Did you choose the right CD drive?\n2-Are you sure the game's CD is in the specified drive?\n\nIf none of these solves your problem, contact the author."),
    (
"Error","The name can only contain letters and/or numbers"),
    (
"Error","You must supply a game name."),
    (
"Error","The supplied directory does not exist."),
    (
"Error","You don't have \"write\" perrmissions to the specified directory."),
    (
"Error","You don't have \"execute\" perrmissions to the specified directory."),
    (
"Error","Files with the same name already exist."),
)

def errors(number):
    
titletext error_messages[number]
    
error_message(titletext
Rename error_message to show_error_message or something more appropriate.

Also:

PHP Code:
def message(title,text):
    
dialog=gtk.MessageDialog(
            
parent            window,
            
flags            gtk.DIALOG_DESTROY_WITH_PARENT gtk.DIALOG_MODAL,
            
type            gtk.MESSAGE_INFO,
            
buttons            gtk.BUTTONS_CLOSE,
            
message_format    text)
    
dialog.set_title(title)
    
dialog.run()
    
dialog.destroy()


def error_message(title,text):
    
dialog=gtk.MessageDialog(
            
parent            window,
            
flags            gtk.DIALOG_DESTROY_WITH_PARENT gtk.DIALOG_MODAL,
            
type            gtk.MESSAGE_ERROR,
            
buttons            gtk.BUTTONS_CLOSE,
            
message_format    text)
    
dialog.set_title(title)
    
dialog.run()
    
dialog.destroy() 
Code duplication is considered bad. You could do something like this:

PHP Code:
severity_level_for_message_box = {
    
'INFO' gtk.MESSAGE_INFO,
    
'ERROR' gtk.MESSAGE_ERROR
}

def message_box(titletextseverity):
    
dialog=gtk.MessageDialog(
            
parent window,
            
flags gtk.DIALOG_DESTROY_WITH_PARENT gtk.DIALOG_MODAL,
            
type severity_level_for_message_box[severity],
            
buttons gtk.BUTTONS_CLOSE,
            
message_format    text)
    
dialog.set_title(title)
    
dialog.run()
    
dialog.destroy()

def errors(number):
    
titletext error_messages[number]
    
message_box(titletext'ERROR'
Quikee is offline   Reply With Quote
Old December 17th, 2007   #3
slavik
Ubuntu Master Roaster
 
slavik's Avatar
 
Join Date: Jan 2006
My beans are hidden!
Ubuntu Jaunty Jackalope (testing)
Re: Need your comments on a PyGTK PlayStation CD backup tool

may I suggest looking into libglade?
__________________
I am infallible, you should know that by now.
"My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
42 lines of Perl - SHI
slavik is offline   Reply With Quote
Old December 17th, 2007   #4
Wybiral
Tall Cafè Ubuntu
 
Wybiral's Avatar
 
Join Date: Oct 2006
Beans: 2,678
Ubuntu 7.10 Gutsy Gibbon
Re: Need your comments on a PyGTK PlayStation CD backup tool

Quote:
Originally Posted by slavik View Post
may I suggest looking into libglade?
I second that. It will make it easier to maintain later. Dynamic widget creation should be reserved for applications that require their interface to dynamically alter itself, this one is stationary so I would just use Glade.

My other critique (since you asked) is to use 4-space tabs instead of regular tabs (it's practically standard in Python, and is definitely considered good practice).
__________________
Wybiral is offline   Reply With Quote
Old December 17th, 2007   #5
verb3k
A Carafe of Ubuntu
 
verb3k's Avatar
 
Join Date: Mar 2007
My beans are hidden!
Re: Need your comments on a PyGTK PlayStation CD backup tool

Thanks Quikee for those corrections .... will be studied indeed

slavik and Wybiral, I agree ...I should have used glade but I thought as I was learning, I needed to know how to do a GUI myself rather than using a builder. I think I am starting to understand some basics.
I also like the idea that my program is only one file
__________________
Freedom is neither exclusive nor unlimited.
verb3k is offline   Reply With Quote
Old December 17th, 2007   #6
slavik
Ubuntu Master Roaster
 
slavik's Avatar
 
Join Date: Jan 2006
My beans are hidden!
Ubuntu Jaunty Jackalope (testing)
Re: Need your comments on a PyGTK PlayStation CD backup tool

understandable, but the counterpoint to the one file program is that (while not in this case) sometimes splitting your program into separate files is very good for maintainability (library/front end is a very common split).
__________________
I am infallible, you should know that by now.
"My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
42 lines of Perl - SHI
slavik is offline   Reply With Quote
Old December 17th, 2007   #7
verb3k
A Carafe of Ubuntu
 
verb3k's Avatar
 
Join Date: Mar 2007
My beans are hidden!
Re: Need your comments on a PyGTK PlayStation CD backup tool

Quote:
Originally Posted by Quikee View Post

PHP Code:
error_messages = (
    (
"Missing Dependency","The program \"cdrdao\" was not found on your system. You can get it from:\nhttp:/cdrdao.sourceforge.net"),
    (
"Error!","An error occurred!"),
    (
"Error!","An error occurred!\n\n1-Did you choose the right CD drive?\n2-Are you sure the game's CD is in the specified drive?\n\nIf none of these solves your problem, contact the author."),
    (
"Error","The name can only contain letters and/or numbers"),
    (
"Error","You must supply a game name."),
    (
"Error","The supplied directory does not exist."),
    (
"Error","You don't have \"write\" perrmissions to the specified directory."),
    (
"Error","You don't have \"execute\" perrmissions to the specified directory."),
    (
"Error","Files with the same name already exist."),
)

def errors(number):
    
titletext error_messages[number]
    
error_message(titletext
I think this way is slower the the previous, because the interpreter will have to slice the error messages and assign them and their titles to 2 variables and then operate on them every time messages are needed to be displayed, surely not that big difference but I like faster programs.

Don't you think that's true?
__________________
Freedom is neither exclusive nor unlimited.
verb3k is offline   Reply With Quote
Old December 18th, 2007   #8
Quikee
Ubuntu Extra Shot
 
Quikee's Avatar
 
Join Date: Apr 2006
Location: Slovenia
Beans: 356
Ubuntu Karmic Koala (testing)
Thumbs up Re: Need your comments on a PyGTK PlayStation CD backup tool

Quote:
Originally Posted by verb3k View Post
I think this way is slower the the previous, because the interpreter will have to slice the error messages and assign them and their titles to 2 variables and then operate on them every time messages are needed to be displayed, surely not that big difference but I like faster programs.

Don't you think that's true?
I really don't know - it should be faster because you don't go through all ifs and compare if the number is the input number. And I also used tuples instead of lists which are "faster" (they are immutable which means they can't change once they are created - like strings) than regular lists. So this would be generally identical to:
PHP Code:
error_messages = [
    [
"Missing Dependency","The program \"cdrdao\" was not found on your system. You can get it from:\nhttp:/cdrdao.sourceforge.net"],
    [
"Error!","An error occurred!"],
    [
"Error!","An error occurred!\n\n1-Did you choose the right CD drive?\n2-Are you sure the game's CD is in the specified drive?\n\nIf none of these solves your problem, contact the author."],
    [
"Error","The name can only contain letters and/or numbers"],
    [
"Error","You must supply a game name."],
    [
"Error","The supplied directory does not exist."],
    [
"Error","You don't have \"write\" perrmissions to the specified directory."],
    [
"Error","You don't have \"execute\" perrmissions to the specified directory."],
    [
"Error","Files with the same name already exist."],
]

def errors(number):
    
error error_messages[number]
    
title error[0]
    
text error[1]
    
error_message(titletext
Anyway you should not notice any difference.. also first priority should not be optimisation but that it works correctly and doesn't crash. Optimisations can then be applied later.
Quikee is offline   Reply With Quote
Old December 18th, 2007   #9
verb3k
A Carafe of Ubuntu
 
verb3k's Avatar
 
Join Date: Mar 2007
My beans are hidden!
Re: Need your comments on a PyGTK PlayStation CD backup tool

Thanks Quikee for your support

Any other comments are welcome.
__________________
Freedom is neither exclusive nor unlimited.
verb3k is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:36 PM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry