Refactor GnomeSudoku code

My random and unstructured thoughts on how sudoku code can be improved.

  1. in SudokuNumberGrid (gsudoku.py), parameters of methods should be coordinates, not the object representing each number-box. There aren't any cases going against this now, so it just guides future changes. (But is this proper?)

  2. SudokuGameDisplay (gsudoku.py) has too many methods.

  3. Make (most) methods as private.

  4. Add a method to SudokuGameDisplay to retrieve a box, instead of using __entries__[(x, y)] everywhere.

    • def get_number_box (self, x, y)

      then replace all __entries__

  5. Functions/methods should have docstring, at least the newly added ones.
  6. Define an interface for sudoku logic.
    • input: requests to update the logic
    • output: signals per the result of the request.
    So, the number boxes read user input and then send them as requests to the logic. The 9x9 grid listens for signals from the logic and then update the GUI. Other objects can also send request to and receive signals from the logic, say Auto-Fill and a DBus object.

Checke the code using Pylint

run_pylint.sh: Check the Code

The following code is much adopted from orca's.

#!/bin/bash
#
# Script to run pylint on the GNOME-Sudoku sources you've modified or added.
#
if [ "x$*" == "x" ]
then
    FILES=`git status | egrep 'modified:|new file:' | grep '[.]py$' | awk '{ print $NF }'`
    echo $FILES
else
    FILES="$*"
fi
echo Thank you for your attention to quality

# write pylint files into a separate 'lint' dir.
(test -d lint) || mkdir lint

for foo in $FILES
do
    echo
    OUTPUT_FILE=lint/`basename $foo .py`.pylint
    # no need to check if OUTPUT_FILE is newer than foo
    if [ -e $OUTPUT_FILE -a $OUTPUT_FILE -nt $foo ]
    then
        echo "$foo is not changed since last check"
    else
        echo Checking $foo, sending output to $OUTPUT_FILE
        pylint $foo > $OUTPUT_FILE 2>&1
    fi
    grep "code has been rated" $OUTPUT_FILE
done

report.sh: Get a list of status from generated .pylint files

#!/bin/bash

function report {
    for file in `ls lint/*.pylint`
    do
        # the status is on the second line from end of file.
        echo -n `tail -n 2 $file | cut -f7 -d ' ' | cut -f1 -d '/'`
        echo -e "\t: `basename $file | cut -f1 -d '.'`"
    done
}

report | sort -n


Back to Home

ZhangSen/sudoku (last edited 2009-05-14 02:55:27 by ZhangSen)