Ignore:
Timestamp:
Apr 18, 2011 11:16:39 AM (13 years ago)
Author:
vmaksymiv
Message:

pep8 fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.captcha.core/trunk/quintagroup/captcha/core/utils.py

    r3097 r3128  
    1919    import Crypto 
    2020 
     21 
    2122def encrypt1(s): 
    2223    return md5.md5(s).hexdigest().upper() 
    2324 
     25 
    2426def getTransform(x, y, a, p, o): 
    25     return (math.sin( (y+o[0])*p )*a + x, math.sin( (x+o[1])*p )*a + y) 
     27    return (math.sin((y + o[0]) * p) * a + x, math.sin((x + o[1]) * p) * a + y) 
     28 
    2629 
    2730def gen_captcha(**kwargs): 
     
    5760 
    5861    #select font for captcha text 
    59     ALL_FONTS=('Bd', 'It', 'MoBI', 'Mono', 'Se', 'BI', 'MoBd', 'MoIt', 'SeBd', '') 
     62    ALL_FONTS = ('Bd', 'It', 'MoBI', 'Mono', 'Se', 
     63                 'BI', 'MoBd', 'MoIt', 'SeBd', '') 
    6064    rand_font = random.choice(ALL_FONTS) 
    61     font = ImageFont.truetype(FONT_PATH+'/vera/Vera%s.ttf'%rand_font, fnt_sz) 
     65    rand_font_path = FONT_PATH + '/vera/Vera%s.ttf' % rand_font 
     66    font = ImageFont.truetype(rand_font_path, fnt_sz) 
    6267    textSize = font.getsize(text) 
    6368 
    6469#------------------------------render   background1 ----------------------- 
    65     image = Image.new('RGB', (textSize[0]+7,textSize[1]+7), bkground) 
     70    image = Image.new('RGB', (textSize[0] + 7, textSize[1] + 7), bkground) 
    6671    image.paste(bkground) 
    6772#------------------------------render       Text2 ------------------------ 
    6873    draw = ImageDraw.Draw(image) 
    69     alignment = (random.uniform(0,1), random.uniform(0,1)) 
     74    alignment = (random.uniform(0, 1), random.uniform(0, 1)) 
    7075    x = int((image.size[0] - textSize[0]) * alignment[0] + 0.5) 
    7176    y = int((image.size[1] - textSize[1]) * alignment[1] + 0.5) 
    7277 
    73     draw.text((x,y), text, font=font, fill=font_color) 
     78    draw.text((x, y), text, font=font, fill=font_color) 
    7479 
    7580#------------------------------render       Distortion ----------------------- 
     
    8590        yRow = [] 
    8691        for i in xrange(xPoints): 
    87             x, y = getTransform(i*r, j*r, amplitude, period, offset) 
     92            x, y = getTransform(i * r, j * r, amplitude, period, offset) 
    8893 
    8994            # Clamp the edges so we don't get black undefined areas 
    90             x = max(0, min(image.size[0]-1, x)) 
    91             y = max(0, min(image.size[1]-1, y)) 
     95            x = max(0, min(image.size[0] - 1, x)) 
     96            y = max(0, min(image.size[1] - 1, y)) 
    9297 
    9398            xRow.append(x) 
     
    99104    # each square between points on the grid 
    100105    mesh = [] 
    101     for j in xrange(yPoints-1): 
    102         for i in xrange(xPoints-1): 
     106    for j in xrange(yPoints - 1): 
     107        for i in xrange(xPoints - 1): 
    103108            mesh.append(( 
    104109                # Destination rectangle 
    105                 (i*r, j*r, 
    106                  (i+1)*r, (j+1)*r), 
     110                (i * r, j * r, 
     111                 (i + 1) * r, (j + 1) * r), 
    107112                # Source quadrilateral 
    108                 (xRows[j  ][i  ], yRows[j  ][i  ], 
    109                  xRows[j+1][i  ], yRows[j+1][i  ], 
    110                  xRows[j+1][i+1], yRows[j+1][i+1], 
    111                  xRows[j  ][i+1], yRows[j  ][i+1]), 
     113                (xRows[j][i],         yRows[j][i], 
     114                 xRows[j + 1][i],     yRows[j + 1][i], 
     115                 xRows[j + 1][i + 1], yRows[j + 1][i + 1], 
     116                 xRows[j][i + 1],     yRows[j][i + 1]), 
    112117                )) 
    113118 
     
    120125    size = len(src) 
    121126    sys.modules['ImageFile'] = ImageFile 
    122     return {'src':src, 'size':size} 
     127    return {'src': src, 'size': size} 
    123128 
    124129 
     
    127132    return words[index] 
    128133 
     134 
    129135def getIndex(word): 
    130136    words = basic_english.words.split() 
     
    132138        res = words.index(word) 
    133139    except ValueError: 
    134         res = getLen()+1 
     140        res = getLen() + 1 
    135141    return res 
     142 
    136143 
    137144def getCaptchasCount(dynamic): 
     
    141148        return CAPTCHAS_COUNT 
    142149 
     150 
    143151def formKey(num): 
    144152    def normalize(s): 
    145         return (not len(s)%8 and s) or normalize(s+str(randint(0, 9))) 
     153        return (not len(s) % 8 and s) or normalize(s + str(randint(0, 9))) 
    146154 
    147     return normalize('%s_%i_'%(str(DateTime().timeTime()), num)) 
     155    return normalize('%s_%i_' % (str(DateTime().timeTime()), num)) 
     156 
    148157 
    149158def encrypt(key, s): 
    150159    return toHex(Crypto.new(key).encrypt(s)) 
    151160 
     161 
    152162def decrypt(key, s): 
    153163    return Crypto.new(key).decrypt(toStr(s)) 
     164 
    154165 
    155166def parseKey(s): 
     
    157168    if ps is None: 
    158169        return {'date': '', 'key': ''} 
    159     return {'date': ps.group(1), 'key':ps.group(2)} 
     170    return {'date': ps.group(1), 'key': ps.group(2)} 
     171 
    160172 
    161173def toHex(s): 
     
    164176        hv = hex(ord(ch)).replace('0x', '') 
    165177        if len(hv) == 1: 
    166             hv = '0'+hv 
     178            hv = '0' + hv 
    167179        lst.append(hv) 
    168180 
    169     return reduce(lambda x,y:x+y, lst) 
     181    return reduce(lambda x, y: x + y, lst) 
     182 
    170183 
    171184def toStr(s): 
Note: See TracChangeset for help on using the changeset viewer.