Forum Archive

clipboard2jpg

brumm

My first python script. Please tell me your opinion.
https://github.com/humberry/clipboard2jpg

brumm

Another version with camera roll access.
https://github.com/humberry/camera_roll_resize

ccc

I am reading "Writing Idomatic Python" to improve my Python code. This book is full of good rules for keeping Python code clean with nice explanations for each rule... http://www.jeffknupp.com/writing-idiomatic-python-ebook

I saw two rules that related to your script:

4.1.2 Avoid repeating variable name in compound if statement.

# instead of:
        if m == 1:
                m = '1'
        elif m == 2:
                m = 'L'
        elif m == 3:
                m = 'RGB'
        elif m == 4:
                m = 'RGBA'
        elif m == 5:
                m = 'CMYK'
        elif m == 6:
                m = 'YCbCr'
        elif m == 7:
                m = 'I'
        else:
                m = mAlt
        return m, q

# consider using:
        menu_options = { 1 : '1',
                         2 : 'L',
                         3 : 'RGB',
                         4 : 'RGBA',
                         5 : 'CMYK',
                         6 : 'YCbCr',
                         7 : 'I' }
        return menu_options.get(m, mAlt), q

4.1.3 Avoid comparing to True, False, or None.

# instead of:
if r == True:
if (image == None):
if o == 0:

# consider using:
if r:
if not image:
if not o:

Also... The following line made me gulp because it mixes both new style string formatting (format()) and concatenation (+) with type conversion (str()) in a single line of code. If you are going to make a call to format(), try to take maximum advantage of it.

# instead of:
print 'Resolution = ' + str(x) + ' x ' + str(y) + ', quality = {0:.0f}'.format(q*100) + '%, mode = ' + m + ',',
# consider using:
print('Resolution = {} x {}, quality = {0:.0f}%, mode = {},'.format(x, y, q*100, m)),

I hope this helps.

brumm

Thank you very much ccc. Your improvement are looking great.